@nxtedition/cache 1.0.5 → 1.0.7
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 +23 -10
- package/lib/index.d.ts +2 -2
- package/lib/index.js +6 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -61,31 +61,44 @@ if (result.async) {
|
|
|
61
61
|
| `lru` | `LRUCache.Options \| false \| null` | `{ max: 4096 }` | LRU cache options, or `false`/`null` to disable in-memory caching |
|
|
62
62
|
| `db` | `{ timeout?, maxSize? } \| false \| null` | `{ timeout: 20, maxSize: 256MB }` | SQLite options, or `false`/`null` to disable persistence |
|
|
63
63
|
|
|
64
|
-
###
|
|
64
|
+
### `CacheResult<V>`
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
Both `get()` and `peek()` return a `CacheResult<V>`, a discriminated union on the `async` property:
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
| `async` | `value` | Meaning |
|
|
69
|
+
| ------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
70
|
+
| `false` | `V` | Cache hit — the value is available synchronously. Also returned for stale entries (a background refresh is triggered automatically). |
|
|
71
|
+
| `true` | `Promise<V> \| undefined` | Cache miss — `value` is a `Promise` that resolves when the `valueSelector` completes, or `undefined` when called via `peek()` (which never triggers a fetch). |
|
|
69
72
|
|
|
70
73
|
```ts
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
const result = cache.get('key')
|
|
75
|
+
|
|
76
|
+
if (result.async) {
|
|
77
|
+
// miss — await the fetch
|
|
78
|
+
const value = await result.value
|
|
79
|
+
} else {
|
|
80
|
+
// hit (fresh or stale) — use directly
|
|
81
|
+
const value = result.value
|
|
82
|
+
}
|
|
74
83
|
```
|
|
75
84
|
|
|
76
|
-
|
|
85
|
+
### Methods
|
|
86
|
+
|
|
87
|
+
#### `cache.get(...args): CacheResult<V>`
|
|
88
|
+
|
|
89
|
+
Returns a cached value or triggers a fetch on cache miss.
|
|
77
90
|
|
|
78
91
|
#### `cache.peek(...args): CacheResult<V>`
|
|
79
92
|
|
|
80
|
-
Same as `get()` but does **not** trigger a refresh on cache miss. Returns `{ value:
|
|
93
|
+
Same as `get()` but does **not** trigger a refresh on cache miss. Returns `{ value: undefined, async: true }` for missing entries.
|
|
81
94
|
|
|
82
95
|
#### `cache.refresh(...args): Promise<V>`
|
|
83
96
|
|
|
84
97
|
Triggers a fetch via `valueSelector` regardless of cache state. If a fetch for the same key is already in-flight (from a prior `get()` or `refresh()`), the existing promise is returned instead of starting a new one.
|
|
85
98
|
|
|
86
|
-
#### `cache.delete(
|
|
99
|
+
#### `cache.delete(...args): void`
|
|
87
100
|
|
|
88
|
-
Remove
|
|
101
|
+
Remove an entry from the cache. The cache key is derived from `args` via the `keySelector`, just like `get()` and `refresh()`. Also cancels any in-flight deduplication for that key, meaning a pending fetch will not write its result to the cache.
|
|
89
102
|
|
|
90
103
|
#### `cache.purgeStale(): void`
|
|
91
104
|
|
package/lib/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export type CacheResult<V> = {
|
|
|
18
18
|
value: V;
|
|
19
19
|
async: false;
|
|
20
20
|
} | {
|
|
21
|
-
value: Promise<V> |
|
|
21
|
+
value: Promise<V> | undefined;
|
|
22
22
|
async: true;
|
|
23
23
|
};
|
|
24
24
|
export declare class AsyncCache<V = unknown, A extends unknown[] = unknown[]> {
|
|
@@ -28,7 +28,7 @@ export declare class AsyncCache<V = unknown, A extends unknown[] = unknown[]> {
|
|
|
28
28
|
get(...args: A): CacheResult<V>;
|
|
29
29
|
peek(...args: A): CacheResult<V>;
|
|
30
30
|
refresh(...args: A): Promise<V>;
|
|
31
|
-
delete(
|
|
31
|
+
delete(...args: A): void;
|
|
32
32
|
purgeStale(): void;
|
|
33
33
|
}
|
|
34
34
|
export {};
|
package/lib/index.js
CHANGED
|
@@ -57,7 +57,7 @@ const dbs = new Set ()
|
|
|
57
57
|
|
|
58
58
|
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
|
|
62
62
|
const VERSION = 2
|
|
63
63
|
const MAX_DURATION = 365000000e3
|
|
@@ -196,8 +196,8 @@ export class AsyncCache {
|
|
|
196
196
|
return this.#refresh(args)
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
delete(
|
|
200
|
-
this.#delete(
|
|
199
|
+
delete(...args ) {
|
|
200
|
+
this.#delete(args)
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
purgeStale() {
|
|
@@ -261,14 +261,14 @@ export class AsyncCache {
|
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
const promise = refresh ? this.#refresh(args, key) :
|
|
264
|
+
const promise = refresh ? this.#refresh(args, key) : undefined
|
|
265
265
|
|
|
266
266
|
return cached !== undefined
|
|
267
267
|
? { value: cached.value, async: false }
|
|
268
268
|
: { value: promise, async: true }
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
-
#refresh(args , key
|
|
271
|
+
#refresh(args , key = this.#keySelector(...args)) {
|
|
272
272
|
if (typeof key !== 'string' || key.length === 0) {
|
|
273
273
|
throw new TypeError('keySelector must return a non-empty string')
|
|
274
274
|
}
|
|
@@ -350,7 +350,7 @@ export class AsyncCache {
|
|
|
350
350
|
}
|
|
351
351
|
}
|
|
352
352
|
|
|
353
|
-
#delete(key
|
|
353
|
+
#delete(args , key = this.#keySelector(...args)) {
|
|
354
354
|
if (typeof key !== 'string' || key.length === 0) {
|
|
355
355
|
throw new TypeError('key must be a non-empty string')
|
|
356
356
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/cache",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"lru-cache": "^11.2.6"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "a113680af7f36b0262a5de692bbf57409a51b86b"
|
|
35
35
|
}
|