Implement the Least Recently Used (LRU) and First In First Out (FIFO) cache eviction policies, respectively.
Pick ONE OR MORE options.
// LRU cache = LinkedHashMap(maxEntries)
Function put(key, value)
if cache is full
remove the least recently accessed entry from cache
add the new entry to cache
// FIFO cache = Queue(maxEntries)
Function put(key, value)
if cache is full
remove the oldest entry from cache
add the new entry to cache
这道题考察缓存淘汰策略的基本理解。LRU(Least Recently Used)应使用能够维护访问顺序的数据结构,例如 LinkedHashMap,当缓存满时删除“最近最少使用”的条目;而 FIFO(First In First Out)则应使用队列,缓存满时删除最早进入的条目。题目的关键是区分“按最近访问时间淘汰”和“按进入顺序淘汰”,两者在实现思路和适用场景上都不同。
正文完