@isaacs/ttlcache 2.0.1 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/commonjs/index.d.ts +27 -3
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +31 -4
- package/dist/commonjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +27 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +31 -4
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ Custom size calculation is not supported. Max capacity is simply the count
|
|
|
29
29
|
of items in the cache.
|
|
30
30
|
|
|
31
31
|
```js
|
|
32
|
-
|
|
32
|
+
import { TTLCache } from '@isaacs/ttlcache')
|
|
33
33
|
const cache = new TTLCache({ max: 10000, ttl: 1000 })
|
|
34
34
|
|
|
35
35
|
// set some value
|
|
@@ -59,9 +59,9 @@ timer in this way will of course prevent anything from expiring.
|
|
|
59
59
|
|
|
60
60
|
## API
|
|
61
61
|
|
|
62
|
-
### `const TTLCache = require('@isaacs/ttlcache')` or `import TTLCache from '@isaacs/ttlcache'`
|
|
62
|
+
### `const { TTLCache } = require('@isaacs/ttlcache')` or `import TTLCache from '@isaacs/ttlcache'`
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
The `TTLCache` class is a named export.
|
|
65
65
|
|
|
66
66
|
### `new TTLCache({ ttl, max = Infinty, updateAgeOnGet = false, checkAgeOnGet = false, noUpdateTTL = false, noDisposeOnSet = false })`
|
|
67
67
|
|
|
@@ -208,7 +208,7 @@ this on the constructor options.
|
|
|
208
208
|
|
|
209
209
|
**Internal**
|
|
210
210
|
|
|
211
|
-
Called when an with a ttl is added. This ensures that only one timer
|
|
211
|
+
Called when an item with a ttl is added. This ensures that only one timer
|
|
212
212
|
is setup at once. Called automatically.
|
|
213
213
|
|
|
214
214
|
## Algorithm
|
package/dist/commonjs/index.d.ts
CHANGED
|
@@ -1,16 +1,37 @@
|
|
|
1
1
|
export type DisposeReason = 'set' | 'delete' | 'stale' | 'evict';
|
|
2
2
|
export type DisposeFunction<K, V> = (val: V, key: K, reason: DisposeReason) => unknown;
|
|
3
|
+
export type TimeInMilliseconds = number;
|
|
3
4
|
export type TTLCacheOptions<K, V> = {
|
|
5
|
+
/** the maximum number of items to store in the cache */
|
|
4
6
|
max?: number;
|
|
5
|
-
|
|
7
|
+
/** time in ms to store items, must be positive number */
|
|
8
|
+
ttl?: TimeInMilliseconds;
|
|
9
|
+
/** Update the remaining TTL when getting items */
|
|
6
10
|
updateAgeOnGet?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Check the remaining age when getting items. Note that if this is not
|
|
13
|
+
* set, then it's possible to get expired items that have not yet been
|
|
14
|
+
* preemptively purged.
|
|
15
|
+
*/
|
|
7
16
|
checkAgeOnGet?: boolean;
|
|
17
|
+
/** Update the remaining TTL when checking for an item's presence */
|
|
18
|
+
updateAgeOnHas?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Check the remaining age when checking for an items presence. Note that if
|
|
21
|
+
* this is not set, then expired items will return `true` if they have not yet
|
|
22
|
+
* been preemptively purged.
|
|
23
|
+
*/
|
|
24
|
+
checkAgeOnHas?: boolean;
|
|
25
|
+
/** do not update the TTL when setting a new value for an existing key */
|
|
8
26
|
noUpdateTTL?: boolean;
|
|
27
|
+
/** A function to call when an item is removed from the cache */
|
|
9
28
|
dispose?: DisposeFunction<K, V>;
|
|
29
|
+
/** Do not call `dispose` when setting an existing key to a new value */
|
|
10
30
|
noDisposeOnSet?: boolean;
|
|
11
31
|
};
|
|
12
32
|
export type SetOptions<K, V> = Pick<TTLCacheOptions<K, V>, 'ttl' | 'noUpdateTTL' | 'noDisposeOnSet'>;
|
|
13
33
|
export type GetOptions<K, V> = Pick<TTLCacheOptions<K, V>, 'updateAgeOnGet' | 'ttl' | 'checkAgeOnGet'>;
|
|
34
|
+
export type HasOptions<K, V> = Pick<TTLCacheOptions<K, V>, 'updateAgeOnHas' | 'ttl' | 'checkAgeOnHas'>;
|
|
14
35
|
export declare class TTLCache<K = unknown, V = unknown> {
|
|
15
36
|
expirations: Record<number, K[]>;
|
|
16
37
|
data: Map<K, V>;
|
|
@@ -18,20 +39,23 @@ export declare class TTLCache<K = unknown, V = unknown> {
|
|
|
18
39
|
ttl?: number;
|
|
19
40
|
max: number;
|
|
20
41
|
updateAgeOnGet: boolean;
|
|
42
|
+
updateAgeOnHas: boolean;
|
|
21
43
|
noUpdateTTL: boolean;
|
|
22
44
|
noDisposeOnSet: boolean;
|
|
23
45
|
checkAgeOnGet: boolean;
|
|
46
|
+
checkAgeOnHas: boolean;
|
|
24
47
|
dispose: DisposeFunction<K, V>;
|
|
25
48
|
timer?: ReturnType<typeof setTimeout>;
|
|
26
49
|
timerExpiration?: number;
|
|
27
|
-
|
|
50
|
+
immortalKeys: Set<K>;
|
|
51
|
+
constructor({ max, ttl, updateAgeOnGet, checkAgeOnGet, updateAgeOnHas, checkAgeOnHas, noUpdateTTL, dispose, noDisposeOnSet, }?: TTLCacheOptions<K, V>);
|
|
28
52
|
setTimer(expiration: number, ttl: number): void;
|
|
29
53
|
cancelTimer(): void;
|
|
30
54
|
cancelTimers(): void;
|
|
31
55
|
clear(): void;
|
|
32
56
|
setTTL(key: K, ttl?: number | undefined): void;
|
|
33
57
|
set(key: K, val: V, { ttl, noUpdateTTL, noDisposeOnSet, }?: SetOptions<K, V>): this;
|
|
34
|
-
has(key: K): boolean;
|
|
58
|
+
has(key: K, { checkAgeOnHas, ttl, updateAgeOnHas, }?: HasOptions<K, V>): boolean;
|
|
35
59
|
getRemainingTTL(key: K): number;
|
|
36
60
|
get(key: K, { updateAgeOnGet, ttl, checkAgeOnGet, }?: GetOptions<K, V>): V | undefined;
|
|
37
61
|
delete(key: K): boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"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AC,CAAM;IA8B7B,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAkCxC,WAAW;IASX,YAAY;IAUZ,KAAK;IAaL,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,qBAAW;IA2B7B,GAAG,CACD,GAAG,EAAE,CAAC,EACN,GAAG,EAAE,CAAC,EACN,EACE,GAAc,EACd,WAA8B,EAC9B,cAAoC,GACrC,GAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAM;IA6B1B,GAAG,CACD,GAAG,EAAE,CAAC,EACN,EACE,aAAkC,EAClC,GAAc,EACd,cAAoC,GACrC,GAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAM;IAe1B,eAAe,CAAC,GAAG,EAAE,CAAC;IAStB,GAAG,CACD,GAAG,EAAE,CAAC,EACN,EACE,cAAoC,EACpC,GAAc,EACd,aAAkC,GACnC,GAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAM;IAa1B,MAAM,CAAC,GAAG,EAAE,CAAC;IAwBb,eAAe;IA8Bf,IAAI,IAAI,WAEP;IAED,UAAU;IA4BT,OAAO;IAUP,IAAI;IAUJ,MAAM;IAUP,CAAC,MAAM,CAAC,QAAQ,CAAC;CAGlB"}
|
package/dist/commonjs/index.js
CHANGED
|
@@ -22,13 +22,16 @@ class TTLCache {
|
|
|
22
22
|
ttl;
|
|
23
23
|
max;
|
|
24
24
|
updateAgeOnGet;
|
|
25
|
+
updateAgeOnHas;
|
|
25
26
|
noUpdateTTL;
|
|
26
27
|
noDisposeOnSet;
|
|
27
28
|
checkAgeOnGet;
|
|
29
|
+
checkAgeOnHas;
|
|
28
30
|
dispose;
|
|
29
31
|
timer;
|
|
30
32
|
timerExpiration;
|
|
31
|
-
|
|
33
|
+
immortalKeys = new Set();
|
|
34
|
+
constructor({ max = Infinity, ttl, updateAgeOnGet = false, checkAgeOnGet = false, updateAgeOnHas = false, checkAgeOnHas = false, noUpdateTTL = false, dispose, noDisposeOnSet = false, } = {}) {
|
|
32
35
|
if (ttl !== undefined && !isPosIntOrInf(ttl)) {
|
|
33
36
|
throw new TypeError('ttl must be positive integer or Infinity if set');
|
|
34
37
|
}
|
|
@@ -39,6 +42,8 @@ class TTLCache {
|
|
|
39
42
|
this.max = max;
|
|
40
43
|
this.updateAgeOnGet = !!updateAgeOnGet;
|
|
41
44
|
this.checkAgeOnGet = !!checkAgeOnGet;
|
|
45
|
+
this.updateAgeOnHas = !!updateAgeOnHas;
|
|
46
|
+
this.checkAgeOnHas = !!checkAgeOnHas;
|
|
42
47
|
this.noUpdateTTL = !!noUpdateTTL;
|
|
43
48
|
this.noDisposeOnSet = !!noDisposeOnSet;
|
|
44
49
|
if (dispose !== undefined) {
|
|
@@ -69,7 +74,7 @@ class TTLCache {
|
|
|
69
74
|
this.setTimer(e, e - now());
|
|
70
75
|
break;
|
|
71
76
|
}
|
|
72
|
-
}, ttl);
|
|
77
|
+
}, Math.max(0, ttl));
|
|
73
78
|
/* c8 ignore start - affordance for non-node envs */
|
|
74
79
|
if (t.unref)
|
|
75
80
|
t.unref();
|
|
@@ -119,6 +124,7 @@ class TTLCache {
|
|
|
119
124
|
}
|
|
120
125
|
}
|
|
121
126
|
if (ttl && ttl !== Infinity) {
|
|
127
|
+
this.immortalKeys.delete(key);
|
|
122
128
|
const expiration = Math.floor(now() + ttl);
|
|
123
129
|
this.expirationMap.set(key, expiration);
|
|
124
130
|
if (!this.expirations[expiration]) {
|
|
@@ -128,6 +134,7 @@ class TTLCache {
|
|
|
128
134
|
this.expirations[expiration].push(key);
|
|
129
135
|
}
|
|
130
136
|
else {
|
|
137
|
+
this.immortalKeys.add(key);
|
|
131
138
|
this.expirationMap.set(key, Infinity);
|
|
132
139
|
}
|
|
133
140
|
}
|
|
@@ -157,8 +164,18 @@ class TTLCache {
|
|
|
157
164
|
}
|
|
158
165
|
return this;
|
|
159
166
|
}
|
|
160
|
-
has(key) {
|
|
161
|
-
|
|
167
|
+
has(key, { checkAgeOnHas = this.checkAgeOnHas, ttl = this.ttl, updateAgeOnHas = this.updateAgeOnHas, } = {}) {
|
|
168
|
+
if (this.data.has(key)) {
|
|
169
|
+
if (checkAgeOnHas && this.getRemainingTTL(key) === 0) {
|
|
170
|
+
this.delete(key);
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (updateAgeOnHas) {
|
|
174
|
+
this.setTTL(key, ttl);
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
162
179
|
}
|
|
163
180
|
getRemainingTTL(key) {
|
|
164
181
|
const expiration = this.expirationMap.get(key);
|
|
@@ -185,6 +202,7 @@ class TTLCache {
|
|
|
185
202
|
const value = this.data.get(key);
|
|
186
203
|
this.data.delete(key);
|
|
187
204
|
this.expirationMap.delete(key);
|
|
205
|
+
this.immortalKeys.delete(key);
|
|
188
206
|
const exp = this.expirations[current];
|
|
189
207
|
if (exp) {
|
|
190
208
|
if (exp.length <= 1) {
|
|
@@ -267,6 +285,9 @@ class TTLCache {
|
|
|
267
285
|
yield [key, this.data.get(key)];
|
|
268
286
|
}
|
|
269
287
|
}
|
|
288
|
+
for (const key of this.immortalKeys) {
|
|
289
|
+
yield [key, this.data.get(key)];
|
|
290
|
+
}
|
|
270
291
|
}
|
|
271
292
|
*keys() {
|
|
272
293
|
for (const exp in this.expirations) {
|
|
@@ -274,6 +295,9 @@ class TTLCache {
|
|
|
274
295
|
yield key;
|
|
275
296
|
}
|
|
276
297
|
}
|
|
298
|
+
for (const key of this.immortalKeys) {
|
|
299
|
+
yield key;
|
|
300
|
+
}
|
|
277
301
|
}
|
|
278
302
|
*values() {
|
|
279
303
|
for (const exp in this.expirations) {
|
|
@@ -281,6 +305,9 @@ class TTLCache {
|
|
|
281
305
|
yield this.data.get(key);
|
|
282
306
|
}
|
|
283
307
|
}
|
|
308
|
+
for (const key of this.immortalKeys) {
|
|
309
|
+
yield this.data.get(key);
|
|
310
|
+
}
|
|
284
311
|
}
|
|
285
312
|
[Symbol.iterator]() {
|
|
286
313
|
return this.entries();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,8DAA8D;AAC9D,kDAAkD;AAClD,+DAA+D;AAC/D,sCAAsC;;;AAEtC,qBAAqB;AACrB,MAAM,IAAI,GACR,OAAO,WAAW,KAAK,QAAQ;IAC/B,WAAW;IACX,OAAO,WAAW,CAAC,GAAG,KAAK,UAAU;IACnC,CAAC,CAAC,WAAW;IACb,CAAC,CAAC,IAAI,CAAA;AACV,oBAAoB;AAEpB,MAAM,GAAG,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;AAC5B,MAAM,QAAQ,GAAG,CAAC,CAAM,EAAe,EAAE,CACvC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAA;AACpD,MAAM,aAAa,GAAG,CAAC,CAAM,EAAe,EAAE,CAC5C,CAAC,KAAK,QAAQ,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAA;AA6B/B,MAAa,QAAQ;IACnB,WAAW,GAAwB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACtD,IAAI,GAAG,IAAI,GAAG,EAAQ,CAAA;IACtB,aAAa,GAAG,IAAI,GAAG,EAAa,CAAA;IACpC,GAAG,CAAS;IACZ,GAAG,CAAQ;IACX,cAAc,CAAS;IACvB,WAAW,CAAS;IACpB,cAAc,CAAS;IACvB,aAAa,CAAS;IACtB,OAAO,CAAuB;IAC9B,KAAK,CAAgC;IACrC,eAAe,CAAS;IAExB,YAAY,EACV,GAAG,GAAG,QAAQ,EACd,GAAG,EACH,cAAc,GAAG,KAAK,EACtB,aAAa,GAAG,KAAK,EACrB,WAAW,GAAG,KAAK,EACnB,OAAO,EACP,cAA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AE,CAAC;gBAC/C,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAW,CAAA;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IACD,CAAC,IAAI;QACH,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACnC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAQ,EAAE,CAAC;gBAC/C,MAAM,GAAG,CAAA;YACX,CAAC;QACH,CAAC;IACH,CAAC;IACD,CAAC,MAAM;QACL,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACnC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAQ,EAAE,CAAC;gBAC/C,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAM,CAAA;YAC/B,CAAC;QACH,CAAC;IACH,CAAC;IACD,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;CACF;AA3TD,4BA2TC","sourcesContent":["// A simple TTL cache with max capacity option, ms resolution,\n// autopurge, and reasonably optimized performance\n// Relies on the fact that integer Object keys are kept sorted,\n// and managed very efficiently by V8.\n\n/* c8 ignore start */\nconst perf =\n typeof performance === 'object' &&\n performance &&\n typeof performance.now === 'function'\n ? performance\n : Date\n/* c8 ignore stop */\n\nconst now = () => perf.now()\nconst isPosInt = (n: any): n is number =>\n !!n && n === Math.floor(n) && n > 0 && isFinite(n)\nconst isPosIntOrInf = (n: any): n is number =>\n n === Infinity || isPosInt(n)\n\nexport type DisposeReason = 'set' | 'delete' | 'stale' | 'evict'\n\nexport type DisposeFunction<K, V> = (\n val: V,\n key: K,\n reason: DisposeReason,\n) => unknown\n\nexport type TTLCacheOptions<K, V> = {\n max?: number\n ttl?: number\n updateAgeOnGet?: boolean\n checkAgeOnGet?: boolean\n noUpdateTTL?: boolean\n dispose?: DisposeFunction<K, V>\n noDisposeOnSet?: boolean\n}\n\nexport type SetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'ttl' | 'noUpdateTTL' | 'noDisposeOnSet'\n>\nexport type GetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'updateAgeOnGet' | 'ttl' | 'checkAgeOnGet'\n>\n\nexport class TTLCache<K = unknown, V = unknown> {\n expirations: Record<number, K[]> = Object.create(null)\n data = new Map<K, V>()\n expirationMap = new Map<K, number>()\n ttl?: number\n max: number\n updateAgeOnGet: boolean\n noUpdateTTL: boolean\n noDisposeOnSet: boolean\n checkAgeOnGet: boolean\n dispose: DisposeFunction<K, V>\n timer?: ReturnType<typeof setTimeout>\n timerExpiration?: number\n\n constructor({\n max = Infinity,\n ttl,\n updateAgeOnGet = false,\n checkAgeOnGet = false,\n noUpdateTTL = false,\n dispose,\n noDisposeOnSet = false,\n }: TTLCacheOptions<K, V> = {}) {\n if (ttl !== undefined && !isPosIntOrInf(ttl)) {\n throw new TypeError(\n 'ttl must be positive integer or Infinity if set',\n )\n }\n if (!isPosIntOrInf(max)) {\n throw new TypeError('max must be positive integer or Infinity')\n }\n this.ttl = ttl\n this.max = max\n this.updateAgeOnGet = !!updateAgeOnGet\n this.checkAgeOnGet = !!checkAgeOnGet\n this.noUpdateTTL = !!noUpdateTTL\n this.noDisposeOnSet = !!noDisposeOnSet\n if (dispose !== undefined) {\n if (typeof dispose !== 'function') {\n throw new TypeError('dispose must be function if set')\n }\n this.dispose = dispose\n } else {\n this.dispose = (_, __, ___) => {}\n }\n\n this.timer = undefined\n this.timerExpiration = undefined\n }\n\n setTimer(expiration: number, ttl: number) {\n if (this.timerExpiration && this.timerExpiration < expiration) {\n return\n }\n\n if (this.timer) {\n clearTimeout(this.timer)\n }\n\n const t = setTimeout(() => {\n this.timer = undefined\n this.timerExpiration = undefined\n this.purgeStale()\n for (const exp in this.expirations) {\n const e = Number(exp)\n this.setTimer(e, e - now())\n break\n }\n }, ttl)\n\n /* c8 ignore start - affordance for non-node envs */\n if (t.unref) t.unref()\n /* c8 ignore stop */\n\n this.timerExpiration = expiration\n this.timer = t\n }\n\n // hang onto the timer so we can clearTimeout if all items\n // are deleted. Deno doesn't have Timer.unref(), so it\n // hangs otherwise.\n cancelTimer() {\n if (this.timer) {\n clearTimeout(this.timer)\n this.timerExpiration = undefined\n this.timer = undefined\n }\n }\n\n /* c8 ignore start */\n cancelTimers() {\n process.emitWarning(\n 'TTLCache.cancelTimers has been renamed to ' +\n 'TTLCache.cancelTimer (no \"s\"), and will be removed in the next ' +\n 'major version update',\n )\n return this.cancelTimer()\n }\n /* c8 ignore stop */\n\n clear() {\n const entries =\n this.dispose !== TTLCache.prototype.dispose ? [...this] : []\n this.data.clear()\n this.expirationMap.clear()\n // no need for any purging now\n this.cancelTimer()\n this.expirations = Object.create(null)\n for (const [key, val] of entries as [K, V][]) {\n this.dispose(val, key, 'delete')\n }\n }\n\n setTTL(key: K, ttl = this.ttl) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n // remove from the expirations list, so it isn't purged\n const exp = this.expirations[current]\n if (!exp || exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n\n if (ttl && ttl !== Infinity) {\n const expiration = Math.floor(now() + ttl)\n this.expirationMap.set(key, expiration)\n if (!this.expirations[expiration]) {\n this.expirations[expiration] = []\n this.setTimer(expiration, ttl)\n }\n this.expirations[expiration].push(key)\n } else {\n this.expirationMap.set(key, Infinity)\n }\n }\n\n set(\n key: K,\n val: V,\n {\n ttl = this.ttl,\n noUpdateTTL = this.noUpdateTTL,\n noDisposeOnSet = this.noDisposeOnSet,\n }: SetOptions<K, V> = {},\n ) {\n if (!isPosIntOrInf(ttl)) {\n throw new TypeError('ttl must be positive integer or Infinity')\n }\n if (this.expirationMap.has(key)) {\n if (!noUpdateTTL) {\n this.setTTL(key, ttl)\n }\n // has old value\n const oldValue = this.data.get(key)\n if (oldValue !== undefined && oldValue !== val) {\n this.data.set(key, val)\n if (!noDisposeOnSet) {\n this.dispose(oldValue, key, 'set')\n }\n }\n } else {\n this.setTTL(key, ttl)\n this.data.set(key, val)\n }\n\n while (this.size > this.max) {\n this.purgeToCapacity()\n }\n\n return this\n }\n\n has(key: K) {\n return this.data.has(key)\n }\n\n getRemainingTTL(key: K) {\n const expiration = this.expirationMap.get(key)\n return expiration === Infinity\n ? expiration\n : expiration !== undefined\n ? Math.max(0, Math.ceil(expiration - now()))\n : 0\n }\n\n get(\n key: K,\n {\n updateAgeOnGet = this.updateAgeOnGet,\n ttl = this.ttl,\n checkAgeOnGet = this.checkAgeOnGet,\n }: GetOptions<K, V> = {},\n ) {\n const val = this.data.get(key)\n if (checkAgeOnGet && this.getRemainingTTL(key) === 0) {\n this.delete(key)\n return undefined\n }\n if (updateAgeOnGet) {\n this.setTTL(key, ttl)\n }\n return val\n }\n\n delete(key: K) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n const value = this.data.get(key) as V\n this.data.delete(key)\n this.expirationMap.delete(key)\n const exp = this.expirations[current]\n if (exp) {\n if (exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n this.dispose(value, key, 'delete')\n if (this.size === 0) {\n this.cancelTimer()\n }\n return true\n }\n return false\n }\n\n purgeToCapacity() {\n for (const exp in this.expirations) {\n const keys = this.expirations[exp] as K[]\n if (this.size - keys.length >= this.max) {\n delete this.expirations[exp]\n const entries: [K, V][] = []\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n } else {\n const s = this.size - this.max\n const entries: [K, V][] = []\n for (const key of keys.splice(0, s)) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n return\n }\n }\n }\n\n get size() {\n return this.data.size\n }\n\n purgeStale() {\n const n = Math.ceil(now())\n for (const exp in this.expirations) {\n if (exp === 'Infinity' || Number(exp) > n) {\n return\n }\n\n /* c8 ignore start\n * mysterious need for a guard here?\n * https://github.com/isaacs/ttlcache/issues/26 */\n const keys = [...(this.expirations[exp] || [])]\n /* c8 ignore stop */\n const entries: [K, V][] = []\n delete this.expirations[exp]\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'stale')\n }\n }\n if (this.size === 0) {\n this.cancelTimer()\n }\n }\n\n *entries() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield [key, this.data.get(key)] as [K, V]\n }\n }\n }\n *keys() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield key\n }\n }\n }\n *values() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield this.data.get(key) as V\n }\n }\n }\n [Symbol.iterator]() {\n return this.entries()\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,8DAA8D;AAC9D,kDAAkD;AAClD,+DAA+D;AAC/D,sCAAsC;;;AAEtC,qBAAqB;AACrB,MAAM,IAAI,GACR,OAAO,WAAW,KAAK,QAAQ;IAC/B,WAAW;IACX,OAAO,WAAW,CAAC,GAAG,KAAK,UAAU;IACnC,CAAC,CAAC,WAAW;IACb,CAAC,CAAC,IAAI,CAAA;AACV,oBAAoB;AAEpB,MAAM,GAAG,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;AAC5B,MAAM,QAAQ,GAAG,CAAC,CAAM,EAAe,EAAE,CACvC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAA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CAAC;YACpC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAM,CAAA;QAC/B,CAAC;IACH,CAAC;IACD,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;CACF;AAlWD,4BAkWC","sourcesContent":["// A simple TTL cache with max capacity option, ms resolution,\n// autopurge, and reasonably optimized performance\n// Relies on the fact that integer Object keys are kept sorted,\n// and managed very efficiently by V8.\n\n/* c8 ignore start */\nconst perf =\n typeof performance === 'object' &&\n performance &&\n typeof performance.now === 'function'\n ? performance\n : Date\n/* c8 ignore stop */\n\nconst now = () => perf.now()\nconst isPosInt = (n: any): n is number =>\n !!n && n === Math.floor(n) && n > 0 && isFinite(n)\nconst isPosIntOrInf = (n: any): n is number =>\n n === Infinity || isPosInt(n)\n\nexport type DisposeReason = 'set' | 'delete' | 'stale' | 'evict'\n\nexport type DisposeFunction<K, V> = (\n val: V,\n key: K,\n reason: DisposeReason,\n) => unknown\n\nexport type TimeInMilliseconds = number\n\nexport type TTLCacheOptions<K, V> = {\n /** the maximum number of items to store in the cache */\n max?: number\n /** time in ms to store items, must be positive number */\n ttl?: TimeInMilliseconds\n /** Update the remaining TTL when getting items */\n updateAgeOnGet?: boolean\n /**\n * Check the remaining age when getting items. Note that if this is not\n * set, then it's possible to get expired items that have not yet been\n * preemptively purged.\n */\n checkAgeOnGet?: boolean\n /** Update the remaining TTL when checking for an item's presence */\n updateAgeOnHas?: boolean\n /**\n * Check the remaining age when checking for an items presence. Note that if\n * this is not set, then expired items will return `true` if they have not yet\n * been preemptively purged.\n */\n checkAgeOnHas?: boolean\n /** do not update the TTL when setting a new value for an existing key */\n noUpdateTTL?: boolean\n /** A function to call when an item is removed from the cache */\n dispose?: DisposeFunction<K, V>\n /** Do not call `dispose` when setting an existing key to a new value */\n noDisposeOnSet?: boolean\n}\n\nexport type SetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'ttl' | 'noUpdateTTL' | 'noDisposeOnSet'\n>\nexport type GetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'updateAgeOnGet' | 'ttl' | 'checkAgeOnGet'\n>\n\nexport type HasOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'updateAgeOnHas' | 'ttl' | 'checkAgeOnHas'\n>\n\nexport class TTLCache<K = unknown, V = unknown> {\n expirations: Record<number, K[]> = Object.create(null)\n data = new Map<K, V>()\n expirationMap = new Map<K, number>()\n ttl?: number\n max: number\n updateAgeOnGet: boolean\n updateAgeOnHas: boolean\n noUpdateTTL: boolean\n noDisposeOnSet: boolean\n checkAgeOnGet: boolean\n checkAgeOnHas: boolean\n dispose: DisposeFunction<K, V>\n timer?: ReturnType<typeof setTimeout>\n timerExpiration?: number\n immortalKeys = new Set<K>()\n\n constructor({\n max = Infinity,\n ttl,\n updateAgeOnGet = false,\n checkAgeOnGet = false,\n updateAgeOnHas = false,\n checkAgeOnHas = false,\n noUpdateTTL = false,\n dispose,\n noDisposeOnSet = false,\n }: TTLCacheOptions<K, V> = {}) {\n if (ttl !== undefined && !isPosIntOrInf(ttl)) {\n throw new TypeError(\n 'ttl must be positive integer or Infinity if set',\n )\n }\n if (!isPosIntOrInf(max)) {\n throw new TypeError('max must be positive integer or Infinity')\n }\n this.ttl = ttl\n this.max = max\n this.updateAgeOnGet = !!updateAgeOnGet\n this.checkAgeOnGet = !!checkAgeOnGet\n this.updateAgeOnHas = !!updateAgeOnHas\n this.checkAgeOnHas = !!checkAgeOnHas\n this.noUpdateTTL = !!noUpdateTTL\n this.noDisposeOnSet = !!noDisposeOnSet\n if (dispose !== undefined) {\n if (typeof dispose !== 'function') {\n throw new TypeError('dispose must be function if set')\n }\n this.dispose = dispose\n } else {\n this.dispose = (_, __, ___) => {}\n }\n\n this.timer = undefined\n this.timerExpiration = undefined\n }\n\n setTimer(expiration: number, ttl: number) {\n if (this.timerExpiration && this.timerExpiration < expiration) {\n return\n }\n\n if (this.timer) {\n clearTimeout(this.timer)\n }\n\n const t = setTimeout(\n () => {\n this.timer = undefined\n this.timerExpiration = undefined\n this.purgeStale()\n for (const exp in this.expirations) {\n const e = Number(exp)\n this.setTimer(e, e - now())\n break\n }\n },\n Math.max(0, ttl),\n )\n\n /* c8 ignore start - affordance for non-node envs */\n if (t.unref) t.unref()\n /* c8 ignore stop */\n\n this.timerExpiration = expiration\n this.timer = t\n }\n\n // hang onto the timer so we can clearTimeout if all items\n // are deleted. Deno doesn't have Timer.unref(), so it\n // hangs otherwise.\n cancelTimer() {\n if (this.timer) {\n clearTimeout(this.timer)\n this.timerExpiration = undefined\n this.timer = undefined\n }\n }\n\n /* c8 ignore start */\n cancelTimers() {\n process.emitWarning(\n 'TTLCache.cancelTimers has been renamed to ' +\n 'TTLCache.cancelTimer (no \"s\"), and will be removed in the next ' +\n 'major version update',\n )\n return this.cancelTimer()\n }\n /* c8 ignore stop */\n\n clear() {\n const entries =\n this.dispose !== TTLCache.prototype.dispose ? [...this] : []\n this.data.clear()\n this.expirationMap.clear()\n // no need for any purging now\n this.cancelTimer()\n this.expirations = Object.create(null)\n for (const [key, val] of entries as [K, V][]) {\n this.dispose(val, key, 'delete')\n }\n }\n\n setTTL(key: K, ttl = this.ttl) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n // remove from the expirations list, so it isn't purged\n const exp = this.expirations[current]\n if (!exp || exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n\n if (ttl && ttl !== Infinity) {\n this.immortalKeys.delete(key)\n const expiration = Math.floor(now() + ttl)\n this.expirationMap.set(key, expiration)\n if (!this.expirations[expiration]) {\n this.expirations[expiration] = []\n this.setTimer(expiration, ttl)\n }\n this.expirations[expiration].push(key)\n } else {\n this.immortalKeys.add(key)\n this.expirationMap.set(key, Infinity)\n }\n }\n\n set(\n key: K,\n val: V,\n {\n ttl = this.ttl,\n noUpdateTTL = this.noUpdateTTL,\n noDisposeOnSet = this.noDisposeOnSet,\n }: SetOptions<K, V> = {},\n ) {\n if (!isPosIntOrInf(ttl)) {\n throw new TypeError('ttl must be positive integer or Infinity')\n }\n if (this.expirationMap.has(key)) {\n if (!noUpdateTTL) {\n this.setTTL(key, ttl)\n }\n // has old value\n const oldValue = this.data.get(key)\n if (oldValue !== undefined && oldValue !== val) {\n this.data.set(key, val)\n if (!noDisposeOnSet) {\n this.dispose(oldValue, key, 'set')\n }\n }\n } else {\n this.setTTL(key, ttl)\n this.data.set(key, val)\n }\n\n while (this.size > this.max) {\n this.purgeToCapacity()\n }\n\n return this\n }\n\n has(\n key: K,\n {\n checkAgeOnHas = this.checkAgeOnHas,\n ttl = this.ttl,\n updateAgeOnHas = this.updateAgeOnHas,\n }: HasOptions<K, V> = {},\n ) {\n if (this.data.has(key)) {\n if (checkAgeOnHas && this.getRemainingTTL(key) === 0) {\n this.delete(key)\n return false\n }\n if (updateAgeOnHas) {\n this.setTTL(key, ttl)\n }\n return true\n }\n return false\n }\n\n getRemainingTTL(key: K) {\n const expiration = this.expirationMap.get(key)\n return expiration === Infinity\n ? expiration\n : expiration !== undefined\n ? Math.max(0, Math.ceil(expiration - now()))\n : 0\n }\n\n get(\n key: K,\n {\n updateAgeOnGet = this.updateAgeOnGet,\n ttl = this.ttl,\n checkAgeOnGet = this.checkAgeOnGet,\n }: GetOptions<K, V> = {},\n ) {\n const val = this.data.get(key)\n if (checkAgeOnGet && this.getRemainingTTL(key) === 0) {\n this.delete(key)\n return undefined\n }\n if (updateAgeOnGet) {\n this.setTTL(key, ttl)\n }\n return val\n }\n\n delete(key: K) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n const value = this.data.get(key) as V\n this.data.delete(key)\n this.expirationMap.delete(key)\n this.immortalKeys.delete(key)\n const exp = this.expirations[current]\n if (exp) {\n if (exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n this.dispose(value, key, 'delete')\n if (this.size === 0) {\n this.cancelTimer()\n }\n return true\n }\n return false\n }\n\n purgeToCapacity() {\n for (const exp in this.expirations) {\n const keys = this.expirations[exp] as K[]\n if (this.size - keys.length >= this.max) {\n delete this.expirations[exp]\n const entries: [K, V][] = []\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n } else {\n const s = this.size - this.max\n const entries: [K, V][] = []\n for (const key of keys.splice(0, s)) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n return\n }\n }\n }\n\n get size() {\n return this.data.size\n }\n\n purgeStale() {\n const n = Math.ceil(now())\n for (const exp in this.expirations) {\n if (exp === 'Infinity' || Number(exp) > n) {\n return\n }\n\n /* c8 ignore start\n * mysterious need for a guard here?\n * https://github.com/isaacs/ttlcache/issues/26 */\n const keys = [...(this.expirations[exp] || [])]\n /* c8 ignore stop */\n const entries: [K, V][] = []\n delete this.expirations[exp]\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'stale')\n }\n }\n if (this.size === 0) {\n this.cancelTimer()\n }\n }\n\n *entries() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield [key, this.data.get(key)] as [K, V]\n }\n }\n for (const key of this.immortalKeys) {\n yield [key, this.data.get(key)] as [K, V]\n }\n }\n *keys() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield key\n }\n }\n for (const key of this.immortalKeys) {\n yield key\n }\n }\n *values() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield this.data.get(key) as V\n }\n }\n for (const key of this.immortalKeys) {\n yield this.data.get(key) as V\n }\n }\n [Symbol.iterator]() {\n return this.entries()\n }\n}\n"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,16 +1,37 @@
|
|
|
1
1
|
export type DisposeReason = 'set' | 'delete' | 'stale' | 'evict';
|
|
2
2
|
export type DisposeFunction<K, V> = (val: V, key: K, reason: DisposeReason) => unknown;
|
|
3
|
+
export type TimeInMilliseconds = number;
|
|
3
4
|
export type TTLCacheOptions<K, V> = {
|
|
5
|
+
/** the maximum number of items to store in the cache */
|
|
4
6
|
max?: number;
|
|
5
|
-
|
|
7
|
+
/** time in ms to store items, must be positive number */
|
|
8
|
+
ttl?: TimeInMilliseconds;
|
|
9
|
+
/** Update the remaining TTL when getting items */
|
|
6
10
|
updateAgeOnGet?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Check the remaining age when getting items. Note that if this is not
|
|
13
|
+
* set, then it's possible to get expired items that have not yet been
|
|
14
|
+
* preemptively purged.
|
|
15
|
+
*/
|
|
7
16
|
checkAgeOnGet?: boolean;
|
|
17
|
+
/** Update the remaining TTL when checking for an item's presence */
|
|
18
|
+
updateAgeOnHas?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Check the remaining age when checking for an items presence. Note that if
|
|
21
|
+
* this is not set, then expired items will return `true` if they have not yet
|
|
22
|
+
* been preemptively purged.
|
|
23
|
+
*/
|
|
24
|
+
checkAgeOnHas?: boolean;
|
|
25
|
+
/** do not update the TTL when setting a new value for an existing key */
|
|
8
26
|
noUpdateTTL?: boolean;
|
|
27
|
+
/** A function to call when an item is removed from the cache */
|
|
9
28
|
dispose?: DisposeFunction<K, V>;
|
|
29
|
+
/** Do not call `dispose` when setting an existing key to a new value */
|
|
10
30
|
noDisposeOnSet?: boolean;
|
|
11
31
|
};
|
|
12
32
|
export type SetOptions<K, V> = Pick<TTLCacheOptions<K, V>, 'ttl' | 'noUpdateTTL' | 'noDisposeOnSet'>;
|
|
13
33
|
export type GetOptions<K, V> = Pick<TTLCacheOptions<K, V>, 'updateAgeOnGet' | 'ttl' | 'checkAgeOnGet'>;
|
|
34
|
+
export type HasOptions<K, V> = Pick<TTLCacheOptions<K, V>, 'updateAgeOnHas' | 'ttl' | 'checkAgeOnHas'>;
|
|
14
35
|
export declare class TTLCache<K = unknown, V = unknown> {
|
|
15
36
|
expirations: Record<number, K[]>;
|
|
16
37
|
data: Map<K, V>;
|
|
@@ -18,20 +39,23 @@ export declare class TTLCache<K = unknown, V = unknown> {
|
|
|
18
39
|
ttl?: number;
|
|
19
40
|
max: number;
|
|
20
41
|
updateAgeOnGet: boolean;
|
|
42
|
+
updateAgeOnHas: boolean;
|
|
21
43
|
noUpdateTTL: boolean;
|
|
22
44
|
noDisposeOnSet: boolean;
|
|
23
45
|
checkAgeOnGet: boolean;
|
|
46
|
+
checkAgeOnHas: boolean;
|
|
24
47
|
dispose: DisposeFunction<K, V>;
|
|
25
48
|
timer?: ReturnType<typeof setTimeout>;
|
|
26
49
|
timerExpiration?: number;
|
|
27
|
-
|
|
50
|
+
immortalKeys: Set<K>;
|
|
51
|
+
constructor({ max, ttl, updateAgeOnGet, checkAgeOnGet, updateAgeOnHas, checkAgeOnHas, noUpdateTTL, dispose, noDisposeOnSet, }?: TTLCacheOptions<K, V>);
|
|
28
52
|
setTimer(expiration: number, ttl: number): void;
|
|
29
53
|
cancelTimer(): void;
|
|
30
54
|
cancelTimers(): void;
|
|
31
55
|
clear(): void;
|
|
32
56
|
setTTL(key: K, ttl?: number | undefined): void;
|
|
33
57
|
set(key: K, val: V, { ttl, noUpdateTTL, noDisposeOnSet, }?: SetOptions<K, V>): this;
|
|
34
|
-
has(key: K): boolean;
|
|
58
|
+
has(key: K, { checkAgeOnHas, ttl, updateAgeOnHas, }?: HasOptions<K, V>): boolean;
|
|
35
59
|
getRemainingTTL(key: K): number;
|
|
36
60
|
get(key: K, { updateAgeOnGet, ttl, checkAgeOnGet, }?: GetOptions<K, V>): V | undefined;
|
|
37
61
|
delete(key: K): boolean;
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAoBA,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAA;AAEhE,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,IAAI,CAClC,GAAG,EAAE,CAAC,EACN,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,aAAa,KAClB,OAAO,CAAA;AAEZ,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,IAAI;IAClC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAoBA,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAA;AAEhE,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,IAAI,CAClC,GAAG,EAAE,CAAC,EACN,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,aAAa,KAClB,OAAO,CAAA;AAEZ,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAA;AAEvC,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,IAAI;IAClC,wDAAwD;IACxD,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,yDAAyD;IACzD,GAAG,CAAC,EAAE,kBAAkB,CAAA;IACxB,kDAAkD;IAClD,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,oEAAoE;IACpE,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,yEAAyE;IACzE,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,gEAAgE;IAChE,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAA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EACP,cAAsB,GACvB,GAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAM;IA8B7B,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAkCxC,WAAW;IASX,YAAY;IAUZ,KAAK;IAaL,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,qBAAW;IA2B7B,GAAG,CACD,GAAG,EAAE,CAAC,EACN,GAAG,EAAE,CAAC,EACN,EACE,GAAc,EACd,WAA8B,EAC9B,cAAoC,GACrC,GAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAM;IA6B1B,GAAG,CACD,GAAG,EAAE,CAAC,EACN,EACE,aAAkC,EAClC,GAAc,EACd,cAAoC,GACrC,GAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAM;IAe1B,eAAe,CAAC,GAAG,EAAE,CAAC;IAStB,GAAG,CACD,GAAG,EAAE,CAAC,EACN,EACE,cAAoC,EACpC,GAAc,EACd,aAAkC,GACnC,GAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAM;IAa1B,MAAM,CAAC,GAAG,EAAE,CAAC;IAwBb,eAAe;IA8Bf,IAAI,IAAI,WAEP;IAED,UAAU;IA4BT,OAAO;IAUP,IAAI;IAUJ,MAAM;IAUP,CAAC,MAAM,CAAC,QAAQ,CAAC;CAGlB"}
|
package/dist/esm/index.js
CHANGED
|
@@ -19,13 +19,16 @@ export class TTLCache {
|
|
|
19
19
|
ttl;
|
|
20
20
|
max;
|
|
21
21
|
updateAgeOnGet;
|
|
22
|
+
updateAgeOnHas;
|
|
22
23
|
noUpdateTTL;
|
|
23
24
|
noDisposeOnSet;
|
|
24
25
|
checkAgeOnGet;
|
|
26
|
+
checkAgeOnHas;
|
|
25
27
|
dispose;
|
|
26
28
|
timer;
|
|
27
29
|
timerExpiration;
|
|
28
|
-
|
|
30
|
+
immortalKeys = new Set();
|
|
31
|
+
constructor({ max = Infinity, ttl, updateAgeOnGet = false, checkAgeOnGet = false, updateAgeOnHas = false, checkAgeOnHas = false, noUpdateTTL = false, dispose, noDisposeOnSet = false, } = {}) {
|
|
29
32
|
if (ttl !== undefined && !isPosIntOrInf(ttl)) {
|
|
30
33
|
throw new TypeError('ttl must be positive integer or Infinity if set');
|
|
31
34
|
}
|
|
@@ -36,6 +39,8 @@ export class TTLCache {
|
|
|
36
39
|
this.max = max;
|
|
37
40
|
this.updateAgeOnGet = !!updateAgeOnGet;
|
|
38
41
|
this.checkAgeOnGet = !!checkAgeOnGet;
|
|
42
|
+
this.updateAgeOnHas = !!updateAgeOnHas;
|
|
43
|
+
this.checkAgeOnHas = !!checkAgeOnHas;
|
|
39
44
|
this.noUpdateTTL = !!noUpdateTTL;
|
|
40
45
|
this.noDisposeOnSet = !!noDisposeOnSet;
|
|
41
46
|
if (dispose !== undefined) {
|
|
@@ -66,7 +71,7 @@ export class TTLCache {
|
|
|
66
71
|
this.setTimer(e, e - now());
|
|
67
72
|
break;
|
|
68
73
|
}
|
|
69
|
-
}, ttl);
|
|
74
|
+
}, Math.max(0, ttl));
|
|
70
75
|
/* c8 ignore start - affordance for non-node envs */
|
|
71
76
|
if (t.unref)
|
|
72
77
|
t.unref();
|
|
@@ -116,6 +121,7 @@ export class TTLCache {
|
|
|
116
121
|
}
|
|
117
122
|
}
|
|
118
123
|
if (ttl && ttl !== Infinity) {
|
|
124
|
+
this.immortalKeys.delete(key);
|
|
119
125
|
const expiration = Math.floor(now() + ttl);
|
|
120
126
|
this.expirationMap.set(key, expiration);
|
|
121
127
|
if (!this.expirations[expiration]) {
|
|
@@ -125,6 +131,7 @@ export class TTLCache {
|
|
|
125
131
|
this.expirations[expiration].push(key);
|
|
126
132
|
}
|
|
127
133
|
else {
|
|
134
|
+
this.immortalKeys.add(key);
|
|
128
135
|
this.expirationMap.set(key, Infinity);
|
|
129
136
|
}
|
|
130
137
|
}
|
|
@@ -154,8 +161,18 @@ export class TTLCache {
|
|
|
154
161
|
}
|
|
155
162
|
return this;
|
|
156
163
|
}
|
|
157
|
-
has(key) {
|
|
158
|
-
|
|
164
|
+
has(key, { checkAgeOnHas = this.checkAgeOnHas, ttl = this.ttl, updateAgeOnHas = this.updateAgeOnHas, } = {}) {
|
|
165
|
+
if (this.data.has(key)) {
|
|
166
|
+
if (checkAgeOnHas && this.getRemainingTTL(key) === 0) {
|
|
167
|
+
this.delete(key);
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
if (updateAgeOnHas) {
|
|
171
|
+
this.setTTL(key, ttl);
|
|
172
|
+
}
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
159
176
|
}
|
|
160
177
|
getRemainingTTL(key) {
|
|
161
178
|
const expiration = this.expirationMap.get(key);
|
|
@@ -182,6 +199,7 @@ export class TTLCache {
|
|
|
182
199
|
const value = this.data.get(key);
|
|
183
200
|
this.data.delete(key);
|
|
184
201
|
this.expirationMap.delete(key);
|
|
202
|
+
this.immortalKeys.delete(key);
|
|
185
203
|
const exp = this.expirations[current];
|
|
186
204
|
if (exp) {
|
|
187
205
|
if (exp.length <= 1) {
|
|
@@ -264,6 +282,9 @@ export class TTLCache {
|
|
|
264
282
|
yield [key, this.data.get(key)];
|
|
265
283
|
}
|
|
266
284
|
}
|
|
285
|
+
for (const key of this.immortalKeys) {
|
|
286
|
+
yield [key, this.data.get(key)];
|
|
287
|
+
}
|
|
267
288
|
}
|
|
268
289
|
*keys() {
|
|
269
290
|
for (const exp in this.expirations) {
|
|
@@ -271,6 +292,9 @@ export class TTLCache {
|
|
|
271
292
|
yield key;
|
|
272
293
|
}
|
|
273
294
|
}
|
|
295
|
+
for (const key of this.immortalKeys) {
|
|
296
|
+
yield key;
|
|
297
|
+
}
|
|
274
298
|
}
|
|
275
299
|
*values() {
|
|
276
300
|
for (const exp in this.expirations) {
|
|
@@ -278,6 +302,9 @@ export class TTLCache {
|
|
|
278
302
|
yield this.data.get(key);
|
|
279
303
|
}
|
|
280
304
|
}
|
|
305
|
+
for (const key of this.immortalKeys) {
|
|
306
|
+
yield this.data.get(key);
|
|
307
|
+
}
|
|
281
308
|
}
|
|
282
309
|
[Symbol.iterator]() {
|
|
283
310
|
return this.entries();
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,kDAAkD;AAClD,+DAA+D;AAC/D,sCAAsC;AAEtC,qBAAqB;AACrB,MAAM,IAAI,GACR,OAAO,WAAW,KAAK,QAAQ;IAC/B,WAAW;IACX,OAAO,WAAW,CAAC,GAAG,KAAK,UAAU;IACnC,CAAC,CAAC,WAAW;IACb,CAAC,CAAC,IAAI,CAAA;AACV,oBAAoB;AAEpB,MAAM,GAAG,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;AAC5B,MAAM,QAAQ,GAAG,CAAC,CAAM,EAAe,EAAE,CACvC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAA;AACpD,MAAM,aAAa,GAAG,CAAC,CAAM,EAAe,EAAE,CAC5C,CAAC,KAAK,QAAQ,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAA;AA6B/B,MAAM,OAAO,QAAQ;IACnB,WAAW,GAAwB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACtD,IAAI,GAAG,IAAI,GAAG,EAAQ,CAAA;IACtB,aAAa,GAAG,IAAI,GAAG,EAAa,CAAA;IACpC,GAAG,CAAS;IACZ,GAAG,CAAQ;IACX,cAAc,CAAS;IACvB,WAAW,CAAS;IACpB,cAAc,CAAS;IACvB,aAAa,CAAS;IACtB,OAAO,CAAuB;IAC9B,KAAK,CAAgC;IACrC,eAAe,CAAS;IAExB,YAAY,EACV,GAAG,GAAG,QAAQ,EACd,GAAG,EACH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WAAW,EAAE,CAAC;YACnC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAQ,EAAE,CAAC;gBAC/C,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAW,CAAA;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IACD,CAAC,IAAI;QACH,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACnC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAQ,EAAE,CAAC;gBAC/C,MAAM,GAAG,CAAA;YACX,CAAC;QACH,CAAC;IACH,CAAC;IACD,CAAC,MAAM;QACL,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACnC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,WAAW,CAAC,GAAG,CAAQ,EAAE,CAAC;gBAC/C,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAM,CAAA;YAC/B,CAAC;QACH,CAAC;IACH,CAAC;IACD,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;CACF","sourcesContent":["// A simple TTL cache with max capacity option, ms resolution,\n// autopurge, and reasonably optimized performance\n// Relies on the fact that integer Object keys are kept sorted,\n// and managed very efficiently by V8.\n\n/* c8 ignore start */\nconst perf =\n typeof performance === 'object' &&\n performance &&\n typeof performance.now === 'function'\n ? performance\n : Date\n/* c8 ignore stop */\n\nconst now = () => perf.now()\nconst isPosInt = (n: any): n is number =>\n !!n && n === Math.floor(n) && n > 0 && isFinite(n)\nconst isPosIntOrInf = (n: any): n is number =>\n n === Infinity || isPosInt(n)\n\nexport type DisposeReason = 'set' | 'delete' | 'stale' | 'evict'\n\nexport type DisposeFunction<K, V> = (\n val: V,\n key: K,\n reason: DisposeReason,\n) => unknown\n\nexport type TTLCacheOptions<K, V> = {\n max?: number\n ttl?: number\n updateAgeOnGet?: boolean\n checkAgeOnGet?: boolean\n noUpdateTTL?: boolean\n dispose?: DisposeFunction<K, V>\n noDisposeOnSet?: boolean\n}\n\nexport type SetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'ttl' | 'noUpdateTTL' | 'noDisposeOnSet'\n>\nexport type GetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'updateAgeOnGet' | 'ttl' | 'checkAgeOnGet'\n>\n\nexport class TTLCache<K = unknown, V = unknown> {\n expirations: Record<number, K[]> = Object.create(null)\n data = new Map<K, V>()\n expirationMap = new Map<K, number>()\n ttl?: number\n max: number\n updateAgeOnGet: boolean\n noUpdateTTL: boolean\n noDisposeOnSet: boolean\n checkAgeOnGet: boolean\n dispose: DisposeFunction<K, V>\n timer?: ReturnType<typeof setTimeout>\n timerExpiration?: number\n\n constructor({\n max = Infinity,\n ttl,\n updateAgeOnGet = false,\n checkAgeOnGet = false,\n noUpdateTTL = false,\n dispose,\n noDisposeOnSet = false,\n }: TTLCacheOptions<K, V> = {}) {\n if (ttl !== undefined && !isPosIntOrInf(ttl)) {\n throw new TypeError(\n 'ttl must be positive integer or Infinity if set',\n )\n }\n if (!isPosIntOrInf(max)) {\n throw new TypeError('max must be positive integer or Infinity')\n }\n this.ttl = ttl\n this.max = max\n this.updateAgeOnGet = !!updateAgeOnGet\n this.checkAgeOnGet = !!checkAgeOnGet\n this.noUpdateTTL = !!noUpdateTTL\n this.noDisposeOnSet = !!noDisposeOnSet\n if (dispose !== undefined) {\n if (typeof dispose !== 'function') {\n throw new TypeError('dispose must be function if set')\n }\n this.dispose = dispose\n } else {\n this.dispose = (_, __, ___) => {}\n }\n\n this.timer = undefined\n this.timerExpiration = undefined\n }\n\n setTimer(expiration: number, ttl: number) {\n if (this.timerExpiration && this.timerExpiration < expiration) {\n return\n }\n\n if (this.timer) {\n clearTimeout(this.timer)\n }\n\n const t = setTimeout(() => {\n this.timer = undefined\n this.timerExpiration = undefined\n this.purgeStale()\n for (const exp in this.expirations) {\n const e = Number(exp)\n this.setTimer(e, e - now())\n break\n }\n }, ttl)\n\n /* c8 ignore start - affordance for non-node envs */\n if (t.unref) t.unref()\n /* c8 ignore stop */\n\n this.timerExpiration = expiration\n this.timer = t\n }\n\n // hang onto the timer so we can clearTimeout if all items\n // are deleted. Deno doesn't have Timer.unref(), so it\n // hangs otherwise.\n cancelTimer() {\n if (this.timer) {\n clearTimeout(this.timer)\n this.timerExpiration = undefined\n this.timer = undefined\n }\n }\n\n /* c8 ignore start */\n cancelTimers() {\n process.emitWarning(\n 'TTLCache.cancelTimers has been renamed to ' +\n 'TTLCache.cancelTimer (no \"s\"), and will be removed in the next ' +\n 'major version update',\n )\n return this.cancelTimer()\n }\n /* c8 ignore stop */\n\n clear() {\n const entries =\n this.dispose !== TTLCache.prototype.dispose ? [...this] : []\n this.data.clear()\n this.expirationMap.clear()\n // no need for any purging now\n this.cancelTimer()\n this.expirations = Object.create(null)\n for (const [key, val] of entries as [K, V][]) {\n this.dispose(val, key, 'delete')\n }\n }\n\n setTTL(key: K, ttl = this.ttl) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n // remove from the expirations list, so it isn't purged\n const exp = this.expirations[current]\n if (!exp || exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n\n if (ttl && ttl !== Infinity) {\n const expiration = Math.floor(now() + ttl)\n this.expirationMap.set(key, expiration)\n if (!this.expirations[expiration]) {\n this.expirations[expiration] = []\n this.setTimer(expiration, ttl)\n }\n this.expirations[expiration].push(key)\n } else {\n this.expirationMap.set(key, Infinity)\n }\n }\n\n set(\n key: K,\n val: V,\n {\n ttl = this.ttl,\n noUpdateTTL = this.noUpdateTTL,\n noDisposeOnSet = this.noDisposeOnSet,\n }: SetOptions<K, V> = {},\n ) {\n if (!isPosIntOrInf(ttl)) {\n throw new TypeError('ttl must be positive integer or Infinity')\n }\n if (this.expirationMap.has(key)) {\n if (!noUpdateTTL) {\n this.setTTL(key, ttl)\n }\n // has old value\n const oldValue = this.data.get(key)\n if (oldValue !== undefined && oldValue !== val) {\n this.data.set(key, val)\n if (!noDisposeOnSet) {\n this.dispose(oldValue, key, 'set')\n }\n }\n } else {\n this.setTTL(key, ttl)\n this.data.set(key, val)\n }\n\n while (this.size > this.max) {\n this.purgeToCapacity()\n }\n\n return this\n }\n\n has(key: K) {\n return this.data.has(key)\n }\n\n getRemainingTTL(key: K) {\n const expiration = this.expirationMap.get(key)\n return expiration === Infinity\n ? expiration\n : expiration !== undefined\n ? Math.max(0, Math.ceil(expiration - now()))\n : 0\n }\n\n get(\n key: K,\n {\n updateAgeOnGet = this.updateAgeOnGet,\n ttl = this.ttl,\n checkAgeOnGet = this.checkAgeOnGet,\n }: GetOptions<K, V> = {},\n ) {\n const val = this.data.get(key)\n if (checkAgeOnGet && this.getRemainingTTL(key) === 0) {\n this.delete(key)\n return undefined\n }\n if (updateAgeOnGet) {\n this.setTTL(key, ttl)\n }\n return val\n }\n\n delete(key: K) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n const value = this.data.get(key) as V\n this.data.delete(key)\n this.expirationMap.delete(key)\n const exp = this.expirations[current]\n if (exp) {\n if (exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n this.dispose(value, key, 'delete')\n if (this.size === 0) {\n this.cancelTimer()\n }\n return true\n }\n return false\n }\n\n purgeToCapacity() {\n for (const exp in this.expirations) {\n const keys = this.expirations[exp] as K[]\n if (this.size - keys.length >= this.max) {\n delete this.expirations[exp]\n const entries: [K, V][] = []\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n } else {\n const s = this.size - this.max\n const entries: [K, V][] = []\n for (const key of keys.splice(0, s)) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n return\n }\n }\n }\n\n get size() {\n return this.data.size\n }\n\n purgeStale() {\n const n = Math.ceil(now())\n for (const exp in this.expirations) {\n if (exp === 'Infinity' || Number(exp) > n) {\n return\n }\n\n /* c8 ignore start\n * mysterious need for a guard here?\n * https://github.com/isaacs/ttlcache/issues/26 */\n const keys = [...(this.expirations[exp] || [])]\n /* c8 ignore stop */\n const entries: [K, V][] = []\n delete this.expirations[exp]\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'stale')\n }\n }\n if (this.size === 0) {\n this.cancelTimer()\n }\n }\n\n *entries() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield [key, this.data.get(key)] as [K, V]\n }\n }\n }\n *keys() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield key\n }\n }\n }\n *values() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield this.data.get(key) as V\n }\n }\n }\n [Symbol.iterator]() {\n return this.entries()\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,kDAAkD;AAClD,+DAA+D;AAC/D,sCAAsC;AAEtC,qBAAqB;AACrB,MAAM,IAAI,GACR,OAAO,WAAW,KAAK,QAAQ;IAC/B,WAAW;IACX,OAAO,WAAW,CAAC,GAAG,KAAK,UAAU;IACnC,CAAC,CAAC,WAAW;IACb,CAAC,CAAC,IAAI,CAAA;AACV,oBAAoB;AAEpB,MAAM,GAAG,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;AAC5B,MAAM,QAAQ,GAAG,CAAC,CAAM,EAAe,EAAE,CACvC,CAAC,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AC/B,CAAC;QACH,CAAC;QACD,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACpC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAM,CAAA;QAC/B,CAAC;IACH,CAAC;IACD,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;CACF","sourcesContent":["// A simple TTL cache with max capacity option, ms resolution,\n// autopurge, and reasonably optimized performance\n// Relies on the fact that integer Object keys are kept sorted,\n// and managed very efficiently by V8.\n\n/* c8 ignore start */\nconst perf =\n typeof performance === 'object' &&\n performance &&\n typeof performance.now === 'function'\n ? performance\n : Date\n/* c8 ignore stop */\n\nconst now = () => perf.now()\nconst isPosInt = (n: any): n is number =>\n !!n && n === Math.floor(n) && n > 0 && isFinite(n)\nconst isPosIntOrInf = (n: any): n is number =>\n n === Infinity || isPosInt(n)\n\nexport type DisposeReason = 'set' | 'delete' | 'stale' | 'evict'\n\nexport type DisposeFunction<K, V> = (\n val: V,\n key: K,\n reason: DisposeReason,\n) => unknown\n\nexport type TimeInMilliseconds = number\n\nexport type TTLCacheOptions<K, V> = {\n /** the maximum number of items to store in the cache */\n max?: number\n /** time in ms to store items, must be positive number */\n ttl?: TimeInMilliseconds\n /** Update the remaining TTL when getting items */\n updateAgeOnGet?: boolean\n /**\n * Check the remaining age when getting items. Note that if this is not\n * set, then it's possible to get expired items that have not yet been\n * preemptively purged.\n */\n checkAgeOnGet?: boolean\n /** Update the remaining TTL when checking for an item's presence */\n updateAgeOnHas?: boolean\n /**\n * Check the remaining age when checking for an items presence. Note that if\n * this is not set, then expired items will return `true` if they have not yet\n * been preemptively purged.\n */\n checkAgeOnHas?: boolean\n /** do not update the TTL when setting a new value for an existing key */\n noUpdateTTL?: boolean\n /** A function to call when an item is removed from the cache */\n dispose?: DisposeFunction<K, V>\n /** Do not call `dispose` when setting an existing key to a new value */\n noDisposeOnSet?: boolean\n}\n\nexport type SetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'ttl' | 'noUpdateTTL' | 'noDisposeOnSet'\n>\nexport type GetOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'updateAgeOnGet' | 'ttl' | 'checkAgeOnGet'\n>\n\nexport type HasOptions<K, V> = Pick<\n TTLCacheOptions<K, V>,\n 'updateAgeOnHas' | 'ttl' | 'checkAgeOnHas'\n>\n\nexport class TTLCache<K = unknown, V = unknown> {\n expirations: Record<number, K[]> = Object.create(null)\n data = new Map<K, V>()\n expirationMap = new Map<K, number>()\n ttl?: number\n max: number\n updateAgeOnGet: boolean\n updateAgeOnHas: boolean\n noUpdateTTL: boolean\n noDisposeOnSet: boolean\n checkAgeOnGet: boolean\n checkAgeOnHas: boolean\n dispose: DisposeFunction<K, V>\n timer?: ReturnType<typeof setTimeout>\n timerExpiration?: number\n immortalKeys = new Set<K>()\n\n constructor({\n max = Infinity,\n ttl,\n updateAgeOnGet = false,\n checkAgeOnGet = false,\n updateAgeOnHas = false,\n checkAgeOnHas = false,\n noUpdateTTL = false,\n dispose,\n noDisposeOnSet = false,\n }: TTLCacheOptions<K, V> = {}) {\n if (ttl !== undefined && !isPosIntOrInf(ttl)) {\n throw new TypeError(\n 'ttl must be positive integer or Infinity if set',\n )\n }\n if (!isPosIntOrInf(max)) {\n throw new TypeError('max must be positive integer or Infinity')\n }\n this.ttl = ttl\n this.max = max\n this.updateAgeOnGet = !!updateAgeOnGet\n this.checkAgeOnGet = !!checkAgeOnGet\n this.updateAgeOnHas = !!updateAgeOnHas\n this.checkAgeOnHas = !!checkAgeOnHas\n this.noUpdateTTL = !!noUpdateTTL\n this.noDisposeOnSet = !!noDisposeOnSet\n if (dispose !== undefined) {\n if (typeof dispose !== 'function') {\n throw new TypeError('dispose must be function if set')\n }\n this.dispose = dispose\n } else {\n this.dispose = (_, __, ___) => {}\n }\n\n this.timer = undefined\n this.timerExpiration = undefined\n }\n\n setTimer(expiration: number, ttl: number) {\n if (this.timerExpiration && this.timerExpiration < expiration) {\n return\n }\n\n if (this.timer) {\n clearTimeout(this.timer)\n }\n\n const t = setTimeout(\n () => {\n this.timer = undefined\n this.timerExpiration = undefined\n this.purgeStale()\n for (const exp in this.expirations) {\n const e = Number(exp)\n this.setTimer(e, e - now())\n break\n }\n },\n Math.max(0, ttl),\n )\n\n /* c8 ignore start - affordance for non-node envs */\n if (t.unref) t.unref()\n /* c8 ignore stop */\n\n this.timerExpiration = expiration\n this.timer = t\n }\n\n // hang onto the timer so we can clearTimeout if all items\n // are deleted. Deno doesn't have Timer.unref(), so it\n // hangs otherwise.\n cancelTimer() {\n if (this.timer) {\n clearTimeout(this.timer)\n this.timerExpiration = undefined\n this.timer = undefined\n }\n }\n\n /* c8 ignore start */\n cancelTimers() {\n process.emitWarning(\n 'TTLCache.cancelTimers has been renamed to ' +\n 'TTLCache.cancelTimer (no \"s\"), and will be removed in the next ' +\n 'major version update',\n )\n return this.cancelTimer()\n }\n /* c8 ignore stop */\n\n clear() {\n const entries =\n this.dispose !== TTLCache.prototype.dispose ? [...this] : []\n this.data.clear()\n this.expirationMap.clear()\n // no need for any purging now\n this.cancelTimer()\n this.expirations = Object.create(null)\n for (const [key, val] of entries as [K, V][]) {\n this.dispose(val, key, 'delete')\n }\n }\n\n setTTL(key: K, ttl = this.ttl) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n // remove from the expirations list, so it isn't purged\n const exp = this.expirations[current]\n if (!exp || exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n\n if (ttl && ttl !== Infinity) {\n this.immortalKeys.delete(key)\n const expiration = Math.floor(now() + ttl)\n this.expirationMap.set(key, expiration)\n if (!this.expirations[expiration]) {\n this.expirations[expiration] = []\n this.setTimer(expiration, ttl)\n }\n this.expirations[expiration].push(key)\n } else {\n this.immortalKeys.add(key)\n this.expirationMap.set(key, Infinity)\n }\n }\n\n set(\n key: K,\n val: V,\n {\n ttl = this.ttl,\n noUpdateTTL = this.noUpdateTTL,\n noDisposeOnSet = this.noDisposeOnSet,\n }: SetOptions<K, V> = {},\n ) {\n if (!isPosIntOrInf(ttl)) {\n throw new TypeError('ttl must be positive integer or Infinity')\n }\n if (this.expirationMap.has(key)) {\n if (!noUpdateTTL) {\n this.setTTL(key, ttl)\n }\n // has old value\n const oldValue = this.data.get(key)\n if (oldValue !== undefined && oldValue !== val) {\n this.data.set(key, val)\n if (!noDisposeOnSet) {\n this.dispose(oldValue, key, 'set')\n }\n }\n } else {\n this.setTTL(key, ttl)\n this.data.set(key, val)\n }\n\n while (this.size > this.max) {\n this.purgeToCapacity()\n }\n\n return this\n }\n\n has(\n key: K,\n {\n checkAgeOnHas = this.checkAgeOnHas,\n ttl = this.ttl,\n updateAgeOnHas = this.updateAgeOnHas,\n }: HasOptions<K, V> = {},\n ) {\n if (this.data.has(key)) {\n if (checkAgeOnHas && this.getRemainingTTL(key) === 0) {\n this.delete(key)\n return false\n }\n if (updateAgeOnHas) {\n this.setTTL(key, ttl)\n }\n return true\n }\n return false\n }\n\n getRemainingTTL(key: K) {\n const expiration = this.expirationMap.get(key)\n return expiration === Infinity\n ? expiration\n : expiration !== undefined\n ? Math.max(0, Math.ceil(expiration - now()))\n : 0\n }\n\n get(\n key: K,\n {\n updateAgeOnGet = this.updateAgeOnGet,\n ttl = this.ttl,\n checkAgeOnGet = this.checkAgeOnGet,\n }: GetOptions<K, V> = {},\n ) {\n const val = this.data.get(key)\n if (checkAgeOnGet && this.getRemainingTTL(key) === 0) {\n this.delete(key)\n return undefined\n }\n if (updateAgeOnGet) {\n this.setTTL(key, ttl)\n }\n return val\n }\n\n delete(key: K) {\n const current = this.expirationMap.get(key)\n if (current !== undefined) {\n const value = this.data.get(key) as V\n this.data.delete(key)\n this.expirationMap.delete(key)\n this.immortalKeys.delete(key)\n const exp = this.expirations[current]\n if (exp) {\n if (exp.length <= 1) {\n delete this.expirations[current]\n } else {\n this.expirations[current] = exp.filter(k => k !== key)\n }\n }\n this.dispose(value, key, 'delete')\n if (this.size === 0) {\n this.cancelTimer()\n }\n return true\n }\n return false\n }\n\n purgeToCapacity() {\n for (const exp in this.expirations) {\n const keys = this.expirations[exp] as K[]\n if (this.size - keys.length >= this.max) {\n delete this.expirations[exp]\n const entries: [K, V][] = []\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n } else {\n const s = this.size - this.max\n const entries: [K, V][] = []\n for (const key of keys.splice(0, s)) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'evict')\n }\n return\n }\n }\n }\n\n get size() {\n return this.data.size\n }\n\n purgeStale() {\n const n = Math.ceil(now())\n for (const exp in this.expirations) {\n if (exp === 'Infinity' || Number(exp) > n) {\n return\n }\n\n /* c8 ignore start\n * mysterious need for a guard here?\n * https://github.com/isaacs/ttlcache/issues/26 */\n const keys = [...(this.expirations[exp] || [])]\n /* c8 ignore stop */\n const entries: [K, V][] = []\n delete this.expirations[exp]\n for (const key of keys) {\n entries.push([key, this.data.get(key) as V])\n this.data.delete(key)\n this.expirationMap.delete(key)\n }\n for (const [key, val] of entries) {\n this.dispose(val, key, 'stale')\n }\n }\n if (this.size === 0) {\n this.cancelTimer()\n }\n }\n\n *entries() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield [key, this.data.get(key)] as [K, V]\n }\n }\n for (const key of this.immortalKeys) {\n yield [key, this.data.get(key)] as [K, V]\n }\n }\n *keys() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield key\n }\n }\n for (const key of this.immortalKeys) {\n yield key\n }\n }\n *values() {\n for (const exp in this.expirations) {\n for (const key of this.expirations[exp] as K[]) {\n yield this.data.get(key) as V\n }\n }\n for (const key of this.immortalKeys) {\n yield this.data.get(key) as V\n }\n }\n [Symbol.iterator]() {\n return this.entries()\n }\n}\n"]}
|