cacheable 1.7.0 → 1.7.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/README.md CHANGED
@@ -261,13 +261,20 @@ By default we use lazy expiration deletion which means on `get` and `getMany` ty
261
261
  ### CacheableMemory API
262
262
 
263
263
  * `set(key, value, ttl?)`: Sets a value in the cache.
264
+ * `setMany([{key, value, ttl?}])`: Sets multiple values in the cache from `CachableItem`.
264
265
  * `get(key)`: Gets a value from the cache.
266
+ * `getMany([keys])`: Gets multiple values from the cache.
267
+ * `getRaw(key)`: Gets a value from the cache as `CacheableStoreItem`.
268
+ * `getManyRaw([keys])`: Gets multiple values from the cache as `CacheableStoreItem`.
265
269
  * `has(key)`: Checks if a value exists in the cache.
266
270
  * `delete(key)`: Deletes a value from the cache.
271
+ * `deleteMany([keys])`: Deletes multiple values from the cache.
272
+ * `take(key)`: Takes a value from the cache and deletes it.
273
+ * `takeMany([keys])`: Takes multiple values from the cache and deletes them.
267
274
  * `clear()`: Clears the cache.
268
275
  * `size()`: The number of keys in the cache.
269
276
  * `keys()`: The keys in the cache.
270
- * `items()`: The items in the cache as `{ key, value, expires? }`.
277
+ * `items()`: The items in the cache as `CacheableStoreItem` example `{ key, value, expires? }`.
271
278
  * `checkExpired()`: Checks for expired keys in the cache. This is used by the `checkInterval` property.
272
279
  * `startIntervalCheck()`: Starts the interval check for expired keys if `checkInterval` is above 0 ms.
273
280
  * `stopIntervalCheck()`: Stops the interval check for expired keys.
package/dist/index.cjs CHANGED
@@ -262,6 +262,13 @@ var CacheableMemory = class {
262
262
  }
263
263
  return this.clone(item.value);
264
264
  }
265
+ getMany(keys) {
266
+ const result = new Array();
267
+ for (const key of keys) {
268
+ result.push(this.get(key));
269
+ }
270
+ return result;
271
+ }
265
272
  getRaw(key) {
266
273
  const store = this.getStore(key);
267
274
  const item = store.get(key);
@@ -275,6 +282,13 @@ var CacheableMemory = class {
275
282
  this.lruMoveToFront(key);
276
283
  return item;
277
284
  }
285
+ getManyRaw(keys) {
286
+ const result = new Array();
287
+ for (const key of keys) {
288
+ result.push(this.getRaw(key));
289
+ }
290
+ return result;
291
+ }
278
292
  set(key, value, ttl) {
279
293
  const store = this.getStore(key);
280
294
  let expires;
@@ -305,6 +319,11 @@ var CacheableMemory = class {
305
319
  expires
306
320
  });
307
321
  }
322
+ setMany(items) {
323
+ for (const item of items) {
324
+ this.set(item.key, item.value, item.ttl);
325
+ }
326
+ }
308
327
  has(key) {
309
328
  const item = this.get(key);
310
329
  return Boolean(item);
@@ -317,10 +336,22 @@ var CacheableMemory = class {
317
336
  this.delete(key);
318
337
  return item;
319
338
  }
339
+ takeMany(keys) {
340
+ const result = new Array();
341
+ for (const key of keys) {
342
+ result.push(this.take(key));
343
+ }
344
+ return result;
345
+ }
320
346
  delete(key) {
321
347
  const store = this.getStore(key);
322
348
  store.delete(key);
323
349
  }
350
+ deleteMany(keys) {
351
+ for (const key of keys) {
352
+ this.delete(key);
353
+ }
354
+ }
324
355
  clear() {
325
356
  this._hash0.clear();
326
357
  this._hash1.clear();
@@ -483,32 +514,30 @@ var KeyvCacheableMemory = class {
483
514
  }
484
515
  return void 0;
485
516
  }
486
- set(key, value, ttl) {
517
+ async getMany(keys) {
518
+ const result = this._cache.getMany(keys);
519
+ return result;
520
+ }
521
+ async set(key, value, ttl) {
487
522
  this._cache.set(key, value, ttl);
488
523
  }
524
+ async setMany(values) {
525
+ this._cache.setMany(values);
526
+ }
489
527
  async delete(key) {
490
528
  this._cache.delete(key);
491
529
  return true;
492
530
  }
531
+ async deleteMany(key) {
532
+ this._cache.deleteMany(key);
533
+ return true;
534
+ }
493
535
  async clear() {
494
536
  this._cache.clear();
495
537
  }
496
538
  async has(key) {
497
539
  return this._cache.has(key);
498
540
  }
499
- async getMany(keys) {
500
- const result = [];
501
- for (const key of keys) {
502
- result.push(this._cache.get(key));
503
- }
504
- return result;
505
- }
506
- async deleteMany(key) {
507
- for (const k of key) {
508
- this._cache.delete(k);
509
- }
510
- return true;
511
- }
512
541
  on(event, listener) {
513
542
  return this;
514
543
  }
package/dist/index.d.cts CHANGED
@@ -46,17 +46,23 @@ declare class CacheableStats {
46
46
  resetStoreValues(): void;
47
47
  }
48
48
 
49
- type CacheableMemoryOptions = {
49
+ type CacheableItem = {
50
+ key: string;
51
+ value: any;
50
52
  ttl?: number | string;
51
- useClone?: boolean;
52
- lruSize?: number;
53
- checkInterval?: number;
54
53
  };
55
- type CacheableItem$1 = {
54
+ type CacheableStoreItem = {
56
55
  key: string;
57
56
  value: any;
58
57
  expires?: number;
59
58
  };
59
+
60
+ type CacheableMemoryOptions = {
61
+ ttl?: number | string;
62
+ useClone?: boolean;
63
+ lruSize?: number;
64
+ checkInterval?: number;
65
+ };
60
66
  declare class CacheableMemory {
61
67
  private readonly _hashCache;
62
68
  private readonly _hash0;
@@ -86,13 +92,18 @@ declare class CacheableMemory {
86
92
  set checkInterval(value: number);
87
93
  get size(): number;
88
94
  get keys(): IterableIterator<string>;
89
- get items(): IterableIterator<CacheableItem$1>;
95
+ get items(): IterableIterator<CacheableStoreItem>;
90
96
  get<T>(key: string): any;
91
- getRaw(key: string): CacheableItem$1 | undefined;
97
+ getMany<T>(keys: string[]): any[];
98
+ getRaw(key: string): CacheableStoreItem | undefined;
99
+ getManyRaw(keys: string[]): Array<CacheableStoreItem | undefined>;
92
100
  set(key: string, value: any, ttl?: number | string): void;
101
+ setMany(items: CacheableItem[]): void;
93
102
  has(key: string): boolean;
94
103
  take<T>(key: string): any;
104
+ takeMany<T>(keys: string[]): any[];
95
105
  delete(key: string): void;
106
+ deleteMany(keys: string[]): void;
96
107
  clear(): void;
97
108
  hashKey(key: string): number;
98
109
  getStore(key: string): Map<string, any>;
@@ -114,12 +125,17 @@ declare class KeyvCacheableMemory implements KeyvStoreAdapter {
114
125
  private readonly _cache;
115
126
  constructor(options?: CacheableMemoryOptions);
116
127
  get<Value>(key: string): Promise<StoredData<Value> | undefined>;
117
- set(key: string, value: any, ttl?: number): void;
128
+ getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
129
+ set(key: string, value: any, ttl?: number): Promise<void>;
130
+ setMany(values: Array<{
131
+ key: string;
132
+ value: any;
133
+ ttl?: number;
134
+ }>): Promise<void>;
118
135
  delete(key: string): Promise<boolean>;
136
+ deleteMany?(key: string[]): Promise<boolean>;
119
137
  clear(): Promise<void>;
120
138
  has?(key: string): Promise<boolean>;
121
- getMany?<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
122
- deleteMany?(key: string[]): Promise<boolean>;
123
139
  on(event: string, listener: (...arguments_: any[]) => void): this;
124
140
  }
125
141
 
@@ -139,11 +155,6 @@ declare enum CacheableHooks {
139
155
  declare enum CacheableEvents {
140
156
  ERROR = "error"
141
157
  }
142
- type CacheableItem = {
143
- key: string;
144
- value: unknown;
145
- ttl?: number | string;
146
- };
147
158
  type CacheableOptions = {
148
159
  primary?: Keyv | KeyvStoreAdapter;
149
160
  secondary?: Keyv | KeyvStoreAdapter;
package/dist/index.d.ts CHANGED
@@ -46,17 +46,23 @@ declare class CacheableStats {
46
46
  resetStoreValues(): void;
47
47
  }
48
48
 
49
- type CacheableMemoryOptions = {
49
+ type CacheableItem = {
50
+ key: string;
51
+ value: any;
50
52
  ttl?: number | string;
51
- useClone?: boolean;
52
- lruSize?: number;
53
- checkInterval?: number;
54
53
  };
55
- type CacheableItem$1 = {
54
+ type CacheableStoreItem = {
56
55
  key: string;
57
56
  value: any;
58
57
  expires?: number;
59
58
  };
59
+
60
+ type CacheableMemoryOptions = {
61
+ ttl?: number | string;
62
+ useClone?: boolean;
63
+ lruSize?: number;
64
+ checkInterval?: number;
65
+ };
60
66
  declare class CacheableMemory {
61
67
  private readonly _hashCache;
62
68
  private readonly _hash0;
@@ -86,13 +92,18 @@ declare class CacheableMemory {
86
92
  set checkInterval(value: number);
87
93
  get size(): number;
88
94
  get keys(): IterableIterator<string>;
89
- get items(): IterableIterator<CacheableItem$1>;
95
+ get items(): IterableIterator<CacheableStoreItem>;
90
96
  get<T>(key: string): any;
91
- getRaw(key: string): CacheableItem$1 | undefined;
97
+ getMany<T>(keys: string[]): any[];
98
+ getRaw(key: string): CacheableStoreItem | undefined;
99
+ getManyRaw(keys: string[]): Array<CacheableStoreItem | undefined>;
92
100
  set(key: string, value: any, ttl?: number | string): void;
101
+ setMany(items: CacheableItem[]): void;
93
102
  has(key: string): boolean;
94
103
  take<T>(key: string): any;
104
+ takeMany<T>(keys: string[]): any[];
95
105
  delete(key: string): void;
106
+ deleteMany(keys: string[]): void;
96
107
  clear(): void;
97
108
  hashKey(key: string): number;
98
109
  getStore(key: string): Map<string, any>;
@@ -114,12 +125,17 @@ declare class KeyvCacheableMemory implements KeyvStoreAdapter {
114
125
  private readonly _cache;
115
126
  constructor(options?: CacheableMemoryOptions);
116
127
  get<Value>(key: string): Promise<StoredData<Value> | undefined>;
117
- set(key: string, value: any, ttl?: number): void;
128
+ getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
129
+ set(key: string, value: any, ttl?: number): Promise<void>;
130
+ setMany(values: Array<{
131
+ key: string;
132
+ value: any;
133
+ ttl?: number;
134
+ }>): Promise<void>;
118
135
  delete(key: string): Promise<boolean>;
136
+ deleteMany?(key: string[]): Promise<boolean>;
119
137
  clear(): Promise<void>;
120
138
  has?(key: string): Promise<boolean>;
121
- getMany?<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
122
- deleteMany?(key: string[]): Promise<boolean>;
123
139
  on(event: string, listener: (...arguments_: any[]) => void): this;
124
140
  }
125
141
 
@@ -139,11 +155,6 @@ declare enum CacheableHooks {
139
155
  declare enum CacheableEvents {
140
156
  ERROR = "error"
141
157
  }
142
- type CacheableItem = {
143
- key: string;
144
- value: unknown;
145
- ttl?: number | string;
146
- };
147
158
  type CacheableOptions = {
148
159
  primary?: Keyv | KeyvStoreAdapter;
149
160
  secondary?: Keyv | KeyvStoreAdapter;
package/dist/index.js CHANGED
@@ -231,6 +231,13 @@ var CacheableMemory = class {
231
231
  }
232
232
  return this.clone(item.value);
233
233
  }
234
+ getMany(keys) {
235
+ const result = new Array();
236
+ for (const key of keys) {
237
+ result.push(this.get(key));
238
+ }
239
+ return result;
240
+ }
234
241
  getRaw(key) {
235
242
  const store = this.getStore(key);
236
243
  const item = store.get(key);
@@ -244,6 +251,13 @@ var CacheableMemory = class {
244
251
  this.lruMoveToFront(key);
245
252
  return item;
246
253
  }
254
+ getManyRaw(keys) {
255
+ const result = new Array();
256
+ for (const key of keys) {
257
+ result.push(this.getRaw(key));
258
+ }
259
+ return result;
260
+ }
247
261
  set(key, value, ttl) {
248
262
  const store = this.getStore(key);
249
263
  let expires;
@@ -274,6 +288,11 @@ var CacheableMemory = class {
274
288
  expires
275
289
  });
276
290
  }
291
+ setMany(items) {
292
+ for (const item of items) {
293
+ this.set(item.key, item.value, item.ttl);
294
+ }
295
+ }
277
296
  has(key) {
278
297
  const item = this.get(key);
279
298
  return Boolean(item);
@@ -286,10 +305,22 @@ var CacheableMemory = class {
286
305
  this.delete(key);
287
306
  return item;
288
307
  }
308
+ takeMany(keys) {
309
+ const result = new Array();
310
+ for (const key of keys) {
311
+ result.push(this.take(key));
312
+ }
313
+ return result;
314
+ }
289
315
  delete(key) {
290
316
  const store = this.getStore(key);
291
317
  store.delete(key);
292
318
  }
319
+ deleteMany(keys) {
320
+ for (const key of keys) {
321
+ this.delete(key);
322
+ }
323
+ }
293
324
  clear() {
294
325
  this._hash0.clear();
295
326
  this._hash1.clear();
@@ -452,32 +483,30 @@ var KeyvCacheableMemory = class {
452
483
  }
453
484
  return void 0;
454
485
  }
455
- set(key, value, ttl) {
486
+ async getMany(keys) {
487
+ const result = this._cache.getMany(keys);
488
+ return result;
489
+ }
490
+ async set(key, value, ttl) {
456
491
  this._cache.set(key, value, ttl);
457
492
  }
493
+ async setMany(values) {
494
+ this._cache.setMany(values);
495
+ }
458
496
  async delete(key) {
459
497
  this._cache.delete(key);
460
498
  return true;
461
499
  }
500
+ async deleteMany(key) {
501
+ this._cache.deleteMany(key);
502
+ return true;
503
+ }
462
504
  async clear() {
463
505
  this._cache.clear();
464
506
  }
465
507
  async has(key) {
466
508
  return this._cache.has(key);
467
509
  }
468
- async getMany(keys) {
469
- const result = [];
470
- for (const key of keys) {
471
- result.push(this._cache.get(key));
472
- }
473
- return result;
474
- }
475
- async deleteMany(key) {
476
- for (const k of key) {
477
- this._cache.delete(k);
478
- }
479
- return true;
480
- }
481
510
  on(event, listener) {
482
511
  return this;
483
512
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cacheable",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Simple Caching Engine using Keyv",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",