@resolid/cache 1.2.0 → 1.2.1
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
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
import { t as CacheStore } from "./types-
|
|
1
|
+
import { t as CacheStore } from "./types-BMvLj72n.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
|
-
type
|
|
4
|
+
type CacheSerializer = {
|
|
5
5
|
serialize: <T = unknown>(value: T) => string;
|
|
6
6
|
deserialize: <T = unknown>(value: string) => T;
|
|
7
7
|
};
|
|
8
8
|
interface CacheOptions {
|
|
9
9
|
store?: CacheStore;
|
|
10
|
-
serializer?:
|
|
10
|
+
serializer?: CacheSerializer;
|
|
11
11
|
defaultTtl?: number;
|
|
12
12
|
}
|
|
13
13
|
declare class Cacher {
|
|
14
14
|
private readonly _store;
|
|
15
15
|
private readonly _serializer;
|
|
16
16
|
private readonly _defaultTtl?;
|
|
17
|
+
private readonly _inflight;
|
|
17
18
|
constructor({
|
|
18
19
|
store,
|
|
19
20
|
serializer,
|
|
20
21
|
defaultTtl
|
|
21
22
|
}?: CacheOptions);
|
|
22
23
|
get<T>(key: string, defaultValue?: T): Promise<T | undefined>;
|
|
24
|
+
private _set;
|
|
23
25
|
set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
|
|
24
|
-
getOrSet<T>(key: string, factory: (
|
|
26
|
+
getOrSet<T>(key: string, factory: (ctx: {
|
|
27
|
+
setTtl: (ttl: number | undefined) => void;
|
|
28
|
+
}) => T | Promise<T>, ttl?: number): Promise<T>;
|
|
25
29
|
del(key: string): Promise<boolean>;
|
|
26
30
|
clear(): Promise<boolean>;
|
|
27
31
|
getMultiple<T>(keys: string[], defaultValue?: T): Promise<(T | undefined)[]>;
|
|
@@ -31,4 +35,4 @@ declare class Cacher {
|
|
|
31
35
|
dispose(): Promise<void>;
|
|
32
36
|
}
|
|
33
37
|
//#endregion
|
|
34
|
-
export { CacheOptions, Cacher };
|
|
38
|
+
export { CacheOptions, CacheSerializer, Cacher };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as e}from"./null-cache-
|
|
1
|
+
import{t as e}from"./null-cache-C6T2ju9e.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;_inflight=new Map;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)}async _set(e,n,r){return this._store.set(t(e),this._serializer.serialize(n),r)}async set(e,t,n){return this._set(e,t,n??this._defaultTtl)}async getOrSet(e,t,n){let r=await this.get(e);if(r!==void 0)return r;let i=this._inflight.get(e);if(i)return i;let a=n??this._defaultTtl,o=Promise.resolve(t({setTtl:e=>{a=e}})).then(async t=>(await this._set(e,t,a),this._inflight.delete(e),t)).catch(t=>{throw this._inflight.delete(e),t});return this._inflight.set(e,o),o}async del(e){return this._store.del(t(e))}async 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
package/dist/stores.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as e}from"./null-cache-
|
|
1
|
+
import{t as e}from"./null-cache-C6T2ju9e.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};
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|