FIFO
最简单的一种缓存算法,设置缓存上限,当达到了缓存上限的时候,按照先进先出的策略进行淘汰,再增加进新的 k-v 。
使用了一个对象作为缓存,一个数组配合着记录添加进对象时的顺序,判断是否到达上限,若到达上限取数组中的第一个元素key,对应删除对象中的键值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class FifoCache{
constructor(limit){
this.limit = limit || 10
this.map = {}
this.keys = []
}
set(key,value){
let map = this.map
let keys = this.keys
if (!Object.prototype.hasOwnProperty.call(map,key)) {
if (keys.length === this.limit) {
delete map[keys.shift()]
}
keys.push(key)
}
map[key] = value
}
get(key){
return this.map[key]
}
}
module.exports = FifoCache
|
Salin selepas log masuk
LRU
LRU(Least recently used,最近最少使用)算法。该算法的观点是,最近被访问的数据那么它将来访问的概率就大,缓存满的时候,优先淘汰最无人问津者。
算法实现思路:基于一个双链表的数据结构,在没有满员的情况下,新来的 k-v 放在链表的头部,以后每次获取缓存中的 k-v 时就将该k-v移到最前面,缓存满的时候优先淘汰末尾的。
双向链表的特点,具有头尾指针,每个节点都有 prev(前驱) 和 next(后继) 指针分别指向他的前一个和后一个节点。
关键点:在双链表的插入过程中要注意顺序问题,一定是在保持链表不断的情况下先处理指针,最后才将原头指针指向新插入的元素,在代码的实现中请注意看我在注释中说明的顺序注意点!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | class LruCache {
constructor(limit) {
this.limit = limit || 10
this.head = this.tail = undefined
this.map = {}
this.size = 0
}
get(key, IfreturnNode) {
let node = this.map[key]
if (node === undefined) return
if (node === this.head) {
return returnnode ?
node :
node.value
}
if (node.prev) {
if (node === this.tail) {
this.tail = node.prev
}
node.prev.next = node.next
}
if (node.next) {
node.next.prev = node.prev
}
node.prev = undefined
node.next = this.head
if (this.head) {
this.head.prev = node
}
this.head = node
return IfreturnNode ?
node :
node.value
}
set(key, value) {
let node = this.get(key, true)
if (!node) {
if (this.size === this.limit) {
if (this.tail) {
this.tail = this.tail.prev
this.tail.prev.next = undefined
this.tail.prev = this.tail.next = undefined
this.map[this.tail.key] = undefined
this.size--
}
node = {
key: key
}
this.map[key] = node
if (this.head){
this.head.prev = node
node.next = this.head
} else {
this.head = node
this.tail = node
}
this.size++
}
}
node.value = value
}
}
module.exports = LruCache
|
Salin selepas log masuk
具体的思路就是如果所要get的节点不是头结点(即已经是最近使用的节点了,不需要移动节点位置)要先进行平滑的断链操作,处理好指针指向的关系,拿出需要移动到最前面的节点,进行链表的插入操作。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
在vue-router里query动态传参步骤有哪些
本地开发环境不能用IP访问如何处理
Atas ialah kandungan terperinci FIFO/LRU实现缓存算法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!