14

例如,如果我有一个名为 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 的格式。

4

4 回答 4

16

可能会有所帮助。在开始研究StackExchange.Redis时,我也遇到了同样的问题。在我的项目中,我创建了 2 个扩展方法,它们帮助我序列化/反序列化 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);
于 2014-11-09T18:09:58.153 回答
15

StackExchange.Redis 是一个原始客户端——它只使用 Redis 术语。它不会尝试成为任何类型的 ORM。然而,它会存储任何stringbyte[]你想扔给它的东西——这意味着你应该可以选择序列化器。JSON 将是一个合理的默认值(Jil 很棒),尽管我们倾向于自己使用协议缓冲区(通过 protobuf-net)。

如果您打算使用列表语义,我强烈建议您从 List* 命令开始 - 集合与列表具有不同的语义 - 集合是无序的并且只存储唯一值;列表保留顺序并允许重复。

于 2014-09-17T19:42:09.710 回答
8

Andrey Gubal的回答进行了改进,以处理可空属性或空值:

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;
    }
}
于 2015-08-17T10:39:57.620 回答
3

这是一个选项

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 是 СonnectionMultiplexer.Connect 和 GetDatabase();

于 2015-03-22T18:06:01.633 回答