Update
This commit is contained in:
168
node_modules/lru.min/lib/index.mjs
generated
vendored
168
node_modules/lru.min/lib/index.mjs
generated
vendored
@@ -12,31 +12,64 @@ const createLRU = (options) => {
|
||||
const valList = new Array(max).fill(void 0);
|
||||
const next = new Array(max).fill(0);
|
||||
const prev = new Array(max).fill(0);
|
||||
const setTail = (index, type) => {
|
||||
if (index === tail) return;
|
||||
const nextIndex = next[index];
|
||||
const prevIndex = prev[index];
|
||||
if (index === head) head = nextIndex;
|
||||
else if (type === "get" || prevIndex !== 0) next[prevIndex] = nextIndex;
|
||||
if (nextIndex !== 0) prev[nextIndex] = prevIndex;
|
||||
const linkTail = (index) => {
|
||||
next[tail] = index;
|
||||
prev[index] = tail;
|
||||
next[index] = 0;
|
||||
tail = index;
|
||||
};
|
||||
const _evict = () => {
|
||||
const evictHead = head;
|
||||
const key = keyList[evictHead];
|
||||
onEviction == null ? void 0 : onEviction(key, valList[evictHead]);
|
||||
keyMap.delete(key);
|
||||
keyList[evictHead] = void 0;
|
||||
valList[evictHead] = void 0;
|
||||
head = next[evictHead];
|
||||
if (head !== 0) prev[head] = 0;
|
||||
size--;
|
||||
if (size === 0) head = tail = 0;
|
||||
free.push(evictHead);
|
||||
return evictHead;
|
||||
const moveToTail = (index) => {
|
||||
if (index === tail) return;
|
||||
const nextIndex = next[index];
|
||||
const prevIndex = prev[index];
|
||||
if (index === head) head = nextIndex;
|
||||
else next[prevIndex] = nextIndex;
|
||||
prev[nextIndex] = prevIndex;
|
||||
linkTail(index);
|
||||
};
|
||||
const _shrink = (newMax) => {
|
||||
let current = tail;
|
||||
const preserve = Math.min(size, newMax);
|
||||
const remove = size - preserve;
|
||||
const newKeyList = new Array(preserve);
|
||||
const newValList = new Array(preserve);
|
||||
for (let i = 0; i < remove; i++) {
|
||||
const key = keyList[head];
|
||||
onEviction == null ? void 0 : onEviction(key, valList[head]);
|
||||
keyMap.delete(key);
|
||||
head = next[head];
|
||||
}
|
||||
for (let i = preserve - 1; i >= 0; i--) {
|
||||
newKeyList[i] = keyList[current];
|
||||
newValList[i] = valList[current];
|
||||
keyMap.set(keyList[current], i);
|
||||
current = prev[current];
|
||||
}
|
||||
head = 0;
|
||||
tail = preserve - 1;
|
||||
size = preserve;
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
for (let i = 0; i < preserve; i++) {
|
||||
keyList[i] = newKeyList[i];
|
||||
valList[i] = newValList[i];
|
||||
next[i] = i + 1;
|
||||
prev[i] = i - 1;
|
||||
}
|
||||
free = [];
|
||||
for (let i = preserve; i < newMax; i++) free.push(i);
|
||||
};
|
||||
const _grow = (newMax) => {
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
keyList.fill(void 0, max);
|
||||
valList.fill(void 0, max);
|
||||
next.fill(0, max);
|
||||
prev.fill(0, max);
|
||||
};
|
||||
return {
|
||||
/** Adds a key-value pair to the cache. Updates the value if the key already exists. */
|
||||
@@ -44,20 +77,33 @@ const createLRU = (options) => {
|
||||
if (key === void 0) return;
|
||||
let index = keyMap.get(key);
|
||||
if (index === void 0) {
|
||||
index = size === max ? _evict() : free.length > 0 ? free.pop() : size;
|
||||
if (size === max) {
|
||||
index = head;
|
||||
const evictKey = keyList[index];
|
||||
onEviction == null ? void 0 : onEviction(evictKey, valList[index]);
|
||||
keyMap.delete(evictKey);
|
||||
head = next[index];
|
||||
prev[head] = 0;
|
||||
} else {
|
||||
index = free.length > 0 ? free.pop() : size;
|
||||
size++;
|
||||
}
|
||||
keyMap.set(key, index);
|
||||
keyList[index] = key;
|
||||
size++;
|
||||
} else onEviction == null ? void 0 : onEviction(key, valList[index]);
|
||||
valList[index] = value;
|
||||
if (size === 1) head = tail = index;
|
||||
else setTail(index, "set");
|
||||
valList[index] = value;
|
||||
if (size === 1) head = tail = index;
|
||||
else linkTail(index);
|
||||
} else {
|
||||
onEviction == null ? void 0 : onEviction(key, valList[index]);
|
||||
valList[index] = value;
|
||||
moveToTail(index);
|
||||
}
|
||||
},
|
||||
/** Retrieves the value for a given key and moves the key to the most recent position. */
|
||||
get(key) {
|
||||
const index = keyMap.get(key);
|
||||
if (index === void 0) return;
|
||||
if (index !== tail) setTail(index, "get");
|
||||
if (index !== tail) moveToTail(index);
|
||||
return valList[index];
|
||||
},
|
||||
/** Retrieves the value for a given key without changing its position. */
|
||||
@@ -112,10 +158,10 @@ const createLRU = (options) => {
|
||||
valList[index] = void 0;
|
||||
const prevIndex = prev[index];
|
||||
const nextIndex = next[index];
|
||||
if (prevIndex !== 0) next[prevIndex] = nextIndex;
|
||||
if (nextIndex !== 0) prev[nextIndex] = prevIndex;
|
||||
if (index === head) head = nextIndex;
|
||||
else next[prevIndex] = nextIndex;
|
||||
if (index === tail) tail = prevIndex;
|
||||
else prev[nextIndex] = prevIndex;
|
||||
size--;
|
||||
return true;
|
||||
},
|
||||
@@ -123,16 +169,28 @@ const createLRU = (options) => {
|
||||
evict: (number) => {
|
||||
let toPrune = Math.min(number, size);
|
||||
while (toPrune > 0) {
|
||||
_evict();
|
||||
const evictHead = head;
|
||||
const key = keyList[evictHead];
|
||||
onEviction == null ? void 0 : onEviction(key, valList[evictHead]);
|
||||
keyMap.delete(key);
|
||||
keyList[evictHead] = void 0;
|
||||
valList[evictHead] = void 0;
|
||||
head = next[evictHead];
|
||||
prev[head] = 0;
|
||||
size--;
|
||||
free.push(evictHead);
|
||||
toPrune--;
|
||||
}
|
||||
if (size === 0) head = tail = 0;
|
||||
},
|
||||
/** Clears all key-value pairs from the cache. */
|
||||
clear() {
|
||||
if (typeof onEviction === "function") {
|
||||
const iterator = keyMap.values();
|
||||
for (let result = iterator.next(); !result.done; result = iterator.next())
|
||||
onEviction(keyList[result.value], valList[result.value]);
|
||||
if (onEviction) {
|
||||
let current = head;
|
||||
for (let i = 0; i < size; i++) {
|
||||
onEviction(keyList[current], valList[current]);
|
||||
current = next[current];
|
||||
}
|
||||
}
|
||||
keyMap.clear();
|
||||
keyList.fill(void 0);
|
||||
@@ -146,46 +204,8 @@ const createLRU = (options) => {
|
||||
if (!(Number.isInteger(newMax) && newMax > 0))
|
||||
throw new TypeError("`max` must be a positive integer");
|
||||
if (newMax === max) return;
|
||||
if (newMax < max) {
|
||||
let current = tail;
|
||||
const preserve = Math.min(size, newMax);
|
||||
const remove = size - preserve;
|
||||
const newKeyList = new Array(newMax);
|
||||
const newValList = new Array(newMax);
|
||||
const newNext = new Array(newMax);
|
||||
const newPrev = new Array(newMax);
|
||||
for (let i = 1; i <= remove; i++)
|
||||
onEviction == null ? void 0 : onEviction(keyList[i], valList[i]);
|
||||
for (let i = preserve - 1; i >= 0; i--) {
|
||||
newKeyList[i] = keyList[current];
|
||||
newValList[i] = valList[current];
|
||||
newNext[i] = i + 1;
|
||||
newPrev[i] = i - 1;
|
||||
keyMap.set(newKeyList[i], i);
|
||||
current = prev[current];
|
||||
}
|
||||
head = 0;
|
||||
tail = preserve - 1;
|
||||
size = preserve;
|
||||
keyList.length = newMax;
|
||||
valList.length = newMax;
|
||||
next.length = newMax;
|
||||
prev.length = newMax;
|
||||
for (let i = 0; i < preserve; i++) {
|
||||
keyList[i] = newKeyList[i];
|
||||
valList[i] = newValList[i];
|
||||
next[i] = newNext[i];
|
||||
prev[i] = newPrev[i];
|
||||
}
|
||||
free = [];
|
||||
for (let i = preserve; i < newMax; i++) free.push(i);
|
||||
} else {
|
||||
const fill = newMax - max;
|
||||
keyList.push(...new Array(fill).fill(void 0));
|
||||
valList.push(...new Array(fill).fill(void 0));
|
||||
next.push(...new Array(fill).fill(0));
|
||||
prev.push(...new Array(fill).fill(0));
|
||||
}
|
||||
if (newMax < max) _shrink(newMax);
|
||||
else _grow(newMax);
|
||||
max = newMax;
|
||||
},
|
||||
/** Returns the maximum number of items that can be stored in the cache. */
|
||||
|
||||
Reference in New Issue
Block a user