LRU cache design

后端 未结 11 2028
借酒劲吻你
借酒劲吻你 2020-11-27 09:26

Least Recently Used (LRU) Cache is to discard the least recently used items first How do you design and implement such a cache class? The design requirements are as follows:

11条回答
  •  被撕碎了的回忆
    2020-11-27 10:06

    Is cache a data structure that supports retrieval value by key like hash table? LRU means the cache has certain size limitation that we need drop least used entries periodically.

    If you implement with linked-list + hashtable of pointers how can you do O(1) retrieval of value by key?

    I would implement LRU cache with a hash table that the value of each entry is value + pointers to prev/next entry.

    Regarding the multi-threading access, I would prefer reader-writer lock (ideally implemented by spin lock since contention is usually fast) to monitor.

提交回复
热议问题