@nxtedition/cache 2.0.2 → 2.0.3

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 CHANGED
@@ -17,9 +17,34 @@ export type CacheResult<V> = {
17
17
  value: Promise<V>;
18
18
  async: true;
19
19
  };
20
+ declare global {
21
+ var __nxt_cache: {
22
+ stats: Cache['stats'];
23
+ }[];
24
+ }
20
25
  export declare class Cache<V = unknown, A extends unknown[] = unknown[]> {
21
26
  #private;
22
27
  constructor(location: string, valueSelector: (...args: A) => V | PromiseLike<V>, keySelector: (...args: A) => string, opts?: CacheOptions<V>);
28
+ get stats(): {
29
+ lock: {
30
+ timeout: number;
31
+ mean: number;
32
+ stddev: number;
33
+ };
34
+ dedupe: {
35
+ size: number;
36
+ };
37
+ memory: {
38
+ size: number;
39
+ maxSize: number;
40
+ count: number;
41
+ maxCount: number;
42
+ } | undefined;
43
+ database: {
44
+ location: string;
45
+ size: number | undefined;
46
+ } | undefined;
47
+ };
23
48
  close(): void;
24
49
  get(...args: A): CacheResult<V>;
25
50
  peek(...args: A): CacheResult<V>;
package/lib/index.js CHANGED
@@ -54,10 +54,17 @@ const dbs = new Set ()
54
54
 
55
55
 
56
56
 
57
+
58
+
59
+
60
+
61
+
57
62
 
58
63
  const VERSION = 4
59
64
  const MAX_DURATION = 365000000e3
60
65
 
66
+ globalThis.__nxt_cache ??= []
67
+
61
68
  export class Cache {
62
69
  #memory
63
70
  #dedupe = new Map ()
@@ -74,6 +81,7 @@ export class Cache {
74
81
  #lockTimeout = 10
75
82
  #lockId = randomUUID()
76
83
 
84
+ #location
77
85
  #database = null
78
86
  #getQuery = null
79
87
  #setQuery = null
@@ -84,6 +92,8 @@ export class Cache {
84
92
  #lockStealQuery = null
85
93
  #lockGetQuery = null
86
94
  #lockPurgeQuery = null
95
+ #pageCountQuery = null
96
+ #pageSizeQuery = null
87
97
 
88
98
  constructor(
89
99
  location ,
@@ -94,6 +104,7 @@ export class Cache {
94
104
  if (typeof location !== 'string') {
95
105
  throw new TypeError('location must be a string')
96
106
  }
107
+ this.#location = location
97
108
 
98
109
  if (typeof valueSelector !== 'function') {
99
110
  throw new TypeError('valueSelector must be a function')
@@ -189,6 +200,8 @@ export class Cache {
189
200
  this.#lockPurgeQuery = this.#database.prepare(
190
201
  `DELETE FROM cache_lock_v${VERSION} WHERE lock_acquired <= ?`,
191
202
  )
203
+ this.#pageCountQuery = this.#database.prepare('PRAGMA page_count')
204
+ this.#pageSizeQuery = this.#database.prepare('PRAGMA page_size')
192
205
  break
193
206
  } catch (err) {
194
207
  if (n >= 16) {
@@ -204,6 +217,8 @@ export class Cache {
204
217
  this.#lockStealQuery = null
205
218
  this.#lockGetQuery = null
206
219
  this.#lockPurgeQuery = null
220
+ this.#pageCountQuery = null
221
+ this.#pageSizeQuery = null
207
222
 
208
223
  process.emitWarning(err )
209
224
  break
@@ -212,6 +227,34 @@ export class Cache {
212
227
  }
213
228
 
214
229
  dbs.add(this)
230
+
231
+ globalThis.__nxt_cache.push(this)
232
+ }
233
+
234
+ get stats() {
235
+ let database
236
+ if (this.#database) {
237
+ let size
238
+ try {
239
+ const { page_count } = this.#pageCountQuery .get()
240
+ const { page_size } = this.#pageSizeQuery .get()
241
+ size = page_count * page_size
242
+ } catch (err) {
243
+ process.emitWarning(err )
244
+ }
245
+ database = { location: this.#location, size }
246
+ }
247
+
248
+ return {
249
+ lock: {
250
+ timeout: this.#lockTimeout,
251
+ mean: this.#lockMean,
252
+ stddev: Math.sqrt(this.#lockVar),
253
+ },
254
+ dedupe: { size: this.#dedupe.size },
255
+ memory: this.#memory?.stats,
256
+ database,
257
+ }
215
258
  }
216
259
 
217
260
  close() {
@@ -228,8 +271,15 @@ export class Cache {
228
271
  this.#lockStealQuery = null
229
272
  this.#lockGetQuery = null
230
273
  this.#lockPurgeQuery = null
274
+ this.#pageCountQuery = null
275
+ this.#pageSizeQuery = null
231
276
  this.#database?.close()
232
277
  this.#database = null
278
+
279
+ const idx = globalThis.__nxt_cache.indexOf(this)
280
+ if (idx !== -1) {
281
+ globalThis.__nxt_cache.splice(idx, 1)
282
+ }
233
283
  }
234
284
 
235
285
  get(...args ) {
package/lib/memory.d.ts CHANGED
@@ -18,4 +18,10 @@ export declare class MemoryCache<V> {
18
18
  get(key: string): MemoryCacheEntry<V> | undefined;
19
19
  delete(key: string): void;
20
20
  purgeStale(now: number): void;
21
+ get stats(): {
22
+ size: number;
23
+ maxSize: number;
24
+ count: number;
25
+ maxCount: number;
26
+ };
21
27
  }
package/lib/memory.js CHANGED
@@ -97,6 +97,15 @@ export class MemoryCache {
97
97
  }
98
98
  }
99
99
 
100
+ get stats() {
101
+ return {
102
+ size: this.#size,
103
+ maxSize: this.#maxSize,
104
+ count: this.#count,
105
+ maxCount: this.#maxCount,
106
+ }
107
+ }
108
+
100
109
  #prune() {
101
110
  while (this.#size > this.#maxSize || this.#count > this.#maxCount) {
102
111
  const e1 = this.#arr[(Math.random() * this.#arr.length) | 0]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/cache",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -30,5 +30,5 @@
30
30
  "tsd": "^0.33.0",
31
31
  "typescript": "^5.9.3"
32
32
  },
33
- "gitHead": "f6592f0be62fecc161237610ae6454ec6585548b"
33
+ "gitHead": "cfc529e921c93a1fb0292018e2026f2b326ec998"
34
34
  }