【问题标题】:How can i add List of custom object in Redis by range redis List如何通过范围 redis 列表在 Redis 中添加自定义对象列表
【发布时间】:2016-10-02 05:52:20
【问题描述】:

有什么方法可以在 redis Stackexchange.Redis.Extensions 中添加我的对象列表。 我知道我可以使用 SetAdd 但它会一一插入项目。我想插入范围。 我还想按范围检索项目。 即按开始和结束索引 在以下代码中,我能够通过块提取列表,但它返回 RedisValue []。如何将其转换为我的列表

注意

  public static List<T> GetListItemRange<T>(string key, int start, int chunksize) where T : class
    {
        List<T> obj = default(List<T>);
        try
        {
            if (Muxer != null && Muxer.IsConnected && Muxer.GetDatabase() != null)
            {
                var cacheClient = new StackExchangeRedisCacheClient(Muxer, new NewtonsoftSerializer());
                var values = cacheClient.Database.ListRange(key, start, (start + chunksize - 1));
                obj = Array.ConvertAll(values, x => JsonConvert.DeserializeObject<T>(x)).ToList();
            }
        }
        catch (Exception ex)
        {
            Logger.Fatal(ex.Message, ex);
        }
        return obj;
    }

而目前的列表添加部分是

public static bool InsertListItemRange<T>(string key, List<T> obj) where T : class
        {
            bool result = false;
            try
            {
                if (Muxer != null && Muxer.IsConnected && Muxer.GetDatabase() != null)
                {
                    var cacheClient = new StackExchangeRedisCacheClient(Muxer, new NewtonsoftSerializer());
                    foreach (var o in obj)
                    {
                        result = cacheClient.ListAddToLeft(key, o) > 0;    
                    }                    
                }
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message, ex);
            }
            return result;
        }

【问题讨论】:

  • 它正在添加简单的字符串,而我想添加我的自定义类的项目列表。
  • 现在唯一的问题是列表中的一项一项。我可以如何通过批量插入将我的 List 插入 Redis List

标签: c# caching redis stackexchange.redis .net-4.5.2


【解决方案1】:

我将列表插入 Redis 的代码是

public static bool InsertListItemRange<T>(string key, List<T> obj, int chunksize = 25000) where T : class
        {
            bool result = false;
            try
            {
                if (Muxer != null && Muxer.IsConnected && Muxer.GetDatabase() != null)
                {
                    var cacheClient = new StackExchangeRedisCacheClient(Muxer, new NewtonsoftSerializer());

                    List<RedisValue> lst  = new List<RedisValue>();

                    foreach (var o in obj)
                    {
                       lst.Add(JsonConvert.SerializeObject(o));   
                    }


                    result = cacheClient.Database.ListLeftPush(key, lst.ToArray()) > 0;


                    if (LogRedisRelatedActivities)
                    {
                        Logger.InfoFormat("InsertItem => Key: {0}, Result: {1}", key, result);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message, ex);
            }
            return result;
        }

也用于通过范围获取我的代码是

public static List<T> GetListItemRange<T>(string key, int start, int chunksize) where T : class
        {
            List<T> obj = default(List<T>);
            try
            {
                if (Muxer != null && Muxer.IsConnected && Muxer.GetDatabase() != null)
                {
                    var cacheClient = new StackExchangeRedisCacheClient(Muxer, new NewtonsoftSerializer());
                    var redisValues = cacheClient.Database.ListRange(key, start, (start + chunksize - 1));
                    if (redisValues.Length > 0)
                    {
                        obj = Array.ConvertAll(redisValues, value => JsonConvert.DeserializeObject<T>(value)).ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message, ex);
            }
            return obj;
        }

【讨论】:

  • 这个还在一一插入,好像没有addrange ryt的选项?
  • 一口气插入。结果 = cacheClient.Database.ListLeftPush(key, lst.ToArray()) > 0;
  • 或者它可以在一行中,结果 = cacheClient.Database.ListLeftPush(key, obj.Select(JsonConvert.SerializeObject).Select(dummy => (RedisValue) dummy).ToArray() ) > 0;
猜你喜欢
  • 2020-08-26
  • 2014-11-11
  • 1970-01-01
  • 2021-02-23
  • 2011-08-30
  • 2018-09-10
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
相关资源
最近更新 更多