如何使用某些命令或LUA脚本读取Redis上存储的多个Set [英] How to read multiple Sets stored on Redis using some command or LUA script

查看:1009
本文介绍了如何使用某些命令或LUA脚本读取Redis上存储的多个Set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一次呼叫中的键列表从Redis获取所有集合.根据文档,Redis为此提供了SSCAN命令,但是由于我使用StackExchange.Redis作为Redis适配器,因此我猜想此命令在此适配器中没有任何这样的方法.所以我要寻找两件事:

I want to get all the sets from Redis using a list of keys in single call. As per the documentation, Redis provides SSCAN command for that but as I am using StackExchange.Redis as a Redis adapter, I guess this command does not have any such method in this adapter. So there two things I am looking for:

  • 我期待使用LUA脚本执行SSCAN,但无法在Internet上找到任何此类示例.谁能分享使用多个SET键从LUA调用SSCAN的方法.
  • 对于StackExchange.Redis,如果我在一个事务中执行多个SetMembers(),是否类似于使用LUA脚本使用SSCAN()命令一样?

谢谢

推荐答案

在单个调用中获取许多SET的示例C#代码如下: 我正在使用StackExchange.Redis作为Redis连接器:

The sample C# code to get many SETs in a single call is as following: I am using StackExchange.Redis as a Redis connector:

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#开发人员寻找解决方案.感谢SE.Redis开发团队的 Mgravell 的帮助,他为我提供了建议.在GitHub 如何通过在一次通话

I hope it will help the C# developers looking for the solution. Thanks to Mgravell from SE.Redis dev team for helping me by his suggestions. More discussion could be found here at GitHub How to get multiple sets by passing set key list in a single call

这篇关于如何使用某些命令或LUA脚本读取Redis上存储的多个Set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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