Design and code a LRU cache

This is a typical technical interview question.

First, we have to know what is a LRU cache.  http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used

Least Recently Used (LRU): discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item. General implementations of this technique require keeping "age bits" for cache-lines and track the "Least Recently Used" cache-line based on age-bits. In such implementation, every time a cache-line is used, the age of all other cache-lines changes. 

1 每个页,设置一个计数器。每次hit的页面设为0,其他的都加一。


posted @ 2011-07-29 15:30  Sw_R  阅读(154)  评论(0编辑  收藏  举报