cachimbo 0.0.4 → 0.0.5

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/dist/index.js CHANGED
@@ -214,61 +214,61 @@ var LocalTTLCache = class extends BaseLocalCache {
214
214
  * Once the limit of items is reached, the first inserted keys will be purged.
215
215
  */
216
216
  var LocalMapCache = class extends BaseLocalCache {
217
- cache;
217
+ map;
218
218
  max;
219
219
  constructor(options = {}) {
220
220
  super(options);
221
- this.cache = options.cache ?? /* @__PURE__ */ new Map();
221
+ this.map = options.map ?? /* @__PURE__ */ new Map();
222
222
  this.max = options.max ?? Infinity;
223
223
  }
224
224
  /** @internal */
225
225
  _get(key) {
226
226
  this.logger?.debug(this.name, "[get]", "key =", key);
227
- const data = this.cache.get(key);
227
+ const data = this.map.get(key);
228
228
  return data === void 0 ? null : data;
229
229
  }
230
230
  /** @internal */
231
231
  _set(key, value, options) {
232
232
  this.logger?.debug(this.name, "[set]", "key =", key);
233
- const previousValue = this.cache.get(key);
234
- if (this.cache.size >= this.max && previousValue === void 0) this.evict(1);
235
- this.cache.set(key, value);
233
+ const previousValue = this.map.get(key);
234
+ if (this.map.size >= this.max && previousValue === void 0) this.evict(1);
235
+ this.map.set(key, value);
236
236
  this.onDispose(key, previousValue, "set");
237
237
  }
238
238
  /** @internal */
239
239
  _delete(key) {
240
240
  this.logger?.debug(this.name, "[delete]", "key =", key);
241
- const previousValue = this.cache.get(key);
242
- this.cache.delete(key);
241
+ const previousValue = this.map.get(key);
242
+ this.map.delete(key);
243
243
  this.onDispose(key, previousValue, "delete");
244
244
  }
245
245
  async setMany(data, options) {
246
246
  this.logger?.debug(this.name, "[setMany]", "data =", data);
247
247
  const entries = Object.entries(data);
248
- const newEntries = entries.filter(([key]) => !this.cache.has(key)).length;
249
- if (this.cache.size + newEntries > this.max) this.evict(this.cache.size + newEntries - this.max);
248
+ const newEntries = entries.filter(([key]) => !this.map.has(key)).length;
249
+ if (this.map.size + newEntries > this.max) this.evict(this.map.size + newEntries - this.max);
250
250
  for (const [key, value] of entries) {
251
- const previousValue = this.cache.get(key);
252
- this.cache.set(key, value);
251
+ const previousValue = this.map.get(key);
252
+ this.map.set(key, value);
253
253
  this.onDispose(key, previousValue, "set");
254
254
  }
255
255
  }
256
256
  clear() {
257
257
  this.logger?.debug(this.name, "[clear]");
258
- for (const key of this.cache.keys()) this.onDispose(key, this.cache.get(key), "delete");
259
- this.cache.clear();
258
+ for (const key of this.map.keys()) this.onDispose(key, this.map.get(key), "delete");
259
+ this.map.clear();
260
260
  }
261
261
  onDispose(key, value, reason) {
262
262
  if (value !== void 0) super.onDispose(key, value, reason);
263
263
  }
264
264
  evict(length) {
265
- const keys = this.cache.keys();
265
+ const keys = this.map.keys();
266
266
  for (let i = 0; i < length; i++) {
267
267
  const key = keys.next();
268
268
  if (key.done) break;
269
269
  this.logger?.debug(this.name, "[evict]", "key = ", key);
270
- const previousValue = this.cache.get(key.value);
271
- this.cache.delete(key.value);
270
+ const previousValue = this.map.get(key.value);
271
+ this.map.delete(key.value);
272
272
  this.onDispose(key.value, previousValue, "evict");
273
273
  }
274
274
  }
@@ -387,6 +387,56 @@ var WeakCache = class extends BaseLocalCache {
387
387
  }
388
388
  };
389
389
 
390
+ //#endregion
391
+ //#region src/local/cloning/index.ts
392
+ /**
393
+ * A cache layer that deep clones data when reading and writing.
394
+ *
395
+ * This is useful when you mutate the objects retrieved from cache,
396
+ * and you don't want them to also change in cache.
397
+ *
398
+ * Do not use this layer if you do not intend to mutate cached objects,
399
+ * as the cloning process adds unnecessary overhead.
400
+ */
401
+ var DeepCloningCache = class extends BaseLocalCache {
402
+ cache;
403
+ cacheInternal;
404
+ deepClone;
405
+ constructor(options) {
406
+ super(options);
407
+ this.cache = options.cache;
408
+ this.cacheInternal = options.cache.internal;
409
+ if (options.deepClone) this.deepClone = options.deepClone;
410
+ else if (typeof structuredClone === "function") this.deepClone = structuredClone;
411
+ else this.deepClone = (data) => JSON.parse(JSON.stringify(data));
412
+ }
413
+ _get(key) {
414
+ return this.deepClone(this.cacheInternal._get(key));
415
+ }
416
+ _set(key, value, options) {
417
+ this.cacheInternal._set(key, this.deepClone(value), options);
418
+ }
419
+ _delete(key) {
420
+ this.cacheInternal._delete(key);
421
+ }
422
+ _getMany(keys) {
423
+ return this.deepClone(this.cacheInternal._getMany(keys));
424
+ }
425
+ _setMany(data, options) {
426
+ this.cacheInternal._setMany(this.deepClone(data), options);
427
+ }
428
+ _deleteMany(keys) {
429
+ this.cacheInternal._deleteMany(keys);
430
+ }
431
+ _addDisposeListener(listener) {
432
+ this.cacheInternal._addDisposeListener(listener);
433
+ }
434
+ async getOrLoad(key, load, options) {
435
+ const loadWrapped = async () => this.deepClone(await load());
436
+ return this.deepClone(await this.cache.getOrLoad(key, loadWrapped, options));
437
+ }
438
+ };
439
+
390
440
  //#endregion
391
441
  //#region src/local/noop/index.ts
392
442
  /**
@@ -987,7 +1037,7 @@ var KeyTransformingCache = class {
987
1037
  transform;
988
1038
  constructor(options) {
989
1039
  this.cache = options.cache;
990
- if ("transform" in options) this.transform = options.transform;
1040
+ if ("transform" in options && typeof options.transform === "function") this.transform = options.transform;
991
1041
  else {
992
1042
  const prefix = options.prefix || "";
993
1043
  const suffix = options.suffix || "";
@@ -1340,5 +1390,5 @@ var MetricsCollectingCache = class {
1340
1390
  };
1341
1391
 
1342
1392
  //#endregion
1343
- export { AsyncLazyCache, BaseCache, BaseLocalCache, CoalescingCache, IORedisCache, JitteringCache, KeyTransformingCache, KeyvCache, LocalLRUCache, LocalMapCache, LocalTTLCache, MemJSCache, MemcacheCache, MetricsCollectingCache, NoOpCache, RedisCache, SWRCache, TieredCache, ValkeyGlideCache, WeakCache, WorkersKVCache };
1393
+ export { AsyncLazyCache, BaseCache, BaseLocalCache, CoalescingCache, DeepCloningCache, IORedisCache, JitteringCache, KeyTransformingCache, KeyvCache, LocalLRUCache, LocalMapCache, LocalTTLCache, MemJSCache, MemcacheCache, MetricsCollectingCache, NoOpCache, RedisCache, SWRCache, TieredCache, ValkeyGlideCache, WeakCache, WorkersKVCache };
1344
1394
  //# sourceMappingURL=index.js.map