问题
(First of all, my English is not very good, please)
As we know, memcached provides lazy expiration, and "replaces" LRU data in its slabs, however I'm not very clear how it does this. For example, if a slab is full, but some data in this slab are expired, what will happen when data are added to the slab?
- Does memcached find some expired data and replace them with the added data, or
- does it replace the LRU data, or
- does it do something else?
As far as I know, the lazy expiration is such that memcached is not actively removing expired data from each slab, but instead only removing expired entries when the key of the expired entry is referenced. This is a waste of resources, isn't it?
回答1:
When an item is requested (a get request) Memcached checks the expiration time to see if the item is still valid before returning it to the client.
Similarly when adding a new item to the cache, if the cache is full, it will look at for expired items to replace before replacing the least used items in the cache.
So expired items are only purged when a get request is sent for the expired item or the expired item is cleared because the storage is needed.
来源:https://stackoverflow.com/questions/5986867/how-does-the-lazy-expiration-mechanism-in-memcached-operate