memcached expiration time

2019-03-12 19:15发布

Memcached provides a cache expiration time option, which specifies how long objects are retained in the cache. Assuming all writes are through the cache I fail to understand why one would ever want to remove an object from the cache. In other words, if all write operations update the cache before the DB, then the cache can never contain a stale object, so why remove it?

One possible argument is that the cache will grow indefinitely if objects are never removed, but memcached allows you to specify a maximum size. Once this size is reached, memcached uses a least-recently-used (LRU) algorithm to determine which items to remove. To summarise, if a sensible maximum size has been configured, and all writes are through the cache, why would you want to expire objects after a certain length of time?

Thanks, Don

8条回答
forever°为你锁心
2楼-- · 2019-03-12 20:04

Some data in cache is expensive to create but small (should last a long time) and some is large but relatively cheap (should last a shorter time)

Also, for most applications it is hard to make memcached work as a write through cache. It is difficult to properly invalidate all caches, especially those of rendered pages. Most users will miss a couple.

查看更多
淡お忘
3楼-- · 2019-03-12 20:05

If your design calls for a write-through cache, you still have an issue with coming up against the memory limit allocated to memcached which is where LRU comes into play.

LRU has two rules when determining what to kick out, and does so in the following order:

  1. Expired slabs
  2. Oldest unused slab

Providing different expiration dates for different groups of objects can help keep less-frequently accessed data that is more expensive to cache in memory while allowing more frequently used slabs that might still find their way to the end of the queue, but are easy to recreate, to expire.

It is also the case that many cache keys wind up becoming aggregates of other objects, and unless you employ a lookup hash for those objects, it's much easier to just let the objects expire after a few hours than to proactively update all the associated keys, and it also preserves the hit/miss ratio that you are effectively vying for by using memcached in the first place.

查看更多
登录 后发表回答