@zenweb/cache 5.1.1 → 5.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/dist/global.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CacheHelperOption } from "./types.js";
1
+ import { CacheHelperKey, CacheHelperOption, CacheKeyType } from "./types.js";
2
2
  import { CacheHelper } from "./helper.js";
3
3
  /**
4
4
  * 取得缓存实例
@@ -11,4 +11,4 @@ export declare const $cache: import("./cache.js").Cache;
11
11
  /**
12
12
  * 快捷方法 - 缓存助手
13
13
  */
14
- export declare function $cacheHelper<T = unknown>(key: string | (() => string), fetch?: () => Promise<T> | T, opt?: CacheHelperOption): CacheHelper<T>;
14
+ export declare function $cacheHelper<P extends CacheKeyType[], T = unknown>(key: CacheHelperKey<P>, fetch?: (...param: P) => Promise<T> | T, opt?: CacheHelperOption): CacheHelper<P, T>;
package/dist/helper.d.ts CHANGED
@@ -1,27 +1,29 @@
1
1
  import { Cache } from "./cache.js";
2
- import { CacheHelperOption } from "./types.js";
2
+ import { CacheHelperKey, CacheHelperOption, CacheKeyType } from "./types.js";
3
3
  export declare class CacheNotExists extends Error {
4
4
  }
5
- export declare class CacheHelper<T = unknown> {
5
+ export declare class CacheHelper<P extends CacheKeyType[], T = unknown> {
6
6
  private _cache;
7
7
  private _key;
8
8
  private _fetch?;
9
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();
10
+ constructor(_cache: Cache, _key: CacheHelperKey<P>, _fetch?: ((...param: P) => Promise<T> | T) | undefined, _opt?: CacheHelperOption | undefined);
11
+ /**
12
+ * 取得缓存 key
13
+ */
14
+ key(...param: P): string | Promise<string>;
13
15
  /**
14
16
  * 取得缓存
15
17
  *
16
18
  * 如果已定义 `fetch` 参数当缓存不存在时使用 `fetch` 获取并设置缓存,否则抛出异常
17
19
  */
18
- get(): Promise<T>;
20
+ get(...param: P): Promise<T>;
19
21
  /**
20
22
  * 设置缓存
21
23
  */
22
- set(value: T): Promise<import("./types.js").SetResult>;
24
+ set(value: T, ...param: P): Promise<import("./types.js").SetResult>;
23
25
  /**
24
26
  * 删除缓存
25
27
  */
26
- del(): Promise<number>;
28
+ del(...param: P): Promise<number>;
27
29
  }
package/dist/helper.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { cacheKey } from "./utils.js";
1
2
  export class CacheNotExists extends Error {
2
3
  }
3
4
  export class CacheHelper {
@@ -5,49 +6,52 @@ export class CacheHelper {
5
6
  _key;
6
7
  _fetch;
7
8
  _opt;
8
- __key;
9
9
  constructor(_cache, _key, _fetch, _opt) {
10
10
  this._cache = _cache;
11
11
  this._key = _key;
12
12
  this._fetch = _fetch;
13
13
  this._opt = _opt;
14
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
- }
15
+ /**
16
+ * 取得缓存 key
17
+ */
18
+ key(...param) {
19
+ if (typeof this._key === 'function') {
20
+ return this._key(...param);
21
+ }
22
+ else {
23
+ return cacheKey(this._key, ...param);
23
24
  }
24
- return this.__key;
25
25
  }
26
26
  /**
27
27
  * 取得缓存
28
28
  *
29
29
  * 如果已定义 `fetch` 参数当缓存不存在时使用 `fetch` 获取并设置缓存,否则抛出异常
30
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 });
31
+ async get(...param) {
32
+ const _key = await this.key(...param);
33
+ const _fetch = this._fetch;
34
+ if (_fetch) {
35
+ return this._cache.lockGet(_key, () => _fetch(...param), { parse: true, noWait: false, ...this._opt });
34
36
  }
35
- const data = await this._cache.get(this._cacheKey, { parse: true, ...this._opt });
37
+ const data = await this._cache.get(_key, { parse: true, ...this._opt });
36
38
  if (data === undefined) {
37
- throw new CacheNotExists(`cache not exists: ${this._cacheKey}`);
39
+ throw new CacheNotExists(`cache not exists: ${_key}`);
38
40
  }
39
41
  return data;
40
42
  }
41
43
  /**
42
44
  * 设置缓存
43
45
  */
44
- set(value) {
45
- return this._cache.set(this._cacheKey, value, this._opt);
46
+ async set(value, ...param) {
47
+ const _key = await this.key(...param);
48
+ return this._cache.set(_key, value, this._opt);
46
49
  }
47
50
  /**
48
51
  * 删除缓存
49
52
  */
50
- del() {
51
- return this._cache.del(this._cacheKey);
53
+ async del(...param) {
54
+ const _key = await this.key(...param);
55
+ return this._cache.del(_key);
52
56
  }
53
57
  }
package/dist/types.d.ts CHANGED
@@ -145,4 +145,6 @@ export interface LockGetOption extends GetOption, SetOption, LockOption {
145
145
  */
146
146
  noWait?: boolean;
147
147
  }
148
+ export type CacheKeyType = string | number | object;
148
149
  export type CacheHelperOption = Omit<LockGetOption, 'parse' | 'noWait'>;
150
+ export type CacheHelperKey<P extends CacheKeyType[]> = string | ((...param: P) => string | Promise<string>);
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Redis, RedisKey, RedisValue } from 'ioredis';
2
+ import { CacheKeyType } from './types.js';
2
3
  export declare const debug: import("@zenweb/core").Debugger;
3
4
  /**
4
5
  * Promise 等待
@@ -20,4 +21,4 @@ export declare function runRedisScript(redis: Redis, hash: string, script: strin
20
21
  * 生成一个参数缓存key
21
22
  * - 多个元素用 : 连接
22
23
  */
23
- export declare function cacheKey(...key: (string | number | object)[]): string;
24
+ export declare function cacheKey(...key: CacheKeyType[]): string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zenweb/cache",
3
3
  "type": "module",
4
- "version": "5.1.1",
4
+ "version": "5.2.0",
5
5
  "description": "Zenweb Cache module",
6
6
  "exports": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",