@aomex/cache 3.30.7 → 3.30.8-alpha.131cf1c.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/adapters/cache-file-adapter.d.ts +72 -0
- package/dist/adapters/cache-file-adapter.js +198 -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 +106 -0
- package/dist/adapters/cache-memory-adapter.js.map +1 -0
- package/dist/cache-adapter-4n2JTK4Y.js +8 -0
- package/dist/cache-adapter-4n2JTK4Y.js.map +1 -0
- package/dist/cache-adapter-CVIt9DB_.d.ts +17 -0
- package/dist/i18n-BKvtLTPF.js +20 -0
- package/dist/i18n-BKvtLTPF.js.map +1 -0
- package/dist/index.d.ts +202 -158
- package/dist/index.js +326 -412
- package/dist/index.js.map +1 -1
- package/package.json +12 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,162 +1,206 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
abstract deleteValue(key: string): Promise<boolean>;
|
|
8
|
-
abstract deleteAllValues(): Promise<boolean>;
|
|
9
|
-
abstract increaseValue(key: string): Promise<number>;
|
|
10
|
-
abstract decreaseValue(key: string): Promise<number>;
|
|
11
|
-
abstract expireKey(key: string, durationMS: number): Promise<boolean>;
|
|
12
|
-
abstract ttlKey(key: string): Promise<number>;
|
|
13
|
-
abstract leftPushValue(key: string, ...values: string[]): Promise<boolean>;
|
|
14
|
-
abstract rightPopValue(key: string): Promise<string | null>;
|
|
1
|
+
import { t as CacheAdapter } from "./cache-adapter-CVIt9DB_.js";
|
|
2
|
+
import { Service } from "@aomex/common";
|
|
3
|
+
export declare namespace Caching {
|
|
4
|
+
type Types = string | number | unknown[] | readonly unknown[] | {
|
|
5
|
+
[K: string]: any;
|
|
6
|
+
} | boolean | Map<any, any> | Set<any> | bigint | Date;
|
|
15
7
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
export declare class Caching<T extends CacheAdapter = CacheAdapter> {
|
|
9
|
+
readonly adapter: T;
|
|
10
|
+
constructor(adapter: T);
|
|
11
|
+
/**
|
|
12
|
+
* 查看缓存是否存在
|
|
13
|
+
*/
|
|
14
|
+
exists(key: string): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* 获取缓存。如果未找到缓存并且未提供默认值,则返回`null`
|
|
18
|
+
* ```typescript
|
|
19
|
+
* await cache.get('key'); // null
|
|
20
|
+
* await cache.get('key', 'my-value'); // my-value
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
get<T>(key: string, defaultValue: T): Promise<T>;
|
|
24
|
+
get<T extends Caching.Types>(key: string, defaultValue: T | null | undefined): Promise<T | null>;
|
|
25
|
+
get<T extends Caching.Types>(key: string): Promise<T | null>;
|
|
26
|
+
/**
|
|
27
|
+
* 设置缓存。可以指定过期时间(毫秒)
|
|
28
|
+
*
|
|
29
|
+
* ```typescript
|
|
30
|
+
* await cache.set('key', 'value');
|
|
31
|
+
* await cache.set('key', 'value', 3600);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
set(key: string, value: Caching.Types, durationMs?: number): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* 设置缓存。如果缓存已经存在,则设置失败,返回`false`
|
|
37
|
+
*
|
|
38
|
+
* ```typescript
|
|
39
|
+
* await cache.setNX('foo', 'bar'); // true
|
|
40
|
+
* await cache.setNX('foo', 'baz'); // false
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
setNX(key: string, value: Caching.Types, durationMs?: number): Promise<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* 将一个或多个值按顺序插入到列表头部。当 key 存在但不是列表类型时,返回一个错误。
|
|
46
|
+
*
|
|
47
|
+
* ```typescript
|
|
48
|
+
* await cache.leftPush('foo', 'a'); // ['a']
|
|
49
|
+
* await cache.leftPush('foo', 'b', 'c'); // ['b', 'c', 'a']
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
leftPush(key: string, ...values: Caching.Types[]): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* 移除列表的最后一个元素,返回值为被移除的元素
|
|
55
|
+
*/
|
|
56
|
+
rightPop<T extends Caching.Types>(key: string): Promise<T | null>;
|
|
57
|
+
/**
|
|
58
|
+
* 将key中储存的数字值增一。如果key不存在,那么key的值会先被初始化为0,然后再执行操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
|
|
59
|
+
*
|
|
60
|
+
* ```typescript
|
|
61
|
+
* await cache.increment('foo'); // 1
|
|
62
|
+
* await cache.increment('foo'); // 2
|
|
63
|
+
* await cache.increment('foo'); // 3
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
increment(key: string): Promise<number>;
|
|
67
|
+
/**
|
|
68
|
+
* 重新设置缓存时间。如果key不存在,则返回`false`
|
|
69
|
+
*/
|
|
70
|
+
expire(key: string, durationMs: number): Promise<boolean>;
|
|
71
|
+
/**
|
|
72
|
+
* 检测缓存的剩余生存时间(Time to live)
|
|
73
|
+
* - 不存在则返回`-2`
|
|
74
|
+
* - 未设置过期时间则返回`-1`
|
|
75
|
+
* - 已设置过期时间则返回剩余时间,单位**毫秒**
|
|
76
|
+
*
|
|
77
|
+
* ```typescript
|
|
78
|
+
* await cache.ttl('foo'); // -2
|
|
79
|
+
* await cache.set('foo', 'bar');
|
|
80
|
+
* await cache.ttl('foo'); // -1
|
|
81
|
+
* await cache.expire('foo', 60000);
|
|
82
|
+
* await cache.ttl('foo'); // 59000
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
ttl(key: string): Promise<number>;
|
|
86
|
+
/**
|
|
87
|
+
* 将key中储存的数字值减一。如果key不存在,那么key的值会先被初始化为0,然后再执行操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误
|
|
88
|
+
*/
|
|
89
|
+
decrement(key: string): Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* 删除指定缓存
|
|
92
|
+
*/
|
|
93
|
+
delete(key: string): Promise<boolean>;
|
|
94
|
+
/**
|
|
95
|
+
* 删除所有缓存
|
|
96
|
+
*/
|
|
97
|
+
deleteAll(): Promise<boolean>;
|
|
98
|
+
protected encodeValue(value: Caching.Types): string;
|
|
99
|
+
protected stringifyReplacer(value: Caching.Types): string | number | boolean | unknown[] | readonly unknown[] | {
|
|
100
|
+
[K: string]: any;
|
|
101
|
+
};
|
|
102
|
+
protected convertDate(value: any): any;
|
|
103
|
+
protected decodeValue(value: string | null, defaultValue?: Caching.Types | null): any;
|
|
104
|
+
protected jsonParseReviver(value: any): any;
|
|
21
105
|
}
|
|
22
|
-
|
|
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
|
-
setNX(key: string, value: Caching.Types, durationMs?: number): Promise<boolean>;
|
|
58
|
-
/**
|
|
59
|
-
* 将一个或多个值按顺序插入到列表头部。当 key 存在但不是列表类型时,返回一个错误。
|
|
60
|
-
*
|
|
61
|
-
* ```typescript
|
|
62
|
-
* await cache.leftPush('foo', 'a'); // ['a']
|
|
63
|
-
* await cache.leftPush('foo', 'b', 'c'); // ['b', 'c', 'a']
|
|
64
|
-
* ```
|
|
65
|
-
*/
|
|
66
|
-
leftPush(key: string, ...values: Caching.Types[]): Promise<boolean>;
|
|
67
|
-
/**
|
|
68
|
-
* 移除列表的最后一个元素,返回值为被移除的元素
|
|
69
|
-
*/
|
|
70
|
-
rightPop<T extends Caching.Types>(key: string): Promise<T | null>;
|
|
71
|
-
/**
|
|
72
|
-
* 将key中储存的数字值增一。如果key不存在,那么key的值会先被初始化为0,然后再执行操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
|
|
73
|
-
*
|
|
74
|
-
* ```typescript
|
|
75
|
-
* await cache.increment('foo'); // 1
|
|
76
|
-
* await cache.increment('foo'); // 2
|
|
77
|
-
* await cache.increment('foo'); // 3
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
increment(key: string): Promise<number>;
|
|
81
|
-
/**
|
|
82
|
-
* 重新设置缓存时间。如果key不存在,则返回`false`
|
|
83
|
-
*/
|
|
84
|
-
expire(key: string, durationMs: number): Promise<boolean>;
|
|
85
|
-
/**
|
|
86
|
-
* 检测缓存的剩余生存时间(Time to live)
|
|
87
|
-
* - 不存在则返回`-2`
|
|
88
|
-
* - 未设置过期时间则返回`-1`
|
|
89
|
-
* - 已设置过期时间则返回剩余时间,单位**毫秒**
|
|
90
|
-
*
|
|
91
|
-
* ```typescript
|
|
92
|
-
* await cache.ttl('foo'); // -2
|
|
93
|
-
* await cache.set('foo', 'bar');
|
|
94
|
-
* await cache.ttl('foo'); // -1
|
|
95
|
-
* await cache.expire('foo', 60000);
|
|
96
|
-
* await cache.ttl('foo'); // 59000
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
ttl(key: string): Promise<number>;
|
|
100
|
-
/**
|
|
101
|
-
* 将key中储存的数字值减一。如果key不存在,那么key的值会先被初始化为0,然后再执行操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误
|
|
102
|
-
*/
|
|
103
|
-
decrement(key: string): Promise<number>;
|
|
104
|
-
/**
|
|
105
|
-
* 删除指定缓存
|
|
106
|
-
*/
|
|
107
|
-
delete(key: string): Promise<boolean>;
|
|
108
|
-
/**
|
|
109
|
-
* 删除所有缓存
|
|
110
|
-
*/
|
|
111
|
-
deleteAll(): Promise<boolean>;
|
|
112
|
-
/**
|
|
113
|
-
* 方法装饰器,自动获取数据,如果不存在则请求原始数据并自动保存。在请求原始数据期间,任意相同请求都会被截流,并共享第一个请求
|
|
114
|
-
* ```typescript
|
|
115
|
-
* const cache = new Caching(memoryAdapter());
|
|
116
|
-
*
|
|
117
|
-
* class MyClass {
|
|
118
|
-
* \@cache.decorate({ key: (id) => `key_${id}`, duration: 60_000 })
|
|
119
|
-
* async getData(id: number) {
|
|
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;
|
|
106
|
+
/**
|
|
107
|
+
* 缓存领域服务基类:子类实现 `createAdapter`,在业务方法里通过 `this.cache` 访问。
|
|
108
|
+
*
|
|
109
|
+
* ```typescript
|
|
110
|
+
* import { CacheMemoryAdapter } from '@aomex/cache/memory';
|
|
111
|
+
*
|
|
112
|
+
* class CacheService extends BaseCacheService {
|
|
113
|
+
* protected createAdapter() {
|
|
114
|
+
* return new CacheMemoryAdapter();
|
|
115
|
+
* }
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare abstract class BaseCacheService<T extends CacheAdapter = CacheAdapter> extends Service {
|
|
120
|
+
private _cache;
|
|
121
|
+
protected get cache(): Caching<T>;
|
|
122
|
+
protected abstract createAdapter(): T;
|
|
123
|
+
protected onInit(): Promise<void>;
|
|
124
|
+
protected onDestroy(): Promise<void>;
|
|
125
|
+
/** 常用过期时间(毫秒):`this.ttl.DAY` */
|
|
126
|
+
protected get ttl(): {
|
|
127
|
+
readonly SECOND: 1000;
|
|
128
|
+
readonly MINUTE: 60000;
|
|
129
|
+
readonly HOUR: 3600000;
|
|
130
|
+
readonly DAY: 86400000;
|
|
131
|
+
readonly WEEK: 604800000;
|
|
132
|
+
/** 按 30 天计 */
|
|
133
|
+
readonly MONTH: 2592000000;
|
|
134
|
+
/** 按 365 天计 */
|
|
135
|
+
readonly YEAR: 31536000000;
|
|
136
|
+
};
|
|
137
|
+
/** 用 `:` 拼接缓存 key 片段,如 `concat('user', 1)` → `'user:1'` */
|
|
138
|
+
protected concat(...keys: Array<string | number>): string;
|
|
139
|
+
/** 对 key 做 MD5,适合过长或不规则片段 */
|
|
140
|
+
protected hash(key: string | number): string;
|
|
141
141
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
142
|
+
export 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>> = {
|
|
143
|
+
key?: string | ((this: any, ...args: Parameters<P>) => string);
|
|
144
|
+
duration: number;
|
|
145
|
+
/**
|
|
146
|
+
* 当数据源返回 null 或者 undefined 时,则返回默认值
|
|
147
|
+
*/
|
|
148
|
+
defaultValue?: Q;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* 可调用的方法装饰器,并携带独立的 `BaseCacheService` 作用域。
|
|
152
|
+
* `fork()` 会得到一份新的 remember(各自的 setInstance / deleteInstance 互不影响)。
|
|
153
|
+
*/
|
|
154
|
+
export interface Remember {
|
|
155
|
+
(durationMs: number): (originalMethod: (...args: any[]) => Promise<any>, context: ClassMethodDecoratorContext) => (...args: any[]) => Promise<any>;
|
|
156
|
+
<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;
|
|
157
|
+
/** 登记本作用域唯一的 `BaseCacheService`;重复传入同一实例为幂等,传入不同实例则抛错 */
|
|
158
|
+
setInstance(service: BaseCacheService): void;
|
|
159
|
+
/** 清除本作用域的 `BaseCacheService` 登记 */
|
|
160
|
+
deleteInstance(): void;
|
|
161
|
+
/** 派生一份独立作用域的 remember */
|
|
162
|
+
fork(): Remember;
|
|
159
163
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
164
|
+
/**
|
|
165
|
+
* 方法装饰器:自动获取缓存,未命中则执行原方法并写入;并发相同 key 会截流共享首个请求。
|
|
166
|
+
* 每个 remember(含 `fork()` 产物)各自只允许登记一个 `BaseCacheService`。
|
|
167
|
+
*
|
|
168
|
+
* ```typescript
|
|
169
|
+
* class CacheService extends BaseCacheService {
|
|
170
|
+
* protected createAdapter() {
|
|
171
|
+
* return new CacheRedisAdapter({ host: '127.0.0.1' });
|
|
172
|
+
* }
|
|
173
|
+
*
|
|
174
|
+
* protected override async onInit() {
|
|
175
|
+
* await super.onInit();
|
|
176
|
+
* remember.setInstance(this);
|
|
177
|
+
* }
|
|
178
|
+
*
|
|
179
|
+
* protected override async onDestroy() {
|
|
180
|
+
* remember.deleteInstance();
|
|
181
|
+
* await super.onDestroy();
|
|
182
|
+
* }
|
|
183
|
+
* }
|
|
184
|
+
*
|
|
185
|
+
* await combineServices(
|
|
186
|
+
* {
|
|
187
|
+
* cache: () => import('./cache.service.js'),
|
|
188
|
+
* report: () => import('./report.service.js'),
|
|
189
|
+
* },
|
|
190
|
+
* { eager: ['cache'] },
|
|
191
|
+
* );
|
|
192
|
+
*
|
|
193
|
+
* class Report {
|
|
194
|
+
* @remember(60_000)
|
|
195
|
+
* async build() {
|
|
196
|
+
* return { ok: true };
|
|
197
|
+
* }
|
|
198
|
+
* }
|
|
199
|
+
*
|
|
200
|
+
* // 另一套缓存作用域
|
|
201
|
+
* const rememberB = remember.fork();
|
|
202
|
+
* rememberB.setInstance(otherCacheService);
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
export declare const remember: Remember;
|
|
206
|
+
export { CacheAdapter };
|