@zenweb/cache 5.0.2 → 5.1.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/cache.d.ts CHANGED
@@ -44,7 +44,7 @@ export declare class Cache {
44
44
  /**
45
45
  * 直接取得缓存
46
46
  */
47
- getRaw(key: string): Promise<Buffer | null>;
47
+ getRaw(key: string): Promise<Buffer<ArrayBufferLike> | null>;
48
48
  /**
49
49
  * 取得缓存对象
50
50
  * @param key 缓存key
package/dist/global.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { CacheHelperOption } from "./types.js";
2
+ import { CacheHelper } from "./helper.js";
1
3
  /**
2
4
  * 取得缓存实例
3
5
  */
@@ -6,3 +8,7 @@ export declare function $getCache(): import("./cache.js").Cache;
6
8
  * 快捷方法 - 缓存实例
7
9
  */
8
10
  export declare const $cache: import("./cache.js").Cache;
11
+ /**
12
+ * 快捷方法 - 缓存助手
13
+ */
14
+ export declare function $cacheHelper<T = unknown>(key: string | (() => string), fetch?: () => Promise<T> | T, opt?: CacheHelperOption): CacheHelper<T>;
package/dist/global.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { callProxy, $getCore } from "@zenweb/core";
2
+ import { CacheHelper } from "./helper.js";
2
3
  /**
3
4
  * 取得缓存实例
4
5
  */
@@ -10,3 +11,9 @@ export function $getCache() {
10
11
  * 快捷方法 - 缓存实例
11
12
  */
12
13
  export const $cache = callProxy($getCache);
14
+ /**
15
+ * 快捷方法 - 缓存助手
16
+ */
17
+ export function $cacheHelper(key, fetch, opt) {
18
+ return new CacheHelper($getCache(), key, fetch, opt);
19
+ }
@@ -0,0 +1,27 @@
1
+ import { Cache } from "./cache.js";
2
+ import { CacheHelperOption } from "./types.js";
3
+ export declare class CacheNotExists extends Error {
4
+ }
5
+ export declare class CacheHelper<T = unknown> {
6
+ private _cache;
7
+ private _key;
8
+ private _fetch?;
9
+ private _opt?;
10
+ private __key?;
11
+ constructor(_cache: Cache, _key: string | (() => string), _fetch?: (() => Promise<T> | T) | undefined, _opt?: CacheHelperOption | undefined);
12
+ private get _cacheKey();
13
+ /**
14
+ * 取得缓存
15
+ *
16
+ * 如果已定义 `fetch` 参数当缓存不存在时使用 `fetch` 获取并设置缓存,否则抛出异常
17
+ */
18
+ get(): Promise<T>;
19
+ /**
20
+ * 设置缓存
21
+ */
22
+ set(value: T): Promise<import("./types.js").SetResult>;
23
+ /**
24
+ * 删除缓存
25
+ */
26
+ del(): Promise<number>;
27
+ }
package/dist/helper.js ADDED
@@ -0,0 +1,53 @@
1
+ export class CacheNotExists extends Error {
2
+ }
3
+ export class CacheHelper {
4
+ _cache;
5
+ _key;
6
+ _fetch;
7
+ _opt;
8
+ __key;
9
+ constructor(_cache, _key, _fetch, _opt) {
10
+ this._cache = _cache;
11
+ this._key = _key;
12
+ this._fetch = _fetch;
13
+ this._opt = _opt;
14
+ }
15
+ get _cacheKey() {
16
+ if (!this.__key) {
17
+ if (typeof this._key === 'function') {
18
+ this.__key = this._key();
19
+ }
20
+ else {
21
+ this.__key = this._key;
22
+ }
23
+ }
24
+ return this.__key;
25
+ }
26
+ /**
27
+ * 取得缓存
28
+ *
29
+ * 如果已定义 `fetch` 参数当缓存不存在时使用 `fetch` 获取并设置缓存,否则抛出异常
30
+ */
31
+ async get() {
32
+ if (typeof this._fetch === 'function') {
33
+ return this._cache.lockGet(this._cacheKey, this._fetch, { parse: true, noWait: false, ...this._opt });
34
+ }
35
+ const data = await this._cache.get(this._cacheKey, { parse: true, ...this._opt });
36
+ if (data === undefined) {
37
+ throw new CacheNotExists(`cache not exists: ${this._cacheKey}`);
38
+ }
39
+ return data;
40
+ }
41
+ /**
42
+ * 设置缓存
43
+ */
44
+ set(value) {
45
+ return this._cache.set(this._cacheKey, value, this._opt);
46
+ }
47
+ /**
48
+ * 删除缓存
49
+ */
50
+ del() {
51
+ return this._cache.del(this._cacheKey);
52
+ }
53
+ }
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export * from './types.js';
7
7
  export * from './utils.js';
8
8
  export { Locker } from './locker.js';
9
9
  export { cached } from './middleware.js';
10
+ export { CacheHelper } from './helper.js';
10
11
  export { Cache };
11
12
  /**
12
13
  * 安装模块
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export * from './types.js';
6
6
  export * from './utils.js';
7
7
  export { Locker } from './locker.js';
8
8
  export { cached } from './middleware.js';
9
+ export { CacheHelper } from './helper.js';
9
10
  export { Cache };
10
11
  /**
11
12
  * 安装模块
package/dist/types.d.ts CHANGED
@@ -145,3 +145,4 @@ export interface LockGetOption extends GetOption, SetOption, LockOption {
145
145
  */
146
146
  noWait?: boolean;
147
147
  }
148
+ export type CacheHelperOption = Omit<LockGetOption, 'parse' | 'noWait'>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zenweb/cache",
3
3
  "type": "module",
4
- "version": "5.0.2",
4
+ "version": "5.1.0",
5
5
  "description": "Zenweb Cache module",
6
6
  "exports": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",