@zenweb/cache 5.1.1 → 5.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/global.d.ts +2 -2
- package/dist/helper.d.ts +10 -8
- package/dist/helper.js +23 -19
- package/dist/types.d.ts +2 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +8 -6
- package/package.json +1 -1
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:
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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(
|
|
37
|
+
const data = await this._cache.get(_key, { parse: true, ...this._opt });
|
|
36
38
|
if (data === undefined) {
|
|
37
|
-
throw new CacheNotExists(`cache not exists: ${
|
|
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
|
-
|
|
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
|
-
|
|
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 | undefined | null | boolean;
|
|
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:
|
|
24
|
+
export declare function cacheKey(...key: CacheKeyType[]): string;
|
package/dist/utils.js
CHANGED
|
@@ -72,12 +72,14 @@ function plainifyOrHash(obj) {
|
|
|
72
72
|
*/
|
|
73
73
|
export function cacheKey(...key) {
|
|
74
74
|
return key.map(i => {
|
|
75
|
-
if (
|
|
76
|
-
return
|
|
77
|
-
|
|
78
|
-
return
|
|
79
|
-
|
|
80
|
-
return '';
|
|
75
|
+
if (i === null)
|
|
76
|
+
return 'N';
|
|
77
|
+
switch (typeof i) {
|
|
78
|
+
case 'string': return i;
|
|
79
|
+
case 'number': return String(i);
|
|
80
|
+
case 'boolean': return i ? 'T' : 'F';
|
|
81
|
+
case 'undefined': return '';
|
|
82
|
+
}
|
|
81
83
|
if (i instanceof Date)
|
|
82
84
|
return i.toJSON();
|
|
83
85
|
return plainifyOrHash(i);
|