如何使用Redis缓存缓存大对象 [英] How to cache large objects using Redis cache
问题描述
我们当前的缓存实现在报表对象中缓存了大量数据(在某些情况下为 50MB).
Our current caching implementation caches large amounts of data in report objects (50MB in some cases).
我们已经从内存缓存转移到文件缓存,并使用 ProtoBuf 进行序列化和反序列化.这很有效,但是我们现在正在试验 Redis 缓存.下面是 Redis 比使用文件系统需要多长时间的示例.(注意:在下面的示例中,当设置字节数组时,使用 protobuf 代替 JsonConvert 将设置时间提高到 15 秒,并将时间提高到 4 秒).
We’ve moved from memory cache to file cache and use ProtoBuf to serialize and de-serialize. This works well, however we are now experimenting with Redis cache. Below is an example of how much longer it takes for Redis than using the file system. (Note: using protobuf instead of JsonConvert improves set time to 15 seconds and get time to 4 seconds in the below example, when setting a byte array).
// Extremely SLOW – caching using Redis (JsonConvert to serialize/de-serialize)
IDatabase cache = Connection.GetDatabase();
// 23 seconds!
cache.StringSet("myKey", JsonConvert.SerializeObject(bigObject));
// 5 seconds!
BigObject redisResult = JsonConvert.DeserializeObject<BigObject>(cache.StringGet("myKey"));
// FAST - caching using file system (protobuf to serialize/de-serialize)
IDataAccessCache fileCache = new DataAccessFileCache();
// .5 seconds
fileCache.SetCache("myKey",bigObject);
// .5 seconds
BigObject fileResult = fileCache.GetCache<BigObject>("myKey");
在此先感谢您的帮助.
ps.我没有从提出的类似问题中找到答案.缓存大对象 - LocalCache 性能
ps. I didn’t find an answer from similar questions asked. Caching large objects - LocalCache performance
或
推荐答案
Redis 实际上并不是为存储大对象(许多 MB)而设计的,因为它是一个单线程服务器.因此,一个请求将足够快,但一些请求会很慢,因为它们都将由一个线程处理.上个版本做了一些优化.
Redis actually is not designed for storing large objects (many MB) because it is a single-thread server. So, one request will be fast enough, but a few requests will be slow because they all will be processed by one thread. In the last versions some optimizations were done.
RAM 速度和内存带宽对于全局性能似乎不太重要,尤其是对于小对象.对于大对象(> 10 KB),它可能会变得很明显.通常,购买昂贵的快速内存模块来优化Redis并不是真正划算的.https://redis.io/topics/benchmarks
Speed of RAM and memory bandwidth seem less critical for global performance especially for small objects. For large objects (>10 KB), it may become noticeable though. Usually, it is not really cost-effective to buy expensive fast memory modules to optimize Redis. https://redis.io/topics/benchmarks
因此,如果可能,您可以使用 Jumbo 帧或购买更快的内存.但实际上它不会有太大帮助.考虑改用 Memcached.它是多线程的,可以横向扩展以支持大量数据.Redis 只能通过主从复制进行扩展.
So, you can use Jumbo frames or buy a faster memory if it is possible. But actually it won't help significantly. Consider using Memcached instead. It is multi-threaded and can be scaled out horizontally to support large amount of data. Redis can be scaled only with master-slave replication.
这篇关于如何使用Redis缓存缓存大对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!