@nxtedition/cache 1.0.15 → 2.0.0
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/lib/index.d.ts +1 -1
- package/lib/index.js +21 -13
- package/lib/memory.js +7 -2
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export declare class AsyncCache<V = unknown, A extends unknown[] = unknown[]> {
|
|
|
23
23
|
close(): void;
|
|
24
24
|
get(...args: A): CacheResult<V>;
|
|
25
25
|
peek(...args: A): CacheResult<V>;
|
|
26
|
-
refresh(...args: A):
|
|
26
|
+
refresh(...args: A): CacheResult<V>;
|
|
27
27
|
delete(...args: A): void;
|
|
28
28
|
purgeStale(): void;
|
|
29
29
|
}
|
package/lib/index.js
CHANGED
|
@@ -203,7 +203,7 @@ export class AsyncCache {
|
|
|
203
203
|
return this.#load(args, false)
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
refresh(...args )
|
|
206
|
+
refresh(...args ) {
|
|
207
207
|
return this.#refresh(args)
|
|
208
208
|
}
|
|
209
209
|
|
|
@@ -278,20 +278,16 @@ export class AsyncCache {
|
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
-
const
|
|
281
|
+
const result = refresh ? this.#refresh(args, key) : ({ value: undefined, async: false } )
|
|
282
282
|
|
|
283
|
-
if (cached !== undefined) {
|
|
283
|
+
if (result.async && cached !== undefined) {
|
|
284
284
|
return { value: cached.value, async: false }
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
-
|
|
288
|
-
return { value: promise, async: true }
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return { value: undefined, async: false }
|
|
287
|
+
return result
|
|
292
288
|
}
|
|
293
289
|
|
|
294
|
-
#refresh(args , key = this.#keySelector(...args))
|
|
290
|
+
#refresh(args , key = this.#keySelector(...args)) {
|
|
295
291
|
if (typeof key !== 'string' || key.length === 0) {
|
|
296
292
|
throw new TypeError('keySelector must return a non-empty string')
|
|
297
293
|
}
|
|
@@ -299,11 +295,23 @@ export class AsyncCache {
|
|
|
299
295
|
// TODO (fix): cross process/thread dedupe...
|
|
300
296
|
const existing = this.#dedupe.get(key)
|
|
301
297
|
if (existing !== undefined) {
|
|
302
|
-
return existing
|
|
298
|
+
return { async: true, value: existing }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const value = this.#valueSelector(...args)
|
|
302
|
+
|
|
303
|
+
if (!(value instanceof Promise)) {
|
|
304
|
+
this.#dedupe.delete(key)
|
|
305
|
+
if (value === undefined) {
|
|
306
|
+
this.#delete(key)
|
|
307
|
+
} else {
|
|
308
|
+
this.#set(key, value)
|
|
309
|
+
}
|
|
310
|
+
return { async: false, value }
|
|
303
311
|
}
|
|
304
312
|
|
|
305
313
|
// eslint-disable-next-line: no-unsafe-argument
|
|
306
|
-
const promise =
|
|
314
|
+
const promise = value.then(
|
|
307
315
|
(value) => {
|
|
308
316
|
if (this.#dedupe.get(key) === promise) {
|
|
309
317
|
this.#dedupe.delete(key)
|
|
@@ -325,7 +333,7 @@ export class AsyncCache {
|
|
|
325
333
|
promise.catch(noop)
|
|
326
334
|
this.#dedupe.set(key, promise)
|
|
327
335
|
|
|
328
|
-
return promise
|
|
336
|
+
return { async: true, value: promise }
|
|
329
337
|
}
|
|
330
338
|
|
|
331
339
|
#set(key , value ) {
|
|
@@ -372,7 +380,7 @@ export class AsyncCache {
|
|
|
372
380
|
try {
|
|
373
381
|
this.#setQuery?.run(key, data , ttl, stale)
|
|
374
382
|
} catch (err) {
|
|
375
|
-
if ((err
|
|
383
|
+
if ((err )?.errcode === 13 /* SQLITE_FULL */) {
|
|
376
384
|
try {
|
|
377
385
|
this.#evictQuery?.run(256)
|
|
378
386
|
this.#setQuery?.run(key, data , ttl, stale)
|
package/lib/memory.js
CHANGED
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
const maxInt = 2147483647
|
|
17
|
+
|
|
16
18
|
export class MemoryCache {
|
|
17
19
|
#map = new Map()
|
|
18
20
|
#arr = []
|
|
@@ -39,6 +41,8 @@ export class MemoryCache {
|
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
set(key , entry ) {
|
|
44
|
+
this.#counter = (this.#counter + 1) & maxInt
|
|
45
|
+
|
|
42
46
|
const existing = this.#map.get(key)
|
|
43
47
|
if (existing != null) {
|
|
44
48
|
this.#delete(existing)
|
|
@@ -47,7 +51,7 @@ export class MemoryCache {
|
|
|
47
51
|
this.#map.set(key, entry)
|
|
48
52
|
entry.key = key
|
|
49
53
|
entry.index = this.#arr.push(entry) - 1
|
|
50
|
-
entry.counter = this.#counter
|
|
54
|
+
entry.counter = this.#counter
|
|
51
55
|
|
|
52
56
|
this.#size += entry.size ?? 0
|
|
53
57
|
this.#count += 1
|
|
@@ -58,7 +62,8 @@ export class MemoryCache {
|
|
|
58
62
|
get(key ) {
|
|
59
63
|
const entry = this.#map.get(key)
|
|
60
64
|
if (entry != null) {
|
|
61
|
-
|
|
65
|
+
this.#counter = (this.#counter + 1) & maxInt
|
|
66
|
+
entry.counter = this.#counter
|
|
62
67
|
}
|
|
63
68
|
return entry
|
|
64
69
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/cache",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"rimraf": "^6.1.3",
|
|
29
29
|
"typescript": "^5.9.3"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "05f9f940c3b262e4c5431c953ac93bc159ade0a0"
|
|
32
32
|
}
|