如何按范围在 Redis 列表中添加自定义对象列表
How can i add List of custom object in Redis by range redis List
有什么方法可以在 redis 中添加我的项目列表 Stackexchange.Redis.Extensions。
我知道我可以使用 SetAdd 但它会一个接一个地插入项目。我想插入范围。
我还想要按范围检索项目。
即通过开始和结束索引
在下面的代码中,我能够通过块提取列表,但它 returns 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;
}
我将列表插入 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;
}
有什么方法可以在 redis 中添加我的项目列表 Stackexchange.Redis.Extensions。 我知道我可以使用 SetAdd 但它会一个接一个地插入项目。我想插入范围。 我还想要按范围检索项目。 即通过开始和结束索引 在下面的代码中,我能够通过块提取列表,但它 returns 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;
}
我将列表插入 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;
}