Memcached best practices - small objects and lots of keys or big objects and few keys?

。_饼干妹妹 提交于 2019-12-31 10:31:50

问题


I use memcached to store the integer result of a complex calculation. I've got hundreds of integer objects that I could cache! Should I cache them under a single key in a more complex object or should I use hundreds of different keys for the objects? (the objects I'm caching do not need to be invalidated more than once a day)


回答1:


I would say lots of little keys. This way you can get the exact result you want in 1 call with minimal serialization effort.

If you store it in another object (an array for example) you will have to fetch the array from cache and then fetch the item you actually want again from that array, plus you have the overhead of serializing/deserializing the whole complex object again. Depending on your language of choice this might mean manually writing a serialization/deserialization function from scratch.




回答2:


I wrote somewhat large analysis at http://dammit.lt/2008/12/25/memcached-for-small-objects/ - it outlines how to optimize memcached for small object storage - it may shed quite some light on the issue.




回答3:


It depends on your application. While memcached is very fast, it does require some request transmission and memory lookup time per request. Those numbers increase depending on whether or not the server is on the local machine (localhost), on the local network, or across a wide area. The size of your cache generally doesn't affect the lookup speed.

So, if your application is using MANY objects per processing unit (per request, method, or what-have-you), then it's generally better to define your cache in a way which lowers total number of hits to the cache while at the same time trying not to duplicate cache data. Like everything else, it's a balance.

i.e. If you have a web request which pulls a list of blog posts, it would be more beneficial to cache the entire object list as one memcached key, rather than (and this is a somewhat bad example, obviously) caching an array of cache keys for that list, which relate to individually memcached objects.




回答4:


The less processing you have to do of the cached values, the better. So why not just dump them into the cache individually?




回答5:


I would say you should store values individually and use some kind of helper class to retrieve values with multiget and generate a complex dataobject for you.




回答6:


It depends on what are those numbers. If you could, for example, group them in ranges, then you could optimize the storage. If you could hash them, into a map, or hashtable and store that map serialized in memcached would be good to.

Anyway, you can save many little keys, just make sure you configure the slabs to have chunks with small size, so you will not waste memory space.



来源:https://stackoverflow.com/questions/437773/memcached-best-practices-small-objects-and-lots-of-keys-or-big-objects-and-few

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!