【发布时间】: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