将单个大对象存储在memcached与多个键中 [英] store single big object in memcached vs multiple keys

查看:88
本文介绍了将单个大对象存储在memcached与多个键中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内存缓存中缓存了以下大对象(20KB)-

I have following large object (20KB) cached in memcache -

Product : 
{
    BasicInfo, //~5KB
    SellerId,  //int
    CityId,    //int
    AdditionalInfo //~15KB
}

可在多个地方访问它-

  1. 在某些地方,仅需要SellerId或cityId.
  2. 在某些地方,仅需要basicInfo.
  3. 在某些地方,需要整个对象.

因此,在第一种和第二种情况下,我们只需要一些字节就不需要获取整个对象.我是否应该将它们分别存储在内存缓存中(唯一的问题是我需要在更新时使多个密钥无效)?

So we are fetching whole object unnecessarily in 1st and 2nd cases while we only require some bytes. Should I store these separately in memcache (only problem is I need to invalidate multiple keys on updation)?

有没有更好的方法来处理这些情况?

Is there any better way to handle these cases?

推荐答案

这里有2个方面需要考虑,并且需要权衡取舍.

There are 2 aspects to consider here and the trade-off between them.

  1. 访问缓存数据的时间
  2. 数据一致性

如何存储小粒度数据将减少访问时间,即为什么复杂对象要花费更多时间?

How storing small granular data will reduce time to access i.e why complex object are taking more time?

  1. 为了将任何值存储在内存缓存中,必须对其进行序列化和反序列化.对象越复杂,它将在应用程序端添加更多的序列化和反序列化处理.

  1. For storing any value in memcache, it has to be serialized and deserialized. The more complex the object is, it will add more processing for serialization and de-serialization @ application end.

在生产环境中,通常将Memcache放在单独的机器中(相同的n/w或不同的n/w).在这种情况下,更大的数据大小会由于网络I/O和往返时间而增加延迟.

It is common in production to have memcache in a separate machine/s (same n/w or different n/w). In such cases, the bigger data size will add latency due to network i/o and roundtrip time.

使用完整对象或多个颗粒对象是一种折衷.

It is a trade-off to use complete object or multiple granular objects.

在我们的案例中,我们主要将缓存用于缓存数据,因此读取频繁且很少更新.我们存储粒度级别的对象.

In our case, where we use cache mostly for our cached data are high frequent read and rarely updated. We store granular level objects.

这篇关于将单个大对象存储在memcached与多个键中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆