如何使用ServiceStack将多个读取命令传递给Redis [英] How to pipeline multiple read commands to Redis using ServiceStack

查看:51
本文介绍了如何使用ServiceStack将多个读取命令传递给Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ServiceStack,是否可以执行多个读取命令(特别是ContainsKey命令)?

存储的对象需要一段时间才能从数据库中获取,因此我们希望仅获取那些不在缓存中的对象.

我知道我可以要求redis作为对象,然后从数据库中获取是否返回null,但是存储的对象相当大,所以我宁愿只取回bool列表,然后确定要从中查询数据库的ID.

目前,我正在遍历我的Ids列表(最多100个左右),并在servicestack中使用ContainsKey方法.我希望避免所有的来回回合,一口气提出所有要求.

解决方案

ServiceStack.Redis 客户端,您可以使用 GetValues GetValuesMap (字符串和T api均可用)来获取多个值(使用

使用管道或事务

如果由于某种原因您真的只想检查Redis中密钥的存在而不获取它们,那么您可以使用Transaction/或Pipeline(均已管道化)来排队多个操作,这些操作将在1个套接字中发送给Redis写.这是 RedisStackOverflow 演示中的一个示例,该示例将 Redis对服务器端LUA的支持来创建和执行复合操作.您可以从 IRedisClient 使用这些API.在ServiceStack.Redis客户端中执行服务器端LUA脚本:

  string GetEvalStr(string body,int numOfArgs,params string [] args);int GetEvalInt(字符串体,int numOfArgs,参数string [] args);List< string>GetEvalMultiData(字符串体,int numOfArgs,参数string [] args); 

Using ServiceStack, is there a way to perform multiple read commands (in particular the ContainsKey command)?

The objects stored take a while to fetch from the database, so we're looking to get only the ones that are not in cache.

I know that I can ask redis for the object and then fetch from the database if it comes back null, but the objects being stored are fairly large, so I'd rather just get a list of bools back and then determine which ids to query in the database from that.

For the time being I'm looping through my list of Ids (can be up to 100 or so) and using the ContainsKey method in servicestack. I'm looking to avoid all of the back and forth and make all of the requests in one shot.

解决方案

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);

这篇关于如何使用ServiceStack将多个读取命令传递给Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆