Search code examples
servicestack.redis

ServiceStack.Redis: Query a subset of objects by object properties stored using redisClient.StoreAll()


I have list of POCO objects (~80k). I have tried different ways to store these objects in Redis.

Refer to redisClient.StoreAll() at http://docs.servicestack.net/redis-client/redis-client. In order to retrieve all of the stored objects you do redisClient.GetAll(). I would like to know how can i query subset of objects based on a criteria.


Solution

  • It's not clear what you mean by querying since data in Redis is typically accessed by key and Redis doesn't have any explicit support for querying values which are effectively opaque to redis.

    I recommend reading this previous answer on how you can store related objects in Redis using the ServiceStack.Redis client. Which shows how you can use Indexes to create relationships between types.

    If you just want to search through keys you can use Redis Scan APIs, e.g:

    var userKeyPattern = IdUtils.CreateUrn<User>("*"); //= urn:User:*
    var scanUsers = Redis.ScanAllKeys(userKeyPattern);
    //Stop after retrieving 10000 user keys 
    var sampleUsers = scanUsers.Take(10000).ToList(); 
    

    But you can't do custom adhoc server-side querying of Redis Values unless you create a custom LUA script to parse the JSON value payload. You would need to create custom indexes for all the relationships you want to maintain otherwise you will need to fetch the results on the client and query them in memory.