如何通过StackExchange.Redis将泛型列表添加到Redis
本文关键字:Redis 列表 添加 泛型 何通过 StackExchange | 更新日期: 2023-09-27 18:22:11
例如,如果我有一个名为Customer 的模型
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string City { get; set; }
public string State { get; set; }
}
示例:
var customers = new List<Customer>();
我将如何添加客户列表?我该怎么做?
using (var redis = ConnectionMultiplexer.Connect(this.redisServer))
{
var db = redis.GetDatabase();
db.SetAdd(key, ?????);
}
我认为SetAdd是正确的方法,但我不知道如何获得我的客户通用列表(即,列表格式为RedisValue.
StackExchange.Redis是一个原始客户端,它只使用Redis术语。它并不试图成为任何形式的ORM。然而,它将存储您想要向它抛出的任何string
或byte[]
——这意味着您应该选择序列化程序。JSON将是一个合理的默认值(Jil非常棒),尽管我们倾向于自己使用协议缓冲区(通过protobuf-net)。
如果您打算使用列表语义,我强烈建议从list*命令开始——集合与列表具有不同的语义——集合是无序的,只存储唯一的值;列表保持顺序并允许重复。
可能会有所帮助。在开始研究StackExchange.RRedis时,我也遇到了同样的问题。在我的项目中,我创建了两个扩展方法,它们帮助我为Redis数据库序列化/反序列化复杂类型。你可以根据自己的需要扩展它们。
方法:
public static class RedisUtils
{
//Serialize in Redis format:
public static HashEntry[] ToHashEntries(this object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
return properties.Select(property => new HashEntry(property.Name, property.GetValue(obj).ToString())).ToArray();
}
//Deserialize from Redis format
public static T ConvertFromRedis<T>(this HashEntry[] hashEntries)
{
PropertyInfo[] properties = typeof(T).GetProperties();
var obj = Activator.CreateInstance(typeof(T));
foreach (var property in properties)
{
HashEntry entry = hashEntries.FirstOrDefault(g => g.Name.ToString().Equals(property.Name));
if (entry.Equals(new HashEntry())) continue;
property.SetValue(obj, Convert.ChangeType(entry.Value.ToString(), property.PropertyType));
}
return (T)obj;
}
}
用法:
var customer = new Customer
{
//Initialization
};
Db.HashSet("customer", customer.ToHashEntries());
Customer result = Db.HashGetAll("customer").ConvertFromRedis<Customer>();
Assert.AreEqual(customer.FirstName, result.FirstName);
Assert.AreEqual(customer.LastName, result.LastName);
Assert.AreEqual(customer.Address1, result.Address1);
对Andrey Gubal的答案的改进,以处理可为null的属性或null值:
public static class RedisUtils
{
//Serialize in Redis format:
public static HashEntry[] ToHashEntries(this object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
return properties
.Where(x=> x.GetValue(obj)!=null) // <-- PREVENT NullReferenceException
.Select(property => new HashEntry(property.Name, property.GetValue(obj)
.ToString())).ToArray();
}
//Deserialize from Redis format
public static T ConvertFromRedis<T>(this HashEntry[] hashEntries)
{
PropertyInfo[] properties = typeof(T).GetProperties();
var obj = Activator.CreateInstance(typeof(T));
foreach (var property in properties)
{
HashEntry entry = hashEntries.FirstOrDefault(g => g.Name.ToString().Equals(property.Name));
if (entry.Equals(new HashEntry())) continue;
property.SetValue(obj, Convert.ChangeType(entry.Value.ToString(), property.PropertyType));
}
return (T)obj;
}
}
这是一个选项
public static class StackExchangeRedisExtensions
{
public static T Get<T>(string key)
{
var connect = AzureredisDb.Cache;
var r = AzureredisDb.Cache.StringGet(key);
return Deserialize<T>(r);
}
public static List<T> GetList<T>(string key)
{
return (List<T>)Get(key);
}
public static void SetList<T>(string key, List<T> list)
{
Set(key, list);
}
public static object Get(string key)
{
return Deserialize<object>(AzureredisDb.Cache.StringGet(key));
}
public static void Set(string key, object value)
{
AzureredisDb.Cache.StringSet(key, Serialize(value));
}
static byte[] Serialize(object o)
{
if (o == null)
{
return null;
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, o);
byte[] objectDataAsStream = memoryStream.ToArray();
return objectDataAsStream;
}
}
static T Deserialize<T>(byte[] stream)
{
if (stream == null)
{
return default(T);
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(stream))
{
T result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}
}
AzureredisDb.Cache是СconnectionMultiplex.Connect和GetDatabase();