【问题标题】:Performance of RedisClient.Get<T> C# with ServiceStack.RedisRedisClient.Get<T> C# 与 ServiceStack.Redis 的性能
【发布时间】:2012-10-29 09:00:11
【问题描述】:
public class MyEntity
{
    public string Att1 { get; set; }
    public DateTime Att2 { get; set; }
    public KeyValuePair Att3 { get; set; }
    public Dictionary Att4 { get; set; }
}

var list = new List<MyEntity>(100);
//put to cache .....
var cached = RedisClient.Get<List<MyEntity>>(key) ; // take 9745.9745 ms
var raw = RedisClient.Get(key); //get raw of the same key just take < 10 ms

我应该使用 Json.net 进行 json 序列化并改用 RedisClient.Get 吗?

【问题讨论】:

    标签: c# redis json.net servicestack


    【解决方案1】:

    您很可能会受到第一次缓存命中惩罚的影响。 将每个 API 的每次调用中的第一个排除在计时之外。

    RedisClient 使用下面的 JsonSerializer,它做的事情完全一样,从 Redis 中拉出一个字符串并调用 JsonSerializer 来反序列化类型。

    【讨论】:

    • “第一次缓存命中惩罚”是什么意思?如果我改变这个顺序,结果是一样的。 var raw = RedisClient.Get(key); //获取相同密钥的原始数据只需 (key) ; // 耗时 9745.9745 毫秒
    • 第一次调用比其他调用花费更长的时间,因为它需要准备委托缓存。如果您只是得到一个字符串,那么您在第二次通话中所做的工作就会减少。 10s 是一个疯狂的时间,用于反序列化任何东西。使用泛型您将获得更快的结果和更小的负载,因为您不必绑定到后期绑定的对象类型。
    • 如果我多次这样做,我得到的都是一样的 var cached = RedisClient.Get>(key) ; // 耗时 9745.9745 毫秒 var cached1 = RedisClient.Get>(key) ; // 耗时 9745.9745 毫秒 var cachedn = RedisClient.Get>(key) ; // 耗时 9745.9745 毫秒
    • 我的意思是在您的模型中也使用通用集合,即不要使用 .NET 1.0 对象集合,例如 DictionaryKeyValuePair - 改用它们的通用等价物。
    • Att3 & Att4 的类型分别为:System.Collections.Generic KeyValuePair 和 System.Collections.Generic.Dictionary
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多