如何使用某些命令或LUA脚本读取Redis上存储的多个Set
我想在单个调用中使用键列表从Redis获取所有集合。根据文档,Redis为此提供了SSCAN命令,但是由于我使用StackExchange.Redis作为Redis适配器,因此我猜想此命令在此适配器中没有任何此类方法。所以我要寻找两件事:
- 我期待使用LUA脚本执行SSCAN,但无法在Internet上找到任何此类示例。任何人都可以共享使用多个SET键从LUA调用SSCAN的方法。
- 同样对于StackExchange.Redis,如果我在一个事务中执行多个SetMembers(),是否类似于使用LUA脚本使用SSCAN()命令?
谢谢
回答:
在单个调用中获取许多SET的示例C#代码如下:我使用StackExchange.Redis作为Redis连接器:
using StackExchange.Redis;using System;
using System.Text;
namespace RedisGetMultipleKeys
{
/// <summary>
/// Class to perofrme operations on SE.Redis
/// </summary>
class Program
{
/// <summary>
/// Executes necessary pre-requisites
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//Connect Redis
var _cache = Program.Connect();
//Store 10k Sets
string prefix = "user";
StringBuilder keys = new StringBuilder();
for (int i = 0; i < 10000; i++)
{
keys.Append(" " + prefix + i);
_cache.SetAdd(prefix + i, i);
}
var keyList = new RedisKey[10000];
//Generate keys array
for (int i = 0; i < 10000; i++)
{
var key = new RedisKey();
key = prefix + i;
keyList.SetValue(key, i);
}
var startTime = DateTime.Now;
//Perform SUNION
var values = _cache.SetCombine(SetOperation.Union, keyList);
var endTime = DateTime.Now;
TimeSpan diff = endTime.Subtract(startTime);
Console.WriteLine("total time taken to read 10k keys = " + diff);
Console.Read();
//TODO: to be changed accordingly to read Set values returned other than String
foreach (var value in values)
{
Console.WriteLine(value.ToString());
}
endTime = DateTime.Now;
diff = endTime.Subtract(startTime);
Console.WriteLine("total time taken to read 10k keys = " + diff);
Console.Read();
}
/// <summary>
/// Connects to Redis db
/// </summary>
/// <returns>Returns an instance of Redis db</returns>
private static IDatabase Connect()
{
string redisConnection = "localhost:6379,ssl=false,allowAdmin=true,ConnectRetry=3,ConnectTimeout=5000,defaultDatabase=1";
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnection);
return connection.GetDatabase();
}
}
}
我希望它能帮助C#开发人员寻找解决方案。由于Mgravell从SE.Redis开发团队由他的建议帮助我。在GitHub上可以找到更多讨论,如何通过在单个调用中传递集合密钥列表来获取多个集合
以上是 如何使用某些命令或LUA脚本读取Redis上存储的多个Set 的全部内容, 来源链接: utcz.com/qa/420096.html