LRU
LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法。 什么是Cache?狭义的Cache指的是位于CPU和主存间的快速RAM, 通常它不像系统主存那样使用DRAM技术,而使用昂贵但较快速的SRAM技术。 广义上的Cache指的是位于速度相差较大的两种硬件之间, 用于协调两者数据传输速度差异的结构。除了CPU与主存之间有Cache, 内存与硬盘之间也有Cache,乃至在硬盘与网络之间也有某种意义上的Cache── 称为Internet临时文件夹或网络内容缓存等。
Cache的容量有限,因此当Cache的容量用完后,而又有新的内容需要添加进来时, 就需要挑选并舍弃原有的部分内容,从而腾出空间来放新内容。LRU Cache 的替换原则就是将最近最少使用的内容替换掉。其实,LRU译成最久未使用会更形象, 因为该算法每次替换掉的就是一段时间内最久没有使用过的内容 。
LRU Cache的数据结构选择
首先要保证put和get 操作时O(1),其次还要保持LRU特性。
综合各种数据结构,我们采用以下数据结构来实现。
list<pair<int, int>> _list; // 将最近用过的往链表的投上移动,保持LRU
// 使用unordered_map,让搜索效率达到O(1)
unordered_map<int, list<pair<int, int>>::iterator> _hashmap;
size_t _capacity; // 容量大小,超过容量则换出,保持LRU
LRU Cache的实现
class LRUCache {
public:
LRUCache(int capacity) {
_capacity = capacity;
}
int get(int key) {
// 如果key对应的值存在,则listit取出,这里就可以看出hashmap的value存的是list的
iterator的好处:找到key
// 也就找到key存的值在list中的iterator,也就直接删除,再进行头插,实现O(1)的数据
挪动。
auto hashit = _hashmap.find(key);
if(hashit != _hashmap.end())
{
auto listit = hashit->second;
pair<int, int> kv = *listit;
_list.erase(listit);
_list.push_front(kv);
_hashmap[key] = _list.begin();
return kv.second;
}
else
{
return -1;
}
}
void put(int key, int value) {
// 1.如果没有数据则进行插入数据
// 2.如果有数据则进行数据更新
auto hashit = _hashmap.find(key);
if(hashit == _hashmap.end())
{
// 插入数据时,如果数据已经达到上限,则删除链表头的数据和hashmap中的数据,两个
删除操作都是O(1)
if(_list.size() >= _capacity)
{
_hashmap.erase(_list.back().first);
_list.pop_back();
}
_list.push_front(make_pair(key,value));
_hashmap[key] = _list.begin();
}
else
{
// 再次put,将数据挪动list前面
auto listit = hashit->second;
pair<int, int> kv = *listit;
kv.second = value;
_list.erase(listit);
_list.push_front(kv);
_hashmap[key] = _list.begin();
}}
private:
list<pair<int, int>> _list; // 将最近用过的往链表的投上移动,保持LRUsize_t _capacity; // 容量大小,超过容量则换出,保持LR
unordered_map<int, list<pair<int, int>>::iterator> _hashmap;// 使用unordered_map,让搜索效率达到O(1)// 需要注意:这里最巧的设计就是将unordered_map的value type放成list<pair<int,int>>::iterator,因为这样,当get一个已有的值以后,就可以直接找到key在list中对应的
iterator,然后将这个值移动到链表的头部,保持LRU。
};