For example, if I have a model called 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; }
}
Example:
var customers = new List<Customer>();
StackExchange.Redis is a raw client - it talks in Redis terms only.
created 2 extension methods, which help me serialize/deserialize complex type for Redis database.
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;
}
}
Usage:
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);
option:
AzureredisDb.Cache is СonnectionMultiplexer.Connect and GetDatabase();
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;
}
}
}
如果你仅仅为了缓存存一批量的 List<Customer> 数据, 那么自己封装一个ListGet() 和 ListSet()方法吧。
我对比过使用 List 和 String 两种类型存储。
Redis 的List类型和 .NET领域还有所不同,实际上,它是一个双向队列,可以左右插入值。
所以如果是批量数据插入 那么必须一个个插入, 代码比较简单如下:
//封装的ListSet
public void ListSet<T>(string key, List<T> value)
{
.....
//下面的database 是redis的数据库对象.
foreach (var single in value)
{
var s = ConvertJson(single); //序列化
database.ListRightPush(key, s); //要一个个的插入
}
}
//封装的ListGet
public void ListGet<T>(string key)
{
...
//ListRange返回的是一组字符串对象
//需要逐个反序列化成实体
var vList = database.ListRange(key) ;
List<T> result = new List<T>();
foreach (var item in vList)
{
var model = ConvertObj<T>(item); //反序列化
result.Add(model);
}
return result;
}