@nxtedition/cache 1.0.0 → 1.0.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/lib/index.d.ts CHANGED
@@ -21,13 +21,13 @@ export type CacheResult<V> = {
21
21
  value: Promise<V> | null | undefined;
22
22
  async: true;
23
23
  };
24
- export declare class AsyncCache<V = unknown> {
24
+ export declare class AsyncCache<V = unknown, A extends unknown[] = unknown[]> {
25
25
  #private;
26
- constructor(location: string, valueSelector?: (...args: unknown[]) => V | Promise<V>, keySelector?: (...args: unknown[]) => string, opts?: AsyncCacheOptions<V>);
26
+ constructor(location: string, valueSelector?: (...args: A) => V | Promise<V>, keySelector?: (...args: A) => string, opts?: AsyncCacheOptions<V>);
27
27
  close(): void;
28
- get(...args: unknown[]): CacheResult<V>;
29
- peek(...args: unknown[]): CacheResult<V>;
30
- refresh(...args: unknown[]): Promise<V> | undefined;
28
+ get(...args: A): CacheResult<V>;
29
+ peek(...args: A): CacheResult<V>;
30
+ refresh(...args: A): Promise<V> | undefined;
31
31
  delete(key: string): void;
32
32
  set(key: string, value: V): void;
33
33
  purgeStale(): void;
package/lib/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { DatabaseSync, } from 'node:sqlite'
2
2
  import { LRUCache } from 'lru-cache'
3
- import { doYield } from '@nxtedition/yield'
4
3
 
5
4
  let fastNowTime = 0
6
5
 
@@ -44,13 +43,6 @@ const dbs = new Set ()
44
43
 
45
44
 
46
45
 
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
46
 
55
47
 
56
48
 
@@ -70,10 +62,10 @@ const dbs = new Set ()
70
62
  const VERSION = 2
71
63
  const MAX_DURATION = 365000000e3
72
64
 
73
- export class AsyncCache {
65
+ export class AsyncCache {
74
66
  #lru
75
- #valueSelector
76
- #keySelector
67
+ #valueSelector
68
+ #keySelector
77
69
  #dedupe = new Map ()
78
70
 
79
71
  #ttl
@@ -86,12 +78,10 @@ export class AsyncCache {
86
78
  #purgeStaleQuery = null
87
79
  #evictQuery = null
88
80
 
89
- #setQueue = []
90
-
91
81
  constructor(
92
82
  location ,
93
- valueSelector ,
94
- keySelector ,
83
+ valueSelector ,
84
+ keySelector ,
95
85
  opts ,
96
86
  ) {
97
87
  if (typeof location === 'string') {
@@ -107,7 +97,7 @@ export class AsyncCache {
107
97
  }
108
98
 
109
99
  if (typeof keySelector === 'function' || keySelector === undefined) {
110
- this.#keySelector = keySelector ?? ((...args ) => JSON.stringify(args))
100
+ this.#keySelector = keySelector ?? ((...args ) => JSON.stringify(args))
111
101
  } else {
112
102
  throw new TypeError('keySelector must be a function')
113
103
  }
@@ -191,7 +181,6 @@ export class AsyncCache {
191
181
 
192
182
  close() {
193
183
  dbs.delete(this)
194
- this.#setQueue.length = 0
195
184
  this.#getQuery = null
196
185
  this.#setQuery = null
197
186
  this.#delQuery = null
@@ -201,15 +190,15 @@ export class AsyncCache {
201
190
  this.#db = null
202
191
  }
203
192
 
204
- get(...args ) {
193
+ get(...args ) {
205
194
  return this.#load(args, true)
206
195
  }
207
196
 
208
- peek(...args ) {
197
+ peek(...args ) {
209
198
  return this.#load(args, false)
210
199
  }
211
200
 
212
- refresh(...args ) {
201
+ refresh(...args ) {
213
202
  return this.#refresh(args)
214
203
  }
215
204
 
@@ -228,7 +217,7 @@ export class AsyncCache {
228
217
  } catch {}
229
218
  }
230
219
 
231
- #load(args , refresh ) {
220
+ #load(args , refresh ) {
232
221
  const key = this.#keySelector(...args)
233
222
 
234
223
  if (typeof key !== 'string' || key.length === 0) {
@@ -246,16 +235,13 @@ export class AsyncCache {
246
235
  cached = {
247
236
  ttl: row.ttl,
248
237
  stale: row.stale,
249
- value: ArrayBuffer.isView(row.val) ? (row.val ) : JSON.parse(row.val),
250
- }
251
- } else {
252
- const entry = this.#setQueue.findLast((x) => x.key === key)
253
- if (entry !== undefined) {
254
- cached = {
255
- ttl: entry.ttl,
256
- stale: entry.stale,
257
- value: entry.value,
258
- }
238
+ value: ArrayBuffer.isView(row.val)
239
+ ? (Buffer.from(
240
+ row.val.buffer,
241
+ row.val.byteOffset,
242
+ row.val.byteLength,
243
+ ) )
244
+ : JSON.parse(row.val),
259
245
  }
260
246
  }
261
247
 
@@ -293,7 +279,7 @@ export class AsyncCache {
293
279
  }
294
280
 
295
281
  // eslint-disable-next-line: no-unsafe-argument
296
- #refresh(args , key = this.#keySelector(...args)) {
282
+ #refresh(args , key = this.#keySelector(...args)) {
297
283
  if (typeof key !== 'string' || key.length === 0) {
298
284
  throw new TypeError('keySelector must return a non-empty string')
299
285
  }
@@ -345,36 +331,24 @@ export class AsyncCache {
345
331
 
346
332
  this.#lru?.set(key, { ttl, stale, value })
347
333
 
348
- this.#setQueue.push({ key, value, ttl, stale })
349
- if (this.#setQueue.length === 1) {
350
- doYield(this.#flushSetQueue)
351
- }
352
- }
334
+ const data = ArrayBuffer.isView(value)
335
+ ? value
336
+ : JSON.stringify(value )
353
337
 
354
- #flushSetQueue = () => {
355
- for (const { key, value, ttl, stale } of this.#setQueue.splice(0, 64)) {
356
- const data = ArrayBuffer.isView(value)
357
- ? value
358
- : JSON.stringify(value )
359
- try {
360
- this.#setQuery?.run(key, data , ttl, stale)
361
- } catch (err) {
362
- if ((err )?.errcode === 13 /* SQLITE_FULL */) {
363
- try {
364
- this.#evictQuery?.run(256)
365
- this.#setQuery?.run(key, data , ttl, stale)
366
- } catch {
367
- process.emitWarning(err )
368
- }
369
- } else {
338
+ try {
339
+ this.#setQuery?.run(key, data , ttl, stale)
340
+ } catch (err) {
341
+ if ((err )?.errcode === 13 /* SQLITE_FULL */) {
342
+ try {
343
+ this.#evictQuery?.run(256)
344
+ this.#setQuery?.run(key, data , ttl, stale)
345
+ } catch {
370
346
  process.emitWarning(err )
371
347
  }
348
+ } else {
349
+ process.emitWarning(err )
372
350
  }
373
351
  }
374
-
375
- if (this.#setQueue.length > 0) {
376
- doYield(this.#flushSetQueue)
377
- }
378
352
  }
379
353
 
380
354
  #delete(key ) {
@@ -383,11 +357,6 @@ export class AsyncCache {
383
357
  }
384
358
 
385
359
  this.#lru?.delete(key)
386
-
387
- if (this.#setQueue.some((x) => x.key === key)) {
388
- this.#setQueue = this.#setQueue.filter((x) => x.key !== key)
389
- }
390
-
391
360
  try {
392
361
  this.#delQuery?.run(key)
393
362
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/cache",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -23,8 +23,7 @@
23
23
  "typescript": "^5.9.3"
24
24
  },
25
25
  "dependencies": {
26
- "@nxtedition/yield": "^1.0.7",
27
26
  "lru-cache": "^11.2.6"
28
27
  },
29
- "gitHead": "e85766435987febc99140e523cd2d4f5af458892"
28
+ "gitHead": "1833b15a4085a33f85063e3fabf522dfa19c64b7"
30
29
  }