|
| 1 | +// import { FrequencySketch } from '../../src/Browser/FrequencySketch.js' |
| 2 | + |
| 3 | +/***** |
| 4 | +* Overall w-TinyLFU Cache |
| 5 | +*****/ |
| 6 | +export default function WTinyLFUCache (capacity) { |
| 7 | + this.capacity = capacity; |
| 8 | + this.sketch = {}; |
| 9 | + |
| 10 | + // initialize window cache with access to frequency sketch |
| 11 | + this.WLRU = new LRUCache(capacity * .01); |
| 12 | + this.WLRU.sketch = this.sketch; |
| 13 | + // initialize segmented main cache with access to frequency sketch |
| 14 | + this.SLRU = new SLRUCache(capacity * .99); |
| 15 | + this.SLRU.probationaryLRU.sketch = this.sketch; |
| 16 | + this.SLRU.protectedLRU.sketch = this.sketch; |
| 17 | +} |
| 18 | + |
| 19 | +WTinyLFUCache.prototype.putAndPromote = async function (key, value) { |
| 20 | + const WLRUCandidate = this.WLRU.put(key, value); |
| 21 | + // if adding to the WLRU cache results in an eviction... |
| 22 | + if (WLRUCandidate) { |
| 23 | + // if the probationary cache is at capacity... |
| 24 | + let winner = WLRUCandidate; |
| 25 | + if (this.SLRU.probationaryLRU.nodeHash.size >= Math.floor(this.SLRU.probationaryLRU.capacity)) { |
| 26 | + // send the last accessed item in the probationary cache to the TinyLFU |
| 27 | + const SLRUCandidate = this.SLRU.probationaryLRU.getCandidate(); |
| 28 | + // determine which item will improve the hit-ratio most |
| 29 | + winner = await this.TinyLFU(WLRUCandidate, SLRUCandidate); |
| 30 | + } |
| 31 | + // add the winner to the probationary SLRU |
| 32 | + this.SLRU.probationaryLRU.put(winner.key, winner.value); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +WTinyLFUCache.prototype.TinyLFU = function (WLRUCandidate, SLRUCandidate) { |
| 37 | + // get the frequency values of both items |
| 38 | + const WLRUFreq = this.sketch[WLRUCandidate.key]; |
| 39 | + const SLRUFreq = this.sketch[SLRUCandidate.key]; |
| 40 | + // return the object with the higher frequency, prioritizing items in the window cache, |
| 41 | + return WLRUFreq >= SLRUFreq ? WLRUCandidate : SLRUCandidate; |
| 42 | +} |
| 43 | + |
| 44 | +/***** |
| 45 | +* Main SLRU Cache |
| 46 | +*****/ |
| 47 | +function SLRUCache(capacity) { |
| 48 | + // Probationary LRU Cache using existing LRU structure in lruBrowserCache.js |
| 49 | + this.probationaryLRU = new LRUCache(capacity * .20); |
| 50 | + // Protected LRU Cache |
| 51 | + this.protectedLRU = new LRUCache(capacity * .80); |
| 52 | +} |
| 53 | + |
| 54 | +// Get item from cache, updates last access, |
| 55 | +// and promotes existing items to protected |
| 56 | +SLRUCache.prototype.get = function (key) { |
| 57 | + // get the item from the protectedLRU |
| 58 | + const protectedItem = this.protectedLRU.get(key); |
| 59 | + // check to see if the item is in the probationaryLRU |
| 60 | + const probationaryItem = this.probationaryLRU.peek(key); |
| 61 | + |
| 62 | + // If the item is in neither segment, return undefined |
| 63 | + if (protectedItem === null && probationaryItem === null) return; |
| 64 | + |
| 65 | + // If the item only exists in the protected segment, return that item |
| 66 | + if (protectedItem !== null) return protectedItem; |
| 67 | + |
| 68 | + // If the item only exists in the probationary segment, promote to protected and return item |
| 69 | + // if adding an item to the protectedLRU results in ejection, demote ejected node |
| 70 | + this.probationaryLRU.delete(key); |
| 71 | + this.putAndDemote(key, probationaryItem); |
| 72 | + return probationaryItem; |
| 73 | +} |
| 74 | + |
| 75 | +// add or update item in cache |
| 76 | +SLRUCache.prototype.put = function (key, node) { |
| 77 | + // if the item is in the protected segment, update it |
| 78 | + if (this.protectedLRU.nodeHash.get(key)) this.putAndDemote(key, node); |
| 79 | + else if (this.probationaryLRU.nodeHash(key)) { |
| 80 | + // if the item is in the probationary segment, |
| 81 | + // promote and update it |
| 82 | + this.probationaryLRU.delete(key); |
| 83 | + this.putAndDemote(key, node); |
| 84 | + } |
| 85 | + // if in neither, add item to the probationary segment |
| 86 | + else this.probationaryLRU.put(key, node) |
| 87 | +} |
| 88 | + |
| 89 | +// Check to see if the item exists in the cache without updating access |
| 90 | +SLRUCache.prototype.has = function (key) { |
| 91 | + return this.protectedLRU.nodeHash.get(key) || this.probationaryLRU.nodeHash.get(key); |
| 92 | +} |
| 93 | + |
| 94 | +// Adds a node to the protectedLRU |
| 95 | +SLRUCache.prototype.putAndDemote = function (key, value) { |
| 96 | + // if adding an item to the protectedLRU results in ejection, demote ejected node |
| 97 | + const demoted = this.protectedLRU.put(key, value); |
| 98 | + if (demoted) this.probationaryLRU.put(demoted.key, demoted.value); |
| 99 | +} |
| 100 | + |
| 101 | +class Node { |
| 102 | + constructor (key, value) { |
| 103 | + this.key = key; |
| 104 | + this.value = value; |
| 105 | + this.next = this.prev = null; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function LRUCache(capacity) { |
| 110 | + this.capacity = capacity; |
| 111 | + this.currentSize = 0; |
| 112 | + // node hash for cache lookup and storage |
| 113 | + this.nodeHash = new Map(); |
| 114 | + |
| 115 | + // doubly-linked list to keep track of recency and handle eviction |
| 116 | + this.head = new Node('head', null); |
| 117 | + this.tail = new Node('tail', null); |
| 118 | + this.head.next = this.tail; |
| 119 | + this.tail.prev = this.head; |
| 120 | +} |
| 121 | + |
| 122 | +LRUCache.prototype.removeNode = function (node) { |
| 123 | + const prev = node.prev; |
| 124 | + const next = node.next; |
| 125 | + prev.next = next; |
| 126 | + next.prev = prev; |
| 127 | +}; |
| 128 | + |
| 129 | + |
| 130 | +LRUCache.prototype.addNode = function (node) { |
| 131 | + const tempTail = this.tail.prev; |
| 132 | + tempTail.next = node; |
| 133 | + |
| 134 | + this.tail.prev = node; |
| 135 | + node.next = this.tail; |
| 136 | + node.prev = tempTail; |
| 137 | +} |
| 138 | + |
| 139 | +// Like get, but doesn't update anything |
| 140 | +LRUCache.prototype.peek = function(key) { |
| 141 | + const node = this.nodeHash.get(key); |
| 142 | + if (!node) return null; |
| 143 | + return node.value; |
| 144 | +} |
| 145 | + |
| 146 | +// Like removeNode, but takes key and deletes from hash |
| 147 | +LRUCache.prototype.delete = function (key) { |
| 148 | + const node = this.nodeHash.get(key); |
| 149 | + const prev = node.prev; |
| 150 | + const next = node.next; |
| 151 | + prev.next = next; |
| 152 | + next.prev = prev; |
| 153 | + this.nodeHash.delete(key); |
| 154 | +} |
| 155 | + |
| 156 | +LRUCache.prototype.get = function(key) { |
| 157 | + const node = this.nodeHash.get(key); |
| 158 | + |
| 159 | + // check if node does not exist in nodeHash obj |
| 160 | + if (!node) return null; |
| 161 | + // update position to most recent in list |
| 162 | + this.removeNode(node); |
| 163 | + this.addNode(node); |
| 164 | + return node.value; |
| 165 | +} |
| 166 | + |
| 167 | +// used by wTinyLFU to get SLRU eviction candidates for TinyLFU decision |
| 168 | +LRUCache.prototype.getCandidate = function () { |
| 169 | + const tempHead = this.head.next; |
| 170 | + this.removeNode(tempHead); |
| 171 | + this.nodeHash.delete(tempHead.key); |
| 172 | + return {key: tempHead.key, value: tempHead.value}; |
| 173 | +} |
| 174 | + |
| 175 | +LRUCache.prototype.put = function (key, value) { |
| 176 | + // create a new node |
| 177 | + const newNode = new Node(key, value); |
| 178 | + |
| 179 | + // remove node from old position |
| 180 | + const node = this.nodeHash.get(key); |
| 181 | + if (node) this.removeNode(node); |
| 182 | + |
| 183 | + // add new node to tail |
| 184 | + this.addNode(newNode); |
| 185 | + this.nodeHash.set(key, newNode); |
| 186 | + |
| 187 | + // check capacity - if over capacity, remove and reassign head node |
| 188 | + if (this.nodeHash.size > this.capacity){ |
| 189 | + const tempHead = this.head.next; |
| 190 | + this.removeNode(tempHead); |
| 191 | + this.nodeHash.delete(tempHead.key); |
| 192 | + // return tempHead for use in w-TinyLFU's SLRU cache |
| 193 | + return {key: tempHead.key, value: tempHead.value}; |
| 194 | + } |
| 195 | +} |
| 196 | + |
0 commit comments