redisservicestackstackexchange.redisservicestack.redisservicestack-auth

Retrieve selection of servicestack redis session objects based on values of properties


I want to update multiple servicestack user sessions that are stored in redis. I want to return all sessions that have a custom property set to a certain value, then I can process them. At the moment the best solution I have returns all keys as such:

List<string> sessionkeys = redis.SearchKeys("urn:iauthsession:*");

I am thinking this will not scale well. I'd like to do something analogous to:

List<string> sessionkeys = redis.AllKeys.Where(x=>x.ParentId == 3);

Is this possible with redis, and if so how, ideally using standard libraries with ServiceStack.


Solution

  • Instead of SearchKeys you want to be using the newer Redis Scan API's which let you iterate over keys in a cursor.

    Redis values are blobbed and not indexed so there's not much opportunity to optimize this. The most efficient way would be to inspect the values using a custom server-side LUA operation which will minimize the number of requests and payload sent to the client.

    Redis embedded version of LUA has cjson which can be used for deserializing JSON values, the Lua guide for Redis users has some examples of using this.

    Since Redis doesn't support server-side querying or indexes, the Redis way would be to premept the queries you need to query on and maintain custom indexes whenever a Session is saved, some info on how to maintain custom indexes in Redis are at:

    In ServiceStack you can override OnSaveSession() in your AppHost which gets called whenever a Session is saved.