源起
最近买了一个学算法的小课,讲的非常不错,里面提到了LRU缓存机制。
其实这不是我第一次听说LRU。在我使用next.js的时候,我看过一篇文章,里面提到使用LRU缓存访问过的页面进行优化。
听说Redis里面也用到了LRU缓存机制。
由此,我找了一些资料学习了一下,为了防止再次忘记,在这里记录一下。
什么是LRU缓存机制
LRU缓存机制,即采用最近最少使用的缓存策略。它的原则是,如果一个数据最近没有被访问到,那么它将来被访问的几率也很小,也就是说当限定的内存空间已没有其他空间可用时,应该把最久没有访问到的数据去除掉。
如下图所示,它是这样工作的:
三个白块可以看作一个链表,链表遵从先进先出的规律,当这个链表被1,0,7塞满的时候,7作为最久没有使用的数据,被淘汰了。
0被使用了一次,因此遵从原则,将其从底部移到头部。
leetcode146题
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果关键字 (key) 存在于缓存中,则获取关键字的值(总是正数),否则返回 -1。
写入数据 put(key, value) - 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字/值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
进阶:
你是否可以在O(1)时间复杂度内完成这两种操作?
示例:
LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 该操作会使得关键字 2 作废
cache.get(2); // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得关键字 1 作废
cache.get(1); // 返回 -1 (未找到)
cache.get(3); // 返回 3
cache.get(4); // 返回 4
针对leetcode146题的题解
题目的要求是在O(1)时间复杂度完成这两种操作。
可以联想到Map和链表两种数据结构。
Map 存取key value的时间复杂度都是 O(1)
链表新增,删除节点的时间复杂度都是 O(1)
下面的图很清晰的反映了Map和链表在之间的关系:
我的题解
Java版本
class LRUCache {
private Map<Integer, LRUNode> cache = new HashMap<>();
private int count;
private int capacity;
private LRUNode head, tail;
public LRUCache(int capacity) {
this.count = 0;
this.capacity = capacity;
this.head = new LRUNode();
this.tail = new LRUNode();
head.next = tail;
tail.prev = head;
}
public int get(int key) {
LRUNode node = cache.get(key);
if (node != null) {
// 节点存在,最近使用,将节点放到顶部
this.moveToHead(node);
return node.value;
}
return -1;
}
public void put(int key, int value) {
LRUNode node = cache.get(key);
if (node != null) {
// 节点存在,最近使用,将节点放到顶部
node.value = value;
this.moveToHead(node);
} else {
// 节点不存在,新增一个节点到顶部
LRUNode newNode = new LRUNode(key, value);
this.cache.put(key, newNode);
this.addNode(newNode);
++this.count;
if (this.count > this.capacity) {
// 如果超过容量,删除底部节点
LRUNode tail = this.removeTail();
this.cache.remove(tail.key);
--this.count;
}
}
}
private void moveToHead(LRUNode node) {
this.removeNode(node);
this.addNode(node);
}
private void addNode(LRUNode node) {
node.prev = head;
node.next = head.next;
head.next.prev = node;
head.next = node;
}
private void removeNode(LRUNode node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private LRUNode removeTail() {
LRUNode res = tail.prev;
removeNode(res);
return res;
}
private class LRUNode {
int key;
int value;
LRUNode prev;
LRUNode next;
LRUNode() {}
LRUNode(int key, int value) {
this.key = key;
this.value = value;
}
}
}
JavaScript版本
/**
* @param {any} value
*/
var isNil = function(value) {
return value === null || value === undefined;
}
/**
* @param {number} key
* @param {number} value
*/
var LRUNode = function(key, value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.size = 0;
this.capacity = capacity;
this.cache = new Map();
this.head = new LRUNode(null, null);
this.tail = new LRUNode(null, null);
this.head.next = this.tail;
this.tail.prev = this.head;
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
var node = this.cache.get(key);
if (!isNil(node)) {
this.moveToHead(node);
return node.value;
}
return -1;
};
/**
* @param {number} node
*/
LRUCache.prototype.moveToHead = function(node) {
this.removeNode(node);
this.addNode(node);
};
/**
* @param {number} node
*/
LRUCache.prototype.removeNode = function(node) {
var prevNode = node.prev;
var nextNode = node.next;
if (!isNil(nextNode))
prevNode.next = node.next;
if (!isNil(nextNode))
nextNode.prev = node.prev;
};
/**
* @param {number} node
*/
LRUCache.prototype.addNode = function(node) {
node.prev = this.head;
node.next = this.head.next;
this.head.next.prev = node;
this.head.next = node;
};
/**
* @param {number} node
*/
LRUCache.prototype.removeTail = function() {
var node = this.tail.prev;
this.removeNode(node);
return node;
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
var node = this.cache.get(key);
if (!isNil(node)) {
node.value = value;
this.moveToHead(node);
} else {
var newNode = new LRUNode(key, value);
this.cache.set(key, newNode);
this.addNode(newNode);
this.size ++;
if (this.size > this.capacity) {
var tailNode = this.removeTail();
this.cache.delete(tailNode.key);
this.size --;
}
}
};
参考
- 力扣(LeetCode)leetcode-cn.com/problems/lr…