@sebspark/promise-cache 3.7.0 → 3.8.0

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.d.mts CHANGED
@@ -875,7 +875,7 @@ declare namespace time {
875
875
  }
876
876
 
877
877
  declare const serialize: <T>(data: T) => string;
878
- declare const deserialize: <T>(serialized: string) => T;
878
+ declare const deserialize: <T>(serialized: string | null) => T | null;
879
879
 
880
880
  declare const serializer_deserialize: typeof deserialize;
881
881
  declare const serializer_serialize: typeof serialize;
package/dist/index.d.ts CHANGED
@@ -875,7 +875,7 @@ declare namespace time {
875
875
  }
876
876
 
877
877
  declare const serialize: <T>(data: T) => string;
878
- declare const deserialize: <T>(serialized: string) => T;
878
+ declare const deserialize: <T>(serialized: string | null) => T | null;
879
879
 
880
880
  declare const serializer_deserialize: typeof deserialize;
881
881
  declare const serializer_serialize: typeof serialize;
package/dist/index.js CHANGED
@@ -409,9 +409,9 @@ var createCache = (persistor, prefix) => {
409
409
  }
410
410
  const resultPromise = (async () => {
411
411
  try {
412
- const cached = await persistor.get(key);
412
+ const cached = deserialize(await persistor.get(key));
413
413
  if (cached !== null) {
414
- return deserialize(cached);
414
+ return cached;
415
415
  }
416
416
  const result = await delegate(...args);
417
417
  const expiry = typeof options.expiry === "function" ? options.expiry(args, result) : options.expiry;
@@ -469,6 +469,12 @@ var InMemoryPersistor = class {
469
469
  * @returns {Promise<'OK' | null>} Resolves to `'OK'` on success, or `null` if a conditional set (`NX`) fails.
470
470
  */
471
471
  async set(key, value, options) {
472
+ if (options?.NX && this.store.has(key)) {
473
+ return null;
474
+ }
475
+ if (options?.XX && !this.store.has(key)) {
476
+ return null;
477
+ }
472
478
  this.store.set(key, value);
473
479
  if (options?.EX !== void 0) {
474
480
  this.setExpiration(key, options.EX * 1e3);
package/dist/index.mjs CHANGED
@@ -390,9 +390,9 @@ var createCache = (persistor, prefix) => {
390
390
  }
391
391
  const resultPromise = (async () => {
392
392
  try {
393
- const cached = await persistor.get(key);
393
+ const cached = deserialize(await persistor.get(key));
394
394
  if (cached !== null) {
395
- return deserialize(cached);
395
+ return cached;
396
396
  }
397
397
  const result = await delegate(...args);
398
398
  const expiry = typeof options.expiry === "function" ? options.expiry(args, result) : options.expiry;
@@ -450,6 +450,12 @@ var InMemoryPersistor = class {
450
450
  * @returns {Promise<'OK' | null>} Resolves to `'OK'` on success, or `null` if a conditional set (`NX`) fails.
451
451
  */
452
452
  async set(key, value, options) {
453
+ if (options?.NX && this.store.has(key)) {
454
+ return null;
455
+ }
456
+ if (options?.XX && !this.store.has(key)) {
457
+ return null;
458
+ }
453
459
  this.store.set(key, value);
454
460
  if (options?.EX !== void 0) {
455
461
  this.setExpiration(key, options.EX * 1e3);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "3.7.0",
3
+ "version": "3.8.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",