@resolid/cache 0.1.0 → 0.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
@@ -5,6 +5,91 @@
5
5
 
6
6
  <b>[Documentation](https://www.resolid.tech/docs/cache)</b> | [Framework Bundle](https://github.com/resolid/framework)
7
7
 
8
+ ## Type-safe Async Cache for TypeScript
9
+
10
+ A fully-typed, flexible cache system for modern TypeScript projects.
11
+ Supports single and batch operations, optional TTL, and pluggable storage backends.
12
+ Designed for libraries, frameworks, and applications needing predictable async caching.
13
+
14
+ ### Feature
15
+
16
+ - Fully typed with TypeScript — no `any`.
17
+ - Supports get/set/del/clear operations.
18
+ - Supports batch operations: getMultiple, setMultiple, delMultiple.
19
+ - Optional TTL for automatic expiration.
20
+ - Pluggable store backend (default is `nullCache`).
21
+ - Detects existence of keys via `has`.
22
+ - Handles disposal of resources via `dispose`.
23
+
24
+ ### Installation
25
+
26
+ ```shell
27
+ pnpm add @resolid/cache
28
+ # or
29
+ npm install @resolid/cache
30
+ # or
31
+ yarn add @resolid/cache
32
+ # or
33
+ bun add @resolid/cache
34
+ ```
35
+
36
+ ### Usage
37
+
38
+ ```js
39
+ import { Cacher } from "@resolid/cache";
40
+
41
+ const cache = new Cacher({ defaultTtl: 1000 });
42
+
43
+ // Single set/get
44
+ await cache.set("foo", { a: 1 });
45
+ const value = await cache.get("foo"); // -> { a: 1 }
46
+
47
+ // Check existence
48
+ const exists = await cache.has("foo"); // -> true
49
+
50
+ // Batch set/get
51
+ await cache.setMultiple({ key1: 1, key2: 2 });
52
+ const values = await cache.getMultiple(["key1", "key2"]); // -> [1, 2]
53
+
54
+ // Delete
55
+ await cache.del("foo");
56
+ await cache.delMultiple(["key1", "key2"]);
57
+
58
+ // Clear all
59
+ await cache.clear();
60
+
61
+ // Dispose store if necessary
62
+ await cache.dispose();
63
+ ```
64
+
65
+ ### Options
66
+
67
+ ```ts
68
+ export interface CacheOptions {
69
+ store?: CacheStore;
70
+ defaultTtl?: number;
71
+ }
72
+ ```
73
+
74
+ ### Store Interface
75
+
76
+ Your custom store should implement `CacheStore`:
77
+
78
+ ```ts
79
+ export interface CacheStore {
80
+ get(key: string): Promise<string | undefined>;
81
+ set(key: string, value: string, ttl?: number): Promise<boolean>;
82
+ del(key: string): Promise<boolean>;
83
+ clear(): Promise<boolean>;
84
+
85
+ getMultiple?(keys: string[]): Promise<(string | undefined)[]>;
86
+ setMultiple?(values: Record<string, string>, ttl?: number): Promise<boolean>;
87
+ delMultiple?(keys: string[]): Promise<boolean>;
88
+ has?(key: string): Promise<boolean>;
89
+ dispose?(): Promise<void> | void;
90
+ }
91
+ ```
92
+
8
93
  ## License
9
94
 
10
95
  MIT License (MIT). Please see [LICENSE](./LICENSE) for more information.
package/dist/index.d.ts CHANGED
@@ -1,21 +1,26 @@
1
1
  import { t as CacheStore } from "./index-DvzAtk0z.js";
2
2
 
3
3
  //#region src/index.d.ts
4
- type CreateCacheOptions = {
4
+ interface CacheOptions {
5
5
  store?: CacheStore;
6
6
  defaultTtl?: number;
7
- };
8
- type CacheInstance = {
9
- get: <T>(key: string, defaultValue?: T) => Promise<T | undefined>;
10
- set: <T>(key: string, value: T, ttl?: number) => Promise<boolean>;
11
- del: (key: string) => Promise<boolean>;
12
- clear: () => Promise<boolean>;
13
- getMultiple: <T>(keys: string[], defaultValue?: T) => Promise<(T | undefined)[]>;
14
- setMultiple: <T>(values: Record<string, T>, ttl?: number) => Promise<boolean>;
15
- delMultiple: (keys: string[]) => Promise<boolean>;
16
- has: (key: string) => Promise<boolean>;
17
- dispose: () => Promise<void> | void;
18
- };
19
- declare const createCache: (options?: CreateCacheOptions) => CacheInstance;
7
+ }
8
+ declare class Cacher {
9
+ private readonly store;
10
+ private readonly defaultTtl?;
11
+ constructor({
12
+ store,
13
+ defaultTtl
14
+ }?: CacheOptions);
15
+ get<T>(key: string, defaultValue?: T): Promise<T | undefined>;
16
+ set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
17
+ del(key: string): Promise<boolean>;
18
+ clear(): Promise<boolean>;
19
+ getMultiple<T>(keys: string[], defaultValue?: T): Promise<(T | undefined)[]>;
20
+ setMultiple<T>(values: Record<string, T>, ttl?: number): Promise<boolean>;
21
+ delMultiple(keys: string[]): Promise<boolean>;
22
+ has(key: string): Promise<boolean>;
23
+ dispose(): Promise<void>;
24
+ }
20
25
  //#endregion
21
- export { CacheInstance, CreateCacheOptions, createCache };
26
+ export { CacheOptions, Cacher };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{t as e}from"./null-cache-2iHFUXTp.js";import{t}from"./utils-CV58ddJF.js";import{destr as n}from"destr";const r=(r={})=>{let{defaultTtl:i,store:a=e}=r,o=async(e,r)=>{let i=await a.get(t(e));return i===void 0?r:n(i)},s=(e,n,r)=>a.set(t(e),JSON.stringify(n),r??i),c=e=>a.del(t(e));return{get:o,set:s,del:c,clear:()=>a.clear(),getMultiple:(e,r)=>typeof a.getMultiple==`function`?a.getMultiple(e.map(e=>t(e))).then(e=>e.map(e=>e===void 0?r:n(e))):Promise.all(e.map(e=>o(e,r))),setMultiple:(e,n)=>typeof a.setMultiple==`function`?a.setMultiple(Object.entries(e).reduce((e,[n,r])=>(e[t(n)]=JSON.stringify(r),e),{}),n):Promise.all(Object.entries(e).map(([e,t])=>s(e,t,n))).then(e=>e.every(Boolean)),delMultiple:e=>typeof a.delMultiple==`function`?a.delMultiple(e.map(e=>t(e))):Promise.all(e.map(c)).then(e=>e.every(Boolean)),has:async e=>typeof a.has==`function`?a.has(t(e)):o(e).then(e=>e!==void 0),dispose:async()=>{a.dispose&&await a.dispose()}}};export{r as createCache};
1
+ import{t as e}from"./null-cache-95ZKDSid.js";import{t}from"./utils-CV58ddJF.js";import{destr as n}from"destr";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(t(e));return i===void 0?r:n(i)}set(e,n,r){return this.store.set(t(e),JSON.stringify(n),r??this.defaultTtl)}del(e){return this.store.del(t(e))}clear(){return this.store.clear()}async getMultiple(e,r){return this.store.getMultiple?(await this.store.getMultiple(e.map(t))).map(e=>e===void 0?r:n(e)):Promise.all(e.map(e=>this.get(e,r)))}async setMultiple(e,n){if(this.store.setMultiple){let r=Object.entries(e).reduce((e,[n,r])=>(e[t(n)]=JSON.stringify(r),e),{});return this.store.setMultiple(r,n)}return(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{r as Cacher};
@@ -0,0 +1 @@
1
+ var e=class{async get(e){}async set(e,t,n){return!0}async del(e){return!0}async clear(){return!0}async getMultiple(e){return e.map(()=>void 0)}async setMultiple(e,t){return!0}async delMultiple(e){return!0}async has(e){return!1}async dispose(){}};export{e as t};
package/dist/stores.d.ts CHANGED
@@ -1,9 +1,27 @@
1
1
  import { t as CacheStore } from "./index-DvzAtk0z.js";
2
2
 
3
3
  //#region src/stores/memory-cache.d.ts
4
- declare const createMemoryCache: (maxSize?: number) => CacheStore;
4
+ declare class MemoryCache implements CacheStore {
5
+ private readonly lru;
6
+ constructor(maxSize?: number);
7
+ get(key: string): Promise<string | undefined>;
8
+ set(key: string, value: string, ttl?: number): Promise<boolean>;
9
+ del(key: string): Promise<boolean>;
10
+ clear(): Promise<boolean>;
11
+ dispose(): Promise<void>;
12
+ }
5
13
  //#endregion
6
14
  //#region src/stores/null-cache.d.ts
7
- declare const nullCache: Required<CacheStore>;
15
+ declare class NullCache implements Required<CacheStore> {
16
+ get<T>(_: string): Promise<T>;
17
+ set(_key: string, _value: string, _ttl?: number): Promise<boolean>;
18
+ del(_key: string): Promise<boolean>;
19
+ clear(): Promise<boolean>;
20
+ getMultiple<T>(_keys: string[]): Promise<(T | undefined)[]>;
21
+ setMultiple(_values: Record<string, string>, _ttl?: number): Promise<boolean>;
22
+ delMultiple(_keys: string[]): Promise<boolean>;
23
+ has(_key: string): Promise<boolean>;
24
+ dispose(): Promise<void>;
25
+ }
8
26
  //#endregion
9
- export { createMemoryCache, nullCache };
27
+ export { MemoryCache, NullCache };
package/dist/stores.js CHANGED
@@ -1 +1 @@
1
- import{t as e}from"./null-cache-2iHFUXTp.js";import t from"quick-lru";const n=(e=1e3)=>{let n=new t({maxSize:e});return{get:async e=>n.get(e),set:async(e,t,r)=>(n.set(e,t,r?{maxAge:r*1e3}:void 0),!0),del:async e=>n.delete(e),clear:async()=>(n.clear(),!0),dispose:async()=>{n.clear()}}};export{n as createMemoryCache,e as nullCache};
1
+ import{t as e}from"./null-cache-95ZKDSid.js";import t from"quick-lru";var n=class{lru;constructor(e=1e3){this.lru=new t({maxSize:e})}async get(e){return this.lru.get(e)}async set(e,t,n){return this.lru.set(e,t,n?{maxAge:n*1e3}:void 0),!0}async del(e){return this.lru.delete(e)}async clear(){return this.lru.clear(),!0}async dispose(){this.lru.clear()}};export{n as MemoryCache,e as NullCache};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolid/cache",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "description": "The Resolid Cache package.",
6
6
  "keywords": [
@@ -44,21 +44,15 @@
44
44
  "files": [
45
45
  "dist"
46
46
  ],
47
- "scripts": {
48
- "build": "tsdown",
49
- "lint": "eslint .",
50
- "test": "vitest run",
51
- "typecheck": "tsc --noEmit"
52
- },
53
47
  "dependencies": {
54
48
  "destr": "^2.0.5",
55
49
  "quick-lru": "^7.3.0"
56
50
  },
57
51
  "devDependencies": {
58
- "@vitest/coverage-v8": "beta",
59
- "tsdown": "^0.15.9",
52
+ "@vitest/coverage-v8": "^4.0.3",
53
+ "tsdown": "^0.15.10",
60
54
  "typescript": "^5.9.3",
61
- "vitest": "beta"
55
+ "vitest": "^4.0.3"
62
56
  },
63
57
  "engines": {
64
58
  "node": "^20.19.0 || ^22.13.0 || >=24"
@@ -66,5 +60,11 @@
66
60
  "publishConfig": {
67
61
  "access": "public",
68
62
  "provenance": true
63
+ },
64
+ "scripts": {
65
+ "build": "tsdown",
66
+ "lint": "eslint .",
67
+ "test": "vitest run",
68
+ "typecheck": "tsc --noEmit"
69
69
  }
70
- }
70
+ }
@@ -1 +0,0 @@
1
- const e={get:async()=>{},set:async()=>!0,del:async()=>!0,clear:async()=>!0,getMultiple:async e=>e.map(()=>void 0),setMultiple:async()=>!0,delMultiple:async()=>!0,has:async()=>!1,dispose:async()=>{}};export{e as t};