LRU 算法 All In One
LRU 算法 All In One
LRU (最近最少使用) 缓存机制
least recently used cache algorithm
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity;
this.map = new Map();
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
if(!this.map.has(key)) {
return -1;
}
const value = this.map.get(key);
// 更新
this.put(key, value);
return value;
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if(this.map.has(key)) {
this.map.delete(key)
}
if(this.map.size === this.capacity) {
// 空间满了,删除第一个 `.next()` ✅
const oldKey = this.map.keys().next.value;
this.map.delete(key)
}
this.map.set(key, value);
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
.next()
iterators
leetcode
https://leetcode.com/problems/lru-cache/
https://leetcode-cn.com/problems/lru-cache/
- Map
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity;
this.map = new Map();
// 换成数组, 使用索引从后往前找到第一个不是 last obj?
// this.last = -1;
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
// ✅ 优先处理代码少的逻辑,错误优先
if(!this.map.has(key)) {
return -1;
} else {
const value = this.map.get(key)
// 读取后,更新顺序
this.put(key, value);
return value;
}
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
// 替换式更新,先删除
if(this.map.has(key)) {
this.map.delete(key);
}
// 达到容量限制,先删除最近最少使用的 map 第一个元素
// if(this.map.size === this.capacity) {
// const oldKey = this.map.keys().next().value;
// this.map.delete(oldKey);
// }
// 再重新添加
this.map.set(key, value);
// 先添加,再判断是否达到容量限制
if(this.map.size > this.capacity) {
// 删除 old key (删除最近最少使用的 map 第一个元素)
this.map.delete(this.map.keys().next().value);
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
// 最近最少使用 (LRU) 缓存
- Double LinkedList / 双向链表
Cache replacement policies
缓存替换策略
https://en.wikipedia.org/wiki/Cache_replacement_policies
https://zh.wikipedia.org/wiki/快取文件置換機制
CPU cache
L1 D Cache L1 I Cache, 分别是 L1 Data Cache 和 L1 Instruction Cache
CPU Caches and Pipelining
A modern CPU
has a lot of different CPU caches
, which are fast and quite small compared to our large main memory (RAM).
The following picture gives you a high level overview about the various caches that are part of a modern CPU.
refs
https://www.cnblogs.com/xgqfrms/p/16351282.html#5064557
https://www.cnblogs.com/xgqfrms/p/16349492.html
https://www.geeksforgeeks.org/program-for-least-recently-used-lru-page-replacement-algorithm/
https://time.geekbang.org/course/detail/100019701-72543
https://time.geekbang.org/course/detail/100019701-72545
https://gitee.com/geektime-geekbang/algorithm-1/raw/master/20-LRU Cache.pdf
- LFU - least frequently used(最近最不常⽤, ⻚⾯置换算法)
- LRU - least recently usd(最近最少使⽤, ⻚⾯置换算法)
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13538917.html
未经授权禁止转载,违者必究!