File size: 2,362 Bytes
1e92f2d |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
export class LRUCache<T> {
private cache: Map<string, T>
private sizes: Map<string, number>
private totalSize: number
private maxSize: number
private calculateSize: (value: T) => number
constructor(maxSize: number, calculateSize?: (value: T) => number) {
this.cache = new Map()
this.sizes = new Map()
this.totalSize = 0
this.maxSize = maxSize
this.calculateSize = calculateSize || (() => 1)
}
set(key?: string | null, value?: T): void {
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?: string | null): boolean {
if (!key) return false
this.touch(key)
return Boolean(this.cache.get(key))
}
get(key?: string | null): T | undefined {
if (!key) return
const value = this.cache.get(key)
if (value === undefined) {
return undefined
}
this.touch(key)
return value
}
private touch(key: string): void {
const value = this.cache.get(key)
if (value !== undefined) {
this.cache.delete(key)
this.cache.set(key, value)
this.evictIfNecessary()
}
}
private evictIfNecessary(): void {
while (this.totalSize > this.maxSize && this.cache.size > 0) {
this.evictLeastRecentlyUsed()
}
}
private evictLeastRecentlyUsed(): void {
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: string): void {
if (this.cache.has(key)) {
this.totalSize -= this.sizes.get(key) || 0
this.cache.delete(key)
this.sizes.delete(key)
}
}
clear(): void {
this.cache.clear()
this.sizes.clear()
this.totalSize = 0
}
get size(): number {
return this.cache.size
}
get currentSize(): number {
return this.totalSize
}
}
|