@resolid/cache 1.0.1 → 1.2.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/README.md CHANGED
@@ -12,7 +12,7 @@ Designed for libraries, frameworks, and applications needing predictable async c
12
12
  ## Feature
13
13
 
14
14
  - Fully typed with TypeScript — no `any`.
15
- - Supports get/set/del/clear operations.
15
+ - Supports get/getOrSet/set/del/clear operations.
16
16
  - Supports batch operations: getMultiple, setMultiple, delMultiple.
17
17
  - Optional TTL for automatic expiration.
18
18
  - Pluggable store backend (default is `nullCache`).
@@ -42,6 +42,9 @@ const cache = new Cacher({ defaultTtl: 1000 });
42
42
  await cache.set("foo", { a: 1 });
43
43
  const value = await cache.get("foo"); // -> { a: 1 }
44
44
 
45
+ // Get or set with factory
46
+ const user = await cache.getOrSet("user:1", () => fetchUser(1), 60);
47
+
45
48
  // Check existence
46
49
  const exists = await cache.has("foo"); // -> true
47
50
 
@@ -65,6 +68,11 @@ await cache.dispose();
65
68
  ```ts
66
69
  export interface CacheOptions {
67
70
  store?: CacheStore;
71
+
72
+ // A custom serializer to use when storing and retrieving values from the cache.
73
+ // For example, you could use superjson to serialize and deserialize your values instead of JSON.stringify and JSON.parse.
74
+ serializer?: Serializer;
75
+
68
76
  defaultTtl?: number;
69
77
  }
70
78
  ```
package/dist/index.d.mts CHANGED
@@ -1,19 +1,27 @@
1
1
  import { t as CacheStore } from "./types-BvLwqpdf.mjs";
2
2
 
3
3
  //#region src/index.d.ts
4
+ type Serializer = {
5
+ serialize: <T = unknown>(value: T) => string;
6
+ deserialize: <T = unknown>(value: string) => T;
7
+ };
4
8
  interface CacheOptions {
5
9
  store?: CacheStore;
10
+ serializer?: Serializer;
6
11
  defaultTtl?: number;
7
12
  }
8
13
  declare class Cacher {
9
14
  private readonly _store;
15
+ private readonly _serializer;
10
16
  private readonly _defaultTtl?;
11
17
  constructor({
12
18
  store,
19
+ serializer,
13
20
  defaultTtl
14
21
  }?: CacheOptions);
15
22
  get<T>(key: string, defaultValue?: T): Promise<T | undefined>;
16
23
  set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
24
+ getOrSet<T>(key: string, factory: () => T | Promise<T>, ttl?: number): Promise<T>;
17
25
  del(key: string): Promise<boolean>;
18
26
  clear(): Promise<boolean>;
19
27
  getMultiple<T>(keys: string[], defaultValue?: T): Promise<(T | undefined)[]>;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{t as e}from"./null-cache-DGPituGm.mjs";import{destr as t}from"destr";const n=e=>{let t=e.split(`?`)[0]?.replace(/[/\\]/g,`:`).replace(/:+/g,`:`).replace(/^:|:$/g,``);if(!t)throw Error(`Cache key cannot be empty after normalization`);return t};var r=class{_store;_defaultTtl;constructor({store:t=new e,defaultTtl:n}={}){this._store=t,this._defaultTtl=n}async get(e,r){let i=await this._store.get(n(e));return i===void 0?r:t(i)}set(e,t,r){return this._store.set(n(e),JSON.stringify(t),r??this._defaultTtl)}del(e){return this._store.del(n(e))}clear(){return this._store.clear()}async getMultiple(e,r){return this._store.getMultiple?(await this._store.getMultiple(e.map(n))).map(e=>e===void 0?r:t(e)):Promise.all(e.map(e=>this.get(e,r)))}async setMultiple(e,t){if(this._store.setMultiple){let r=Object.entries(e).reduce((e,[t,r])=>(e[n(t)]=JSON.stringify(r),e),{});return this._store.setMultiple(r,t)}return(await Promise.all(Object.entries(e).map(([e,n])=>this.set(e,n,t)))).every(Boolean)}async delMultiple(e){return this._store.delMultiple?this._store.delMultiple(e.map(n)):(await Promise.all(e.map(e=>this.del(e)))).every(Boolean)}async has(e){return this._store.has?this._store.has(n(e)):await this.get(e)!==void 0}async dispose(){await this._store.dispose?.()}};export{r as Cacher};
1
+ import{t as e}from"./null-cache-DGPituGm.mjs";function t(e){let t=e.split(`?`)[0]?.replace(/[/\\]/g,`:`).replace(/:+/g,`:`).replace(/^:|:$/g,``);if(!t)throw Error(`Cache key cannot be empty after normalization`);return t}var n=class{_store;_serializer;_defaultTtl;constructor({store:t=new e,serializer:n={serialize:JSON.stringify,deserialize:JSON.parse},defaultTtl:r}={}){this._store=t,this._serializer=n,this._defaultTtl=r}async get(e,n){let r=await this._store.get(t(e));return r===void 0?n:this._serializer.deserialize(r)}set(e,n,r){return this._store.set(t(e),this._serializer.serialize(n),r??this._defaultTtl)}async getOrSet(e,t,n){let r=await this.get(e);if(r!==void 0)return r;let i=await t();return await this.set(e,i,n),i}del(e){return this._store.del(t(e))}clear(){return this._store.clear()}async getMultiple(e,n){return this._store.getMultiple?(await this._store.getMultiple(e.map(t))).map(e=>e===void 0?n:this._serializer.deserialize(e)):Promise.all(e.map(e=>this.get(e,n)))}async setMultiple(e,n){return this._store.setMultiple?this._store.setMultiple(Object.entries(e).reduce((e,[n,r])=>(e[t(n)]=this._serializer.serialize(r),e),{}),n):(await Promise.all(Object.entries(e).map(([e,t])=>this.set(e,t,n)))).every(Boolean)}async delMultiple(e){return this._store.delMultiple?this._store.delMultiple(e.map(t)):(await Promise.all(e.map(e=>this.del(e)))).every(Boolean)}async has(e){return this._store.has?this._store.has(t(e)):await this.get(e)!==void 0}async dispose(){await this._store.dispose?.()}};export{n as Cacher};
package/dist/stores.d.mts CHANGED
@@ -24,4 +24,4 @@ declare class NullCache implements Required<CacheStore> {
24
24
  dispose(): Promise<void>;
25
25
  }
26
26
  //#endregion
27
- export { CacheStore, MemoryCache, NullCache };
27
+ export { type CacheStore, MemoryCache, NullCache };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolid/cache",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "private": false,
5
5
  "description": "Type-safe Async Cache for TypeScript",
6
6
  "keywords": [
@@ -41,11 +41,10 @@
41
41
  "provenance": true
42
42
  },
43
43
  "dependencies": {
44
- "destr": "^2.0.5",
45
44
  "quick-lru": "^7.3.0"
46
45
  },
47
46
  "devDependencies": {
48
- "tsdown": "^0.21.5"
47
+ "tsdown": "^0.22.0"
49
48
  },
50
49
  "engines": {
51
50
  "node": "^22.13.0 || >=24"