@nxtedition/cache 1.0.5 → 1.0.6

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 CHANGED
@@ -61,23 +61,36 @@ 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
- ### Methods
64
+ ### `CacheResult<V>`
65
65
 
66
- #### `cache.get(...args): CacheResult<V>`
66
+ Both `get()` and `peek()` return a `CacheResult<V>`, a discriminated union on the `async` property:
67
67
 
68
- Returns the cached value or triggers a fetch via `valueSelector`.
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
- type CacheResult<V> =
72
- | { value: V; async: false } // cache hit
73
- | { value: Promise<V> | null; async: true } // cache miss
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
- When `async: true`, `value` is a Promise that resolves once the `valueSelector` completes.
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: null, async: true }` for missing entries.
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
 
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> | null | undefined;
21
+ value: Promise<V> | undefined;
22
22
  async: true;
23
23
  };
24
24
  export declare class AsyncCache<V = unknown, A extends unknown[] = unknown[]> {
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
@@ -261,7 +261,7 @@ export class AsyncCache {
261
261
  }
262
262
  }
263
263
 
264
- const promise = refresh ? this.#refresh(args, key) : null
264
+ const promise = refresh ? this.#refresh(args, key) : undefined
265
265
 
266
266
  return cached !== undefined
267
267
  ? { value: cached.value, async: false }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/cache",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
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": "284d1bf697548d0bda69df9a240268da019b626f"
34
+ "gitHead": "5c8b45723e68c527888e75a1536569479da7e4c5"
35
35
  }