【问题标题】:How to store complex object in redis hash in c#?如何在 C# 中将复杂对象存储在 redis 哈希中?
【发布时间】: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


【解决方案1】:

可能最简单的方法是检查每个属性值是否是一个集合(请参阅我修改后的方法版本中的 cmets):

public static HashEntry[] ToHashEntries(this object obj)
{
    PropertyInfo[] properties = obj.GetType().GetProperties();
    return properties
        .Where(x => x.GetValue(obj) != null) // <-- PREVENT NullReferenceException
        .Select
        (
              property => 
              {
                   object propertyValue = property.GetValue(obj);
                   string hashValue;

                   // This will detect if given property value is 
                   // enumerable, which is a good reason to serialize it
                   // as JSON!
                   if(propertyValue is IEnumerable<object>)
                   {
                         // So you use JSON.NET to serialize the property
                         // value as JSON
                         hashValue = JsonConvert.SerializeObject(propertyValue);
                   }
                   else
                   {
                        hashValue = propertyValue.ToString();
                   }

                   return new HashEntry(property.Name, hashValue);
              }
        )
        .ToArray();
}

【讨论】:

    【解决方案2】:

    似乎序列化有问题。在 JSON 和 .NET 对象之间进行转换的最佳方式是使用 JsonSerializer

    JsonConvert.SerializeObject(fooObject);
    

    你可以在Serializing and Deserializing JSON看到更多细节。

    还有一个好方法,你可以试试IRedisTypedClient,它是ServiceStack.Redis的一部分。

    IRedisTypedClient - 可用的高级“强类型”API 在 Service Stack 的 C# Redis Client 上进行所有 Redis Value 操作 适用于任何 c# 类型。所有复杂类型都在哪里 使用 ServiceStack JsonSerializer 透明地序列化为 JSON - .NET 上最快的 JSON 序列化器。

    希望这会有所帮助。

    【讨论】:

    • 但是这个问题不是关于 SE.Redis 的吗?
    • @MatíasFidemraizer 对不起,我已经更新了我的答案,看来序列化有问题,这是使用Json.NET的好方法。
    猜你喜欢
    • 2021-11-30
    • 2014-03-26
    • 2015-05-27
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2015-05-26
    相关资源
    最近更新 更多