62,046
社区成员
发帖
与我相关
我的任务
分享
using StackExchange.Redis;
var appconfigList= MySqlHelper.QueryList<Appconfig>(strDB, "select * from Appconfig");
redisListService.ListRightPush("appconfigList5", appconfigList);
public long ListRightPush<T>(string redisKey, List<T> redisValue)
{
redisKey = AddKeyPrefix(redisKey);
foreach (var item in redisValue)
{
_db.ListRightPush(redisKey, Serialize(item));
}
return redisValue.Count;
}
public static byte[] Serialize(object obj)
{
try
{
if (obj == null)
return null;
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.GetBuffer();
}
}
catch (SerializationException ex)
{
throw ex;
}
}
取出时数据时。根据key取出RedisValue[],循环RedisValue[],然后把RedisValue转换成model,添加到list中
var aa = redisListService.ListRange<Appconfig>("appconfigList5");
public IEnumerable<T> ListRange<T>(string redisKey) where T : class
{
try
{
redisKey = AddKeyPrefix(redisKey);
var List = _db.ListRange(redisKey);
return ConvetList<T>(List);
}
catch (Exception ex)
{
throw ex;
}
}
public IEnumerable<T> ConvetList<T>(RedisValue[] values)
{
List<T> result = new List<T>();
foreach (var item in values)
{
var model = Deserialize<T>(item);
result.Add(model);
}
return result;
}
public static T Deserialize<T>(byte[] data)
{
if (data == null)
return default(T);
using (var memoryStream = new MemoryStream(data))
{
var binaryFormatter = new BinaryFormatter();
var result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}
移除数据时要循环索引移除,不能一次移除key的全部数据
long ListRemove(RedisKey key, RedisValue value, long count = 0, CommandFlags flags = CommandFlags.None);