Water
Water

Reputation: 1182

Return subset of Redis Values matching specific property?

Let's say I have this kind of model:

public class MyModel 
{
    public long ID { get; set; }
    public long ParentModelID { get; set; }
    public long ReferenceID1 { get; set; }
    public long ReferenceID2 { get; set; }
}

There are more attributes, but for examples sake, it is just this. There are around 5000 - 10000 rows of this model. Currently storing it in a Redis Set.

Is there an efficient way in REDIS to query only a subset of the whole Data Set? For example, in LINQ I can do:

allModels.Where(m => m.ParentModelID == my_id);

or

allModels.Where(m => m.ReferenceID1 == my_referenceid);

Basically, being able to search through the dataset without returning the whole dataset and performing the LINQ queries against that. Because querying and returning 10,000 rows to get only 100 is not efficient?

Upvotes: 0

Views: 591

Answers (2)

Itamar Haber
Itamar Haber

Reputation: 49942

You can use an OHM (Object-Hash Mapper, like ORM) in your favorite language to achieve the LINQ-like behavior. There are quite a few listed under the "Higher level libraries and tools" section of the [Redis Clients page](https://redis.io/clients.

Alternatively, you can implement it yourself using the patterns described at https://redis.io/topics/indexes.

Upvotes: 1

J Marlow
J Marlow

Reputation: 827

You can't use something like LINQ in Redis out of the box. Redis is just a key-value store, so it doesn't have the same principles or luxuries as a relational database. It doesn't have queries or relations, so something like LINQ just doesn't translate at all.

As a workaround, you could segment your data using different keys. Each key could reference a set that stores values with a specific range of reference Ids. That way you wouldn't need to retrieve all 10,000 items.

I would also recommend looking at hashes, this might be more appropriate than a set depending on your use case as they're better at storing complex data objects.

Upvotes: 0

Related Questions