【发布时间】:2017-02-11 18:47:56
【问题描述】:
我必须将复杂对象存储到 redis 现金的哈希中。我正在使用 stackexchange.redis 来执行此操作。我的类如下所示。
public class Company
{
public string CompanyName { get; set; }
public List<User> UserList { get; set; }
}
public class User
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Twitter { get; set; }
public string Blog { get; set; }
}
我在redis中存储数据的代码sn-p是:
db.HashSet("Red:10000",comapny.ToHashEntries());
//以Redis格式序列化:
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();
}
我可以将数据存储在 redis 中,但不是我想要的。我得到的结果如下图所示。
我想要 json 格式的 UserList 值。所以,我该怎么做。
【问题讨论】:
-
您可以尝试CachingFramework.Redis,它是 SE.Redis 的包装器,具有可配置序列化机制等增强功能。
标签: c# redis stackexchange.redis