Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

memcached - Will the LRU delete entries that have not been used for some amount of time?

When in memcache the available memory is full, memcache uses the LRU (last recently used) algorithm to free memory. My question is will the LRU Algorithm rather delete entries that have not been used for some amount of time (last recently used) than expired items? Entries that are expiring are not deleted on that exact moment but when the next time someone tries to access it (AFAIR). So will the LRU Algorithm (also) account for the expiry of keys?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To understand how memcached does LRU you must go deeper and understand how memcached stores items. Items are stored depending on their size, simply put all your items that are lets say 100k get stored in the same slab, while other items that are 200k are stored in a different slab.

When memory gets full and you try to store a 100k item, memcached will apply LRU on that slab. If there are keys expired or not used in the 200k slab, they remain there, while if the 100k slab has only hot keys, one of those based on the algorithm will be evicted.

Back to your question, when memory is full and you try to store an item, memcached will look first for expired items in the slab you are trying to write to, then look for the least used items. So yes, it does take into account the expiry of the keys, or better yet, expired keys go first before LRU.

Also, when you try to get an item which is past its expiration date, that item is evicted and the memory reclaimed.

More details on (lots on google for memcached memory allocation which explains LRU as well, so plenty to read on this):

http://returnfoo.com/2012/02/memcached-memory-allocation-and-optimization-2/

http://www.adayinthelifeof.nl/2011/02/06/memcache-internals/

And a really nice tool which I recommend on every memcached topic :

http://code.google.com/p/phpmemcacheadmin/

Hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...