100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "LRUCache", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return LRUCache;
|
|
}
|
|
});
|
|
class LRUCache {
|
|
constructor(maxSize, calculateSize){
|
|
this.cache = new Map();
|
|
this.sizes = new Map();
|
|
this.totalSize = 0;
|
|
this.maxSize = maxSize;
|
|
this.calculateSize = calculateSize || (()=>1);
|
|
}
|
|
set(key, value) {
|
|
if (!key || !value) return;
|
|
const size = this.calculateSize(value);
|
|
if (size > this.maxSize) {
|
|
console.warn('Single item size exceeds maxSize');
|
|
return;
|
|
}
|
|
if (this.cache.has(key)) {
|
|
this.totalSize -= this.sizes.get(key) || 0;
|
|
}
|
|
this.cache.set(key, value);
|
|
this.sizes.set(key, size);
|
|
this.totalSize += size;
|
|
this.touch(key);
|
|
}
|
|
has(key) {
|
|
if (!key) return false;
|
|
this.touch(key);
|
|
return Boolean(this.cache.get(key));
|
|
}
|
|
get(key) {
|
|
if (!key) return;
|
|
const value = this.cache.get(key);
|
|
if (value === undefined) {
|
|
return undefined;
|
|
}
|
|
this.touch(key);
|
|
return value;
|
|
}
|
|
touch(key) {
|
|
const value = this.cache.get(key);
|
|
if (value !== undefined) {
|
|
this.cache.delete(key);
|
|
this.cache.set(key, value);
|
|
this.evictIfNecessary();
|
|
}
|
|
}
|
|
evictIfNecessary() {
|
|
while(this.totalSize > this.maxSize && this.cache.size > 0){
|
|
this.evictLeastRecentlyUsed();
|
|
}
|
|
}
|
|
evictLeastRecentlyUsed() {
|
|
const lruKey = this.cache.keys().next().value;
|
|
if (lruKey !== undefined) {
|
|
const lruSize = this.sizes.get(lruKey) || 0;
|
|
this.totalSize -= lruSize;
|
|
this.cache.delete(lruKey);
|
|
this.sizes.delete(lruKey);
|
|
}
|
|
}
|
|
reset() {
|
|
this.cache.clear();
|
|
this.sizes.clear();
|
|
this.totalSize = 0;
|
|
}
|
|
keys() {
|
|
return [
|
|
...this.cache.keys()
|
|
];
|
|
}
|
|
remove(key) {
|
|
if (this.cache.has(key)) {
|
|
this.totalSize -= this.sizes.get(key) || 0;
|
|
this.cache.delete(key);
|
|
this.sizes.delete(key);
|
|
}
|
|
}
|
|
clear() {
|
|
this.cache.clear();
|
|
this.sizes.clear();
|
|
this.totalSize = 0;
|
|
}
|
|
get size() {
|
|
return this.cache.size;
|
|
}
|
|
get currentSize() {
|
|
return this.totalSize;
|
|
}
|
|
}
|
|
|
|
//# sourceMappingURL=lru-cache.js.map
|