How to pipeline multiple read commands to Redis using ServiceStack

↘锁芯ラ 提交于 2019-12-04 17:24:02

On the ServiceStack.Redis client you can use GetValues or GetValuesMap (both string and T apis are available) for fetching multiple values (uses MGET) in a single call. Only existing items are returned, which you can detect with GetValues if you have Id's on your models or GetValuesMap which returns a dictionary of existing keys and their values.

From this, you can determine and fetch all the missing Ids from the database. If you're just going to fetch the existing values from Redis anyway, then this is the most optimal strategy since you can fetch all the values and determine the existing keys with 1 call.

Alternative 'Single Call' options

Use Pipelining or Transactions

If for some reason you really only want to check the existence of keys in Redis without fetching them, then you can use a Transaction / or Pipeline (both are pipelined) to queue up multiple operations that gets sent to Redis in 1 socket write. Here's an example of this from RedisStackOverflow demo that queues up 30 operations that gets send and executed in a single batch.

Use LUA Scripting

If you have Redis >v2.5x you can create and execute composite operations using Redis's server-side LUA support. You can use these APIs from IRedisClient in ServiceStack.Redis client to execute server-side LUA scripts:

string GetEvalStr(string body, int numOfArgs, params string[] args);
int GetEvalInt(string body, int numOfArgs, params string[] args);
List<string> GetEvalMultiData(string body, int numOfArgs, params string[] args);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!