15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
114
115
116
117
118
119
120
121
122
123
'use strict';
 
class QuickLRU {
    constructor(options = {}) {
        if (!(options.maxSize && options.maxSize > 0)) {
            throw new TypeError('`maxSize` must be a number greater than 0');
        }
 
        this.maxSize = options.maxSize;
        this.onEviction = options.onEviction;
        this.cache = new Map();
        this.oldCache = new Map();
        this._size = 0;
    }
 
    _set(key, value) {
        this.cache.set(key, value);
        this._size++;
 
        if (this._size >= this.maxSize) {
            this._size = 0;
 
            if (typeof this.onEviction === 'function') {
                for (const [key, value] of this.oldCache.entries()) {
                    this.onEviction(key, value);
                }
            }
 
            this.oldCache = this.cache;
            this.cache = new Map();
        }
    }
 
    get(key) {
        if (this.cache.has(key)) {
            return this.cache.get(key);
        }
 
        if (this.oldCache.has(key)) {
            const value = this.oldCache.get(key);
            this.oldCache.delete(key);
            this._set(key, value);
            return value;
        }
    }
 
    set(key, value) {
        if (this.cache.has(key)) {
            this.cache.set(key, value);
        } else {
            this._set(key, value);
        }
 
        return this;
    }
 
    has(key) {
        return this.cache.has(key) || this.oldCache.has(key);
    }
 
    peek(key) {
        if (this.cache.has(key)) {
            return this.cache.get(key);
        }
 
        if (this.oldCache.has(key)) {
            return this.oldCache.get(key);
        }
    }
 
    delete(key) {
        const deleted = this.cache.delete(key);
        if (deleted) {
            this._size--;
        }
 
        return this.oldCache.delete(key) || deleted;
    }
 
    clear() {
        this.cache.clear();
        this.oldCache.clear();
        this._size = 0;
    }
 
    * keys() {
        for (const [key] of this) {
            yield key;
        }
    }
 
    * values() {
        for (const [, value] of this) {
            yield value;
        }
    }
 
    * [Symbol.iterator]() {
        for (const item of this.cache) {
            yield item;
        }
 
        for (const item of this.oldCache) {
            const [key] = item;
            if (!this.cache.has(key)) {
                yield item;
            }
        }
    }
 
    get size() {
        let oldCacheSize = 0;
        for (const key of this.oldCache.keys()) {
            if (!this.cache.has(key)) {
                oldCacheSize++;
            }
        }
 
        return Math.min(this._size + oldCacheSize, this.maxSize);
    }
}
 
module.exports = QuickLRU;