@resolid/cache 0.1.1 → 1.0.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
@@ -1,17 +1,15 @@
1
- # Resolid: Cache Package
1
+ # Type-safe Async Cache for TypeScript
2
2
 
3
3
  ![GitHub License](https://img.shields.io/github/license/resolid/framework)
4
4
  ![NPM Version](https://img.shields.io/npm/v/%40resolid/cache)
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
8
  A fully-typed, flexible cache system for modern TypeScript projects.
11
9
  Supports single and batch operations, optional TTL, and pluggable storage backends.
12
10
  Designed for libraries, frameworks, and applications needing predictable async caching.
13
11
 
14
- ### Feature
12
+ ## Feature
15
13
 
16
14
  - Fully typed with TypeScript — no `any`.
17
15
  - Supports get/set/del/clear operations.
@@ -21,7 +19,7 @@ Designed for libraries, frameworks, and applications needing predictable async c
21
19
  - Detects existence of keys via `has`.
22
20
  - Handles disposal of resources via `dispose`.
23
21
 
24
- ### Installation
22
+ ## Installation
25
23
 
26
24
  ```shell
27
25
  pnpm add @resolid/cache
@@ -33,12 +31,12 @@ yarn add @resolid/cache
33
31
  bun add @resolid/cache
34
32
  ```
35
33
 
36
- ### Usage
34
+ ## Usage
37
35
 
38
36
  ```js
39
- import { createCache } from "@resolid/cache";
37
+ import { Cacher } from "@resolid/cache";
40
38
 
41
- const cache = createCache({ defaultTtl: 1000 });
39
+ const cache = new Cacher({ defaultTtl: 1000 });
42
40
 
43
41
  // Single set/get
44
42
  await cache.set("foo", { a: 1 });
@@ -62,16 +60,16 @@ await cache.clear();
62
60
  await cache.dispose();
63
61
  ```
64
62
 
65
- ### Options
63
+ ## Options
66
64
 
67
65
  ```ts
68
- export type CreateCacheOptions = {
69
- store?: CacheStore; // Custom storage backend
70
- defaultTtl?: number; // Default TTL in seconds
71
- };
66
+ export interface CacheOptions {
67
+ store?: CacheStore;
68
+ defaultTtl?: number;
69
+ }
72
70
  ```
73
71
 
74
- ### Store Interface
72
+ ## Store Interface
75
73
 
76
74
  Your custom store should implement `CacheStore`:
77
75
 
@@ -0,0 +1,26 @@
1
+ import { t as CacheStore } from "./types-D3cbGkJC.mjs";
2
+
3
+ //#region src/index.d.ts
4
+ interface CacheOptions {
5
+ store?: CacheStore;
6
+ defaultTtl?: number;
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
+ }
25
+ //#endregion
26
+ export { CacheOptions, Cacher };
package/dist/index.mjs ADDED
@@ -0,0 +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};
@@ -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};
@@ -0,0 +1,27 @@
1
+ import { t as CacheStore } from "./types-D3cbGkJC.mjs";
2
+
3
+ //#region src/stores/memory-cache.d.ts
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
+ }
13
+ //#endregion
14
+ //#region src/stores/null-cache.d.ts
15
+ declare class NullCache implements Required<CacheStore> {
16
+ get<T>(_key: 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
+ }
26
+ //#endregion
27
+ export { CacheStore, MemoryCache, NullCache };
@@ -0,0 +1 @@
1
+ import{t as e}from"./null-cache-DGPituGm.mjs";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};
@@ -1,5 +1,5 @@
1
- //#region src/types/index.d.ts
2
- type CacheStore = {
1
+ //#region src/stores/types.d.ts
2
+ interface CacheStore {
3
3
  get: (key: string) => Promise<string | undefined>;
4
4
  set: (key: string, value: string, ttl?: number) => Promise<boolean>;
5
5
  del: (key: string) => Promise<boolean>;
@@ -9,6 +9,6 @@ type CacheStore = {
9
9
  delMultiple?: (keys: string[]) => Promise<boolean>;
10
10
  has?: (key: string) => Promise<boolean>;
11
11
  dispose?: () => Promise<void> | void;
12
- };
12
+ }
13
13
  //#endregion
14
14
  export { CacheStore as t };
package/package.json CHANGED
@@ -1,69 +1,58 @@
1
1
  {
2
2
  "name": "@resolid/cache",
3
- "version": "0.1.1",
3
+ "version": "1.0.0",
4
4
  "private": false,
5
- "description": "The Resolid Cache package.",
5
+ "description": "Type-safe Async Cache for TypeScript",
6
6
  "keywords": [
7
- "resolid",
8
7
  "cache",
9
- "cache manager"
8
+ "cache manager",
9
+ "resolid"
10
10
  ],
11
11
  "homepage": "https://www.resolid.tech",
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/resolid/framework.git",
15
- "directory": "packages/cache"
16
- },
17
12
  "license": "MIT",
18
13
  "author": {
19
14
  "name": "Huijie Wei",
20
15
  "email": "hello@resolid.tech"
21
16
  },
22
- "sideEffects": false,
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/resolid/framework.git",
20
+ "directory": "packages/cache"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
23
25
  "type": "module",
26
+ "sideEffects": false,
27
+ "types": "./dist/index.d.mts",
24
28
  "exports": {
25
29
  ".": {
26
- "types": "./dist/index.d.ts",
27
- "import": "./dist/index.js"
30
+ "types": "./dist/index.d.mts",
31
+ "import": "./dist/index.mjs"
28
32
  },
29
33
  "./stores": {
30
- "types": "./dist/stores.d.ts",
31
- "import": "./dist/stores.js"
32
- },
33
- "./utils": {
34
- "types": "./dist/utils.d.ts",
35
- "import": "./dist/utils.js"
36
- },
37
- "./types": {
38
- "types": "./dist/types.d.ts",
39
- "import": "./dist/types.js"
34
+ "types": "./dist/stores.d.mts",
35
+ "import": "./dist/stores.mjs"
40
36
  }
41
37
  },
42
- "main": "./dist/index.js",
43
- "types": "./dist/index.d.ts",
44
- "files": [
45
- "dist"
46
- ],
38
+ "publishConfig": {
39
+ "access": "public",
40
+ "provenance": true
41
+ },
47
42
  "dependencies": {
48
43
  "destr": "^2.0.5",
49
44
  "quick-lru": "^7.3.0"
50
45
  },
51
46
  "devDependencies": {
52
- "@vitest/coverage-v8": "beta",
53
- "tsdown": "^0.15.9",
54
- "typescript": "^5.9.3",
55
- "vitest": "beta"
47
+ "tsdown": "^0.20.3",
48
+ "typescript": "^5.9.3"
56
49
  },
57
50
  "engines": {
58
- "node": "^20.19.0 || ^22.13.0 || >=24"
59
- },
60
- "publishConfig": {
61
- "access": "public",
62
- "provenance": true
51
+ "node": "^22.13.0 || >=24"
63
52
  },
64
53
  "scripts": {
65
54
  "build": "tsdown",
66
- "lint": "eslint .",
55
+ "lint": "oxlint",
67
56
  "test": "vitest run",
68
57
  "typecheck": "tsc --noEmit"
69
58
  }
package/dist/index.d.ts DELETED
@@ -1,21 +0,0 @@
1
- import { t as CacheStore } from "./index-DvzAtk0z.js";
2
-
3
- //#region src/index.d.ts
4
- type CreateCacheOptions = {
5
- store?: CacheStore;
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;
20
- //#endregion
21
- export { CacheInstance, CreateCacheOptions, createCache };
package/dist/index.js DELETED
@@ -1 +0,0 @@
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 +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};
package/dist/stores.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import { t as CacheStore } from "./index-DvzAtk0z.js";
2
-
3
- //#region src/stores/memory-cache.d.ts
4
- declare const createMemoryCache: (maxSize?: number) => CacheStore;
5
- //#endregion
6
- //#region src/stores/null-cache.d.ts
7
- declare const nullCache: Required<CacheStore>;
8
- //#endregion
9
- export { createMemoryCache, nullCache };
package/dist/stores.js DELETED
@@ -1 +0,0 @@
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};
package/dist/types.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { t as CacheStore } from "./index-DvzAtk0z.js";
2
- export { CacheStore };
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export{};
@@ -1 +0,0 @@
1
- const e=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};export{e as t};
package/dist/utils.d.ts DELETED
@@ -1,4 +0,0 @@
1
- //#region src/utils/index.d.ts
2
- declare const normalizeKey: (key: string) => string;
3
- //#endregion
4
- export { normalizeKey };
package/dist/utils.js DELETED
@@ -1 +0,0 @@
1
- import{t as e}from"./utils-CV58ddJF.js";export{e as normalizeKey};