@aomex/cache 3.30.7 → 3.30.8-alpha.3d94097.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 +3 -0
- package/dist/adapters/cache-file-adapter.d.ts +72 -0
- package/dist/adapters/cache-file-adapter.js +197 -0
- package/dist/adapters/cache-file-adapter.js.map +1 -0
- package/dist/adapters/cache-memory-adapter.d.ts +18 -0
- package/dist/adapters/cache-memory-adapter.js +105 -0
- package/dist/adapters/cache-memory-adapter.js.map +1 -0
- package/dist/cache-adapter-CVIt9DB_.d.ts +17 -0
- package/dist/i18n-OXOpRIod.js +27 -0
- package/dist/i18n-OXOpRIod.js.map +1 -0
- package/dist/index.d.ts +217 -158
- package/dist/index.js +389 -411
- package/dist/index.js.map +1 -1
- package/package.json +12 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,162 +1,221 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { t as CacheAdapter } from "./cache-adapter-CVIt9DB_.js";
|
|
2
|
+
import { Service } from "@aomex/common";
|
|
3
|
+
/**
|
|
4
|
+
* 缓存键。通常由 `BaseCacheService#key()` / `#keyHash()` 创建。
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* this.key('user', 1); // user:1
|
|
8
|
+
* this.key('user').key(1); // user:1
|
|
9
|
+
* this.keyHash('user', 1); // md5('user:1')
|
|
10
|
+
* this.key('user').keyHash(1); // user:md5('1')
|
|
11
|
+
* this.key('user', 1).toHash(); // md5('user:1')
|
|
12
|
+
* await this.cache.get(this.key('user', 1));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare class CacheKey {
|
|
16
|
+
protected readonly value: string;
|
|
17
|
+
constructor(value?: string);
|
|
18
|
+
/** 用 `:` 拼接片段(保留已有前缀),如 `key('user').key(1)` → `'user:1'` */
|
|
19
|
+
key(...keys: Array<string | number>): CacheKey;
|
|
20
|
+
/** 仅对本次参数拼接后 MD5,再接到已有前缀后 */
|
|
21
|
+
keyHash(...keys: Array<string | number>): CacheKey;
|
|
22
|
+
/** 对当前完整键做 MD5 */
|
|
23
|
+
toHash(): CacheKey;
|
|
24
|
+
protected toString(): string;
|
|
25
|
+
protected md5(key: string | number): string;
|
|
15
26
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
export declare namespace Caching {
|
|
28
|
+
type Types = string | number | unknown[] | readonly unknown[] | {
|
|
29
|
+
[K: string]: any;
|
|
30
|
+
} | boolean | Map<any, any> | Set<any> | bigint | Date;
|
|
31
|
+
/** 缓存键:字符串或 `CacheKey` */
|
|
32
|
+
type Key = string | CacheKey;
|
|
21
33
|
}
|
|
22
|
-
declare class Caching<T extends CacheAdapter = CacheAdapter> {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
* const result = await queryDB({ id: id });
|
|
121
|
-
* return result;
|
|
122
|
-
* }
|
|
123
|
-
* }
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
decorate<T extends Caching.Types | null, P extends (...args: any[]) => Promise<T>, Q extends NonNullable<T>>(opts: {
|
|
127
|
-
key?: string | ((...args: Parameters<P>) => string);
|
|
128
|
-
duration: number;
|
|
129
|
-
/**
|
|
130
|
-
* 当数据源返回null或者undefined时,则返回默认值
|
|
131
|
-
*/
|
|
132
|
-
defaultValue?: Q;
|
|
133
|
-
}): (originalMethod: P, context: ClassMethodDecoratorContext) => (this: object, ...args: Parameters<P>) => Promise<any>;
|
|
134
|
-
protected encodeValue(value: Caching.Types): string;
|
|
135
|
-
protected stringifyReplacer(value: Caching.Types): string | number | boolean | unknown[] | readonly unknown[] | {
|
|
136
|
-
[K: string]: any;
|
|
137
|
-
};
|
|
138
|
-
protected convertDate(value: any): any;
|
|
139
|
-
protected decodeValue(value: string | null, defaultValue?: Caching.Types | null): any;
|
|
140
|
-
protected jsonParseReviver(value: any): any;
|
|
34
|
+
export declare class Caching<T extends CacheAdapter = CacheAdapter> {
|
|
35
|
+
readonly adapter: T;
|
|
36
|
+
constructor(adapter: T);
|
|
37
|
+
/**
|
|
38
|
+
* 查看缓存是否存在
|
|
39
|
+
*/
|
|
40
|
+
exists(key: Caching.Key): Promise<boolean>;
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* 获取缓存。如果未找到缓存并且未提供默认值,则返回`null`
|
|
44
|
+
* ```typescript
|
|
45
|
+
* await cache.get('key'); // null
|
|
46
|
+
* await cache.get('key', 'my-value'); // my-value
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
get<T>(key: Caching.Key, defaultValue: T): Promise<T>;
|
|
50
|
+
get<T extends Caching.Types>(key: Caching.Key, defaultValue: T | null | undefined): Promise<T | null>;
|
|
51
|
+
get<T extends Caching.Types>(key: Caching.Key): Promise<T | null>;
|
|
52
|
+
/**
|
|
53
|
+
* 设置缓存。可以指定过期时间(毫秒)
|
|
54
|
+
*
|
|
55
|
+
* ```typescript
|
|
56
|
+
* await cache.set('key', 'value');
|
|
57
|
+
* await cache.set('key', 'value', 3600);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
set(key: Caching.Key, value: Caching.Types, durationMs?: number): Promise<boolean>;
|
|
61
|
+
/**
|
|
62
|
+
* 设置缓存。如果缓存已经存在,则设置失败,返回`false`
|
|
63
|
+
*
|
|
64
|
+
* ```typescript
|
|
65
|
+
* await cache.setNX('foo', 'bar'); // true
|
|
66
|
+
* await cache.setNX('foo', 'baz'); // false
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
setNX(key: Caching.Key, value: Caching.Types, durationMs?: number): Promise<boolean>;
|
|
70
|
+
/**
|
|
71
|
+
* 将一个或多个值按顺序插入到列表头部。当 key 存在但不是列表类型时,返回一个错误。
|
|
72
|
+
*
|
|
73
|
+
* ```typescript
|
|
74
|
+
* await cache.leftPush('foo', 'a'); // ['a']
|
|
75
|
+
* await cache.leftPush('foo', 'b', 'c'); // ['b', 'c', 'a']
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
leftPush(key: Caching.Key, ...values: Caching.Types[]): Promise<boolean>;
|
|
79
|
+
/**
|
|
80
|
+
* 移除列表的最后一个元素,返回值为被移除的元素
|
|
81
|
+
*/
|
|
82
|
+
rightPop<T extends Caching.Types>(key: Caching.Key): Promise<T | null>;
|
|
83
|
+
/**
|
|
84
|
+
* 将key中储存的数字值增一。如果key不存在,那么key的值会先被初始化为0,然后再执行操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
|
|
85
|
+
*
|
|
86
|
+
* ```typescript
|
|
87
|
+
* await cache.increment('foo'); // 1
|
|
88
|
+
* await cache.increment('foo'); // 2
|
|
89
|
+
* await cache.increment('foo'); // 3
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
increment(key: Caching.Key): Promise<number>;
|
|
93
|
+
/**
|
|
94
|
+
* 重新设置缓存时间。如果key不存在,则返回`false`
|
|
95
|
+
*/
|
|
96
|
+
expire(key: Caching.Key, durationMs: number): Promise<boolean>;
|
|
97
|
+
/**
|
|
98
|
+
* 检测缓存的剩余生存时间(Time to live)
|
|
99
|
+
* - 不存在则返回`-2`
|
|
100
|
+
* - 未设置过期时间则返回`-1`
|
|
101
|
+
* - 已设置过期时间则返回剩余时间,单位**毫秒**
|
|
102
|
+
*
|
|
103
|
+
* ```typescript
|
|
104
|
+
* await cache.ttl('foo'); // -2
|
|
105
|
+
* await cache.set('foo', 'bar');
|
|
106
|
+
* await cache.ttl('foo'); // -1
|
|
107
|
+
* await cache.expire('foo', 60000);
|
|
108
|
+
* await cache.ttl('foo'); // 59000
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
ttl(key: Caching.Key): Promise<number>;
|
|
112
|
+
/**
|
|
113
|
+
* 将key中储存的数字值减一。如果key不存在,那么key的值会先被初始化为0,然后再执行操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误
|
|
114
|
+
*/
|
|
115
|
+
decrement(key: Caching.Key): Promise<number>;
|
|
116
|
+
/**
|
|
117
|
+
* 删除指定缓存
|
|
118
|
+
*/
|
|
119
|
+
delete(key: Caching.Key): Promise<boolean>;
|
|
120
|
+
/**
|
|
121
|
+
* 删除所有缓存
|
|
122
|
+
*/
|
|
123
|
+
deleteAll(): Promise<boolean>;
|
|
124
|
+
protected resolveKey(key: Caching.Key): string;
|
|
125
|
+
protected encodeValue(value: Caching.Types): string;
|
|
126
|
+
protected stringifyReplacer(value: Caching.Types): string | number | boolean | unknown[] | readonly unknown[] | {
|
|
127
|
+
[K: string]: any;
|
|
128
|
+
};
|
|
129
|
+
protected convertDate(value: any): any;
|
|
130
|
+
protected decodeValue(value: string | null, defaultValue?: Caching.Types | null): any;
|
|
131
|
+
protected jsonParseReviver(value: any): any;
|
|
141
132
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
133
|
+
/**
|
|
134
|
+
* 缓存领域服务基类:子类实现 `createAdapter`,在业务方法里通过 `this.cache` 访问。
|
|
135
|
+
*
|
|
136
|
+
* ```typescript
|
|
137
|
+
* import { CacheMemoryAdapter } from '@aomex/cache/memory';
|
|
138
|
+
* import { createRemember } from '@aomex/cache';
|
|
139
|
+
*
|
|
140
|
+
* class CacheService extends BaseCacheService {
|
|
141
|
+
* protected createAdapter() {
|
|
142
|
+
* return new CacheMemoryAdapter();
|
|
143
|
+
* }
|
|
144
|
+
* }
|
|
145
|
+
*
|
|
146
|
+
* export const remember = createRemember(CacheService);
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export declare abstract class BaseCacheService<T extends CacheAdapter = CacheAdapter> extends Service {
|
|
150
|
+
private _cache;
|
|
151
|
+
protected get cache(): Caching<T>;
|
|
152
|
+
protected abstract createAdapter(): T;
|
|
153
|
+
protected onInit(): Promise<void>;
|
|
154
|
+
protected onDestroy(): Promise<void>;
|
|
155
|
+
/** 常用过期时间(毫秒):`this.ttl.DAY` */
|
|
156
|
+
protected get ttl(): {
|
|
157
|
+
readonly SECOND: 1000;
|
|
158
|
+
readonly MINUTE: 60000;
|
|
159
|
+
readonly HOUR: 3600000;
|
|
160
|
+
readonly DAY: 86400000;
|
|
161
|
+
readonly WEEK: 604800000;
|
|
162
|
+
/** 按 30 天计 */
|
|
163
|
+
readonly MONTH: 2592000000;
|
|
164
|
+
/** 按 365 天计 */
|
|
165
|
+
readonly YEAR: 31536000000;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* 用 `:` 拼接片段,得到可读的缓存键(可继续链式调用)。
|
|
169
|
+
*
|
|
170
|
+
* ```typescript
|
|
171
|
+
* this.key('user', 1); // user:1
|
|
172
|
+
* this.key('user').key(1); // 同上
|
|
173
|
+
* this.key('user').keyHash(1); // user:<md5('1')> 前缀保留,只哈希新片段
|
|
174
|
+
* this.key('user', 1).toHash(); // <md5('user:1')> 整键再哈希
|
|
175
|
+
* await this.cache.get(this.key('user', 1));
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
protected key(...keys: Array<string | number>): CacheKey;
|
|
179
|
+
/**
|
|
180
|
+
* 将片段用 `:` 拼接后整体 MD5(无可读前缀)。
|
|
181
|
+
* 等价于 `this.key(...keys).toHash()`。
|
|
182
|
+
*
|
|
183
|
+
* ```typescript
|
|
184
|
+
* this.keyHash('user', 1); // <md5('user:1')>
|
|
185
|
+
* this.key('user').keyHash(1); // 不是这个:那是「前缀 + 哈希段」
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
protected keyHash(...keys: Array<string | number>): CacheKey;
|
|
159
189
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
190
|
+
type RememberOptions<R extends Caching.Types | null = Caching.Types | null, P extends (...args: any[]) => Promise<R> = (...args: any[]) => Promise<R>, Q extends NonNullable<R> = NonNullable<R>> = {
|
|
191
|
+
key?: string | ((this: any, ...args: Parameters<P>) => string);
|
|
192
|
+
duration: number;
|
|
193
|
+
/**
|
|
194
|
+
* 当数据源返回 null 或者 undefined 时,则返回默认值
|
|
195
|
+
*/
|
|
196
|
+
defaultValue?: Q;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* 可调用的方法装饰器,由 `createRemember(CacheService)` 创建。
|
|
200
|
+
* 绑定到指定缓存服务类;实例 `onInit` 完成前调用会等待就绪,`onDestroy` 后调用会抛错。
|
|
201
|
+
*/
|
|
202
|
+
interface Remember {
|
|
203
|
+
(durationMs: number): (originalMethod: (...args: any[]) => Promise<any>, context: ClassMethodDecoratorContext) => (...args: any[]) => Promise<any>;
|
|
204
|
+
<R extends Caching.Types | null, P extends (...args: any[]) => Promise<R>, Q extends NonNullable<R>>(opts: RememberOptions<R, P, Q>): (originalMethod: P, context: ClassMethodDecoratorContext) => P;
|
|
205
|
+
}
|
|
206
|
+
/** 任意 `BaseCacheService` 构造函数(含基类,供内部挂载使用) */
|
|
207
|
+
type CacheServiceCtor = abstract new (...args: any[]) => BaseCacheService;
|
|
208
|
+
/**
|
|
209
|
+
* 具体缓存服务子类构造函数。传入 `BaseCacheService` 本身时类型为 `never`。
|
|
210
|
+
*/
|
|
211
|
+
type ConcreteCacheServiceCtor<C extends CacheServiceCtor = CacheServiceCtor> = BaseCacheService extends InstanceType<C> ? never : C;
|
|
212
|
+
/**
|
|
213
|
+
* 创建绑定到指定 `BaseCacheService` 子类的 remember 装饰器。
|
|
214
|
+
* 必须传入具体子类,不能传入 `BaseCacheService` 本身。
|
|
215
|
+
*
|
|
216
|
+
* ```typescript
|
|
217
|
+
* export const remember = createRemember(CacheService);
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
export declare function createRemember<C extends CacheServiceCtor>(ctor: ConcreteCacheServiceCtor<C>): Remember;
|
|
221
|
+
export { CacheAdapter, type CacheServiceCtor, type ConcreteCacheServiceCtor, type Remember, type RememberOptions };
|