@jintianxiayu/cache-decorator 1.0.0 → 1.0.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/CHANGELOG.md +20 -14
- package/README.md +358 -270
- package/dist/core/cache-error.d.ts +75 -0
- package/dist/core/cache-error.d.ts.map +1 -0
- package/dist/core/cache-error.js +168 -0
- package/dist/core/cache-error.js.map +1 -0
- package/dist/core/cache-logger.d.ts +3 -2
- package/dist/core/cache-logger.d.ts.map +1 -1
- package/dist/core/cache-logger.js +2 -0
- package/dist/core/cache-logger.js.map +1 -1
- package/dist/decorators/cache.d.ts +6 -0
- package/dist/decorators/cache.d.ts.map +1 -1
- package/dist/decorators/cache.js +106 -23
- package/dist/decorators/cache.js.map +1 -1
- package/jest.config.js +11 -11
- package/package.json +1 -1
- package/src/adapters/ioredis-cache-client.ts +89 -89
- package/src/adapters/node-redis-cache-client.ts +95 -95
- package/src/adapters/redis-key-prefix.ts +38 -38
- package/src/core/cache-error.ts +233 -0
- package/src/core/cache-logger.ts +116 -105
- package/src/core/key-builder.ts +33 -33
- package/src/core/native-cache.ts +55 -55
- package/src/core/pending-cache.ts +29 -29
- package/src/core/redis-cache-client.ts +160 -160
- package/src/core/redis-cache.ts +104 -104
- package/src/decorators/cache-evict.ts +129 -129
- package/src/decorators/cache.ts +319 -203
- package/src/index.ts +11 -11
- package/test/cache-evict-logging.test.ts +362 -362
- package/test/cache-logger.integration.test.ts +129 -129
- package/test/cache-logger.test.ts +156 -153
- package/test/cache-logging.test.ts +852 -544
- package/test/cache.test.ts +939 -255
- package/test/helpers/legacy-redis-cache.ts +41 -22
- package/test/helpers/package-consumer.ts +231 -231
- package/test/helpers/redis-fixture.ts +142 -142
- package/test/ioredis-cache-client.test.ts +143 -143
- package/test/legacy-redis-cache.test.ts +51 -0
- package/test/native-cache.test.ts +77 -77
- package/test/node-redis-cache-client.test.ts +149 -149
- package/test/pending-cache.test.ts +69 -69
- package/test/redis-cache-client-lifecycle.test.ts +112 -112
- package/test/redis-cache-client-types.test.ts +184 -184
- package/test/redis-cache-client.integration.test.ts +355 -327
- package/test/redis-cache-decorator.test.ts +540 -201
- package/test/redis-cache-provider.test.ts +269 -269
- package/test/type-contract/contract.ts +119 -64
- package/test/type-contract/tsconfig.json +12 -12
- package/tsconfig.json +8 -8
package/src/decorators/cache.ts
CHANGED
|
@@ -1,203 +1,319 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
58
|
-
interface
|
|
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
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
*
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import {
|
|
3
|
+
decodeCacheError,
|
|
4
|
+
encodeCacheError,
|
|
5
|
+
isCurrentCacheErrorPayload,
|
|
6
|
+
normalizeCacheErrorPolicy,
|
|
7
|
+
type CacheErrorCodec,
|
|
8
|
+
type CacheErrorPolicy,
|
|
9
|
+
type NormalizedCacheErrorPolicy,
|
|
10
|
+
} from '../core/cache-error';
|
|
11
|
+
import { cacheProviderLabel, logCacheEvent, type CacheLogContext } from '../core/cache-logger';
|
|
12
|
+
import type { CacheProvider } from '../core/cache-provider';
|
|
13
|
+
import { CacheProviderRegistry } from '../core/cache-provider-registry';
|
|
14
|
+
import { KeyBuilder } from '../core/key-builder';
|
|
15
|
+
import { PendingCache } from '../core/pending-cache';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 缓存 key 解析器类型
|
|
19
|
+
* - null: 使用自动生成逻辑
|
|
20
|
+
* - string: 直接作为 key 值的一部分
|
|
21
|
+
* - function: 接收方法参数数组,返回自定义字符串
|
|
22
|
+
*/
|
|
23
|
+
export type CacheKeyResolver = null | string | ((...args: unknown[]) => string);
|
|
24
|
+
|
|
25
|
+
export type { CacheErrorCodec, CacheErrorPolicy };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @Cache 装饰器配置项
|
|
29
|
+
*/
|
|
30
|
+
export interface CacheOptions {
|
|
31
|
+
/**
|
|
32
|
+
* 过期时间(秒)
|
|
33
|
+
*/
|
|
34
|
+
ttl?: number;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 指定 CacheProvider 名称
|
|
38
|
+
*/
|
|
39
|
+
providerName?: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 自定义缓存 key 生成逻辑
|
|
43
|
+
* - undefined/null: 使用默认逻辑 KeyBuilder.build(cacheName, args)
|
|
44
|
+
* - string: 使用 KeyBuilder.build(cacheName, [key])
|
|
45
|
+
* - function: 调用函数后使用 KeyBuilder.build(cacheName, [result])
|
|
46
|
+
*/
|
|
47
|
+
key?: CacheKeyResolver;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 业务异常缓存策略;省略时不向 Provider 持久化业务异常。
|
|
51
|
+
*/
|
|
52
|
+
errorCache?: CacheErrorPolicy;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 成功缓存条目
|
|
57
|
+
*/
|
|
58
|
+
interface SuccessCacheEntry<T> {
|
|
59
|
+
value: T;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 错误缓存条目
|
|
64
|
+
*/
|
|
65
|
+
interface ErrorCacheEntry {
|
|
66
|
+
error: unknown;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 缓存条目联合类型
|
|
71
|
+
*/
|
|
72
|
+
type CacheEntry<T> = SuccessCacheEntry<T> | ErrorCacheEntry;
|
|
73
|
+
|
|
74
|
+
interface CacheWriteRequest<T> {
|
|
75
|
+
readonly provider: CacheProvider;
|
|
76
|
+
readonly cacheKey: string;
|
|
77
|
+
readonly entry: CacheEntry<T>;
|
|
78
|
+
readonly entryType: 'value' | 'error';
|
|
79
|
+
readonly ttl: number | undefined;
|
|
80
|
+
readonly logContext: CacheLogContext;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type CacheReadResult<T> =
|
|
84
|
+
| { readonly type: 'value'; readonly value: T }
|
|
85
|
+
| { readonly type: 'error'; readonly error: unknown }
|
|
86
|
+
| { readonly type: 'miss' };
|
|
87
|
+
|
|
88
|
+
interface BusinessErrorRequest {
|
|
89
|
+
readonly error: unknown;
|
|
90
|
+
readonly errorPolicy: NormalizedCacheErrorPolicy | undefined;
|
|
91
|
+
readonly provider: CacheProvider;
|
|
92
|
+
readonly cacheKey: string;
|
|
93
|
+
readonly logContext: CacheLogContext;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const pendingCache = new PendingCache();
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 获取配置指向的缓存 Provider,并在解析失败时记录原始错误。
|
|
100
|
+
* @param providerName decorator 配置中的 Provider 名称。
|
|
101
|
+
* @param logContext 当前方法的稳定日志上下文。
|
|
102
|
+
* @returns 已注册的缓存 Provider。
|
|
103
|
+
* @throws Provider 注册表抛出的原始错误。
|
|
104
|
+
*/
|
|
105
|
+
function resolveCacheProvider(providerName: string | undefined, logContext: CacheLogContext): CacheProvider {
|
|
106
|
+
try {
|
|
107
|
+
return CacheProviderRegistry.get(providerName);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
logCacheEvent('cache.operation_failed', { ...logContext, operation: 'provider_resolution', error });
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 保持 fire-and-forget 语义提交缓存写入,仅捕获调用当下可观察的同步失败。
|
|
116
|
+
* @param request 写入所需 Provider、entry 和无业务数据日志上下文。
|
|
117
|
+
* @returns 无返回值;异步写入 Promise 不会被等待或消费。
|
|
118
|
+
* @throws Provider set 同步抛出的原始错误。
|
|
119
|
+
*/
|
|
120
|
+
function dispatchCacheWrite<T>(request: CacheWriteRequest<T>): void {
|
|
121
|
+
try {
|
|
122
|
+
request.provider.set(request.cacheKey, request.entry, request.ttl);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
logCacheEvent('cache.operation_failed', { ...request.logContext, operation: 'write', error });
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
logCacheEvent('cache.write_dispatched', { ...request.logContext, entryType: request.entryType });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 提交异常条目写入;同步 Provider 失败只记录基础设施错误,不遮蔽已经发生的业务异常。
|
|
132
|
+
* @param request 异常写入所需 Provider、entry、TTL 和稳定日志上下文。
|
|
133
|
+
* @returns 无返回值;异步写入 Promise 保持既有 fire-and-forget 边界。
|
|
134
|
+
*/
|
|
135
|
+
function dispatchCacheErrorWrite(request: CacheWriteRequest<never>): void {
|
|
136
|
+
try {
|
|
137
|
+
request.provider.set(request.cacheKey, request.entry, request.ttl);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
logCacheEvent('cache.operation_failed', { ...request.logContext, operation: 'write', error });
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
logCacheEvent('cache.write_dispatched', { ...request.logContext, entryType: 'error' });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function isErrorCacheEntry<T>(entry: CacheEntry<T>): entry is ErrorCacheEntry {
|
|
146
|
+
return typeof entry === 'object' && entry !== null && 'error' in entry;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 按当前异常策略分类 Provider 条目,并只把可成功解码的当前版本异常视为命中。
|
|
151
|
+
* @param entry Provider 返回的缓存联合条目。
|
|
152
|
+
* @param errorPolicy 当前装饰器归一化后的异常策略。
|
|
153
|
+
* @param logContext 当前方法的稳定日志上下文。
|
|
154
|
+
* @returns value/error 命中结果,或需要继续业务流程的 miss。
|
|
155
|
+
*/
|
|
156
|
+
function resolveCachedEntry<T>(
|
|
157
|
+
entry: CacheEntry<T>,
|
|
158
|
+
errorPolicy: NormalizedCacheErrorPolicy | undefined,
|
|
159
|
+
logContext: CacheLogContext
|
|
160
|
+
): CacheReadResult<T> {
|
|
161
|
+
if (!isErrorCacheEntry(entry)) {
|
|
162
|
+
logCacheEvent('cache.hit', { ...logContext, entryType: 'value' });
|
|
163
|
+
return { type: 'value', value: entry.value };
|
|
164
|
+
}
|
|
165
|
+
if (!isCurrentCacheErrorPayload(entry.error)) {
|
|
166
|
+
logCacheEvent('cache.error_cache_skipped', { ...logContext, reason: 'legacy_entry' });
|
|
167
|
+
return { type: 'miss' };
|
|
168
|
+
}
|
|
169
|
+
if (errorPolicy === undefined) {
|
|
170
|
+
logCacheEvent('cache.error_cache_skipped', { ...logContext, reason: 'disabled_entry' });
|
|
171
|
+
return { type: 'miss' };
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
const error = decodeCacheError(entry.error, errorPolicy);
|
|
175
|
+
logCacheEvent('cache.hit', { ...logContext, entryType: 'error' });
|
|
176
|
+
return { type: 'error', error };
|
|
177
|
+
} catch (_error) {
|
|
178
|
+
logCacheEvent('cache.error_cache_failed', { ...logContext, phase: 'decode' });
|
|
179
|
+
return { type: 'miss' };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 按固定顺序评估筛选器、编码异常并发起独立 TTL 写入,任何策略失败都保留原业务异常。
|
|
185
|
+
* @param request 本次业务异常、当前策略、Provider、完整 key 与稳定日志上下文。
|
|
186
|
+
* @returns 无返回值。
|
|
187
|
+
*/
|
|
188
|
+
function handleBusinessError(request: BusinessErrorRequest): void {
|
|
189
|
+
const { error, errorPolicy, provider, cacheKey, logContext } = request;
|
|
190
|
+
if (errorPolicy === undefined) {
|
|
191
|
+
logCacheEvent('cache.error_cache_skipped', { ...logContext, reason: 'disabled' });
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (errorPolicy.shouldCache !== undefined) {
|
|
195
|
+
let accepted: boolean;
|
|
196
|
+
try {
|
|
197
|
+
accepted = errorPolicy.shouldCache(error);
|
|
198
|
+
if (typeof accepted !== 'boolean') {
|
|
199
|
+
throw new TypeError('Cache error shouldCache must return a boolean');
|
|
200
|
+
}
|
|
201
|
+
} catch (_error) {
|
|
202
|
+
logCacheEvent('cache.error_cache_failed', { ...logContext, phase: 'predicate' });
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (!accepted) {
|
|
206
|
+
logCacheEvent('cache.error_cache_skipped', { ...logContext, reason: 'predicate_rejected' });
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
let entry: ErrorCacheEntry;
|
|
212
|
+
try {
|
|
213
|
+
entry = { error: encodeCacheError(error, errorPolicy) };
|
|
214
|
+
} catch (_error) {
|
|
215
|
+
logCacheEvent('cache.error_cache_failed', { ...logContext, phase: 'encode' });
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
dispatchCacheErrorWrite({ provider, cacheKey, entry, entryType: 'error', ttl: errorPolicy.ttl, logContext });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 解析缓存 key
|
|
223
|
+
* @param keyResolver key 解析器
|
|
224
|
+
* @param args 方法参数数组
|
|
225
|
+
* @param logContext 不包含业务参数和值的日志上下文
|
|
226
|
+
* @returns 解析后的缓存 key
|
|
227
|
+
*/
|
|
228
|
+
function resolveCacheKey(
|
|
229
|
+
keyResolver: CacheKeyResolver | undefined,
|
|
230
|
+
args: unknown[],
|
|
231
|
+
logContext: CacheLogContext
|
|
232
|
+
): string {
|
|
233
|
+
if (keyResolver === undefined || keyResolver === null) {
|
|
234
|
+
return KeyBuilder.build(logContext.cacheName, args);
|
|
235
|
+
}
|
|
236
|
+
if (typeof keyResolver === 'string') {
|
|
237
|
+
return KeyBuilder.build(logContext.cacheName, [keyResolver]);
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
return KeyBuilder.build(logContext.cacheName, [keyResolver(...args)]);
|
|
241
|
+
} catch (_error) {
|
|
242
|
+
logCacheEvent('cache.key_fallback', { ...logContext, reason: 'resolver_error' });
|
|
243
|
+
return KeyBuilder.build(logContext.cacheName, args);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 缓存装饰器
|
|
249
|
+
* 为方法添加声明式缓存功能,支持 TTL 过期和请求合并
|
|
250
|
+
* @param cacheName 缓存名称
|
|
251
|
+
* @param options 配置项
|
|
252
|
+
*/
|
|
253
|
+
export function Cache(
|
|
254
|
+
cacheName: string,
|
|
255
|
+
options?: CacheOptions
|
|
256
|
+
): (_target: object, _propertyKey: string, descriptor: PropertyDescriptor) => void {
|
|
257
|
+
const errorPolicy = normalizeCacheErrorPolicy(options?.errorCache);
|
|
258
|
+
return function <T>(_target: object, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
259
|
+
const originalMethod = descriptor.value;
|
|
260
|
+
const logContext: CacheLogContext = {
|
|
261
|
+
cacheName,
|
|
262
|
+
methodName: propertyKey,
|
|
263
|
+
providerName: cacheProviderLabel(options?.providerName),
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
descriptor.value = function (...args: unknown[]): Promise<T> {
|
|
267
|
+
const cacheKey = resolveCacheKey(options?.key, args, logContext);
|
|
268
|
+
|
|
269
|
+
const pending = pendingCache.get<T>(cacheKey);
|
|
270
|
+
if (pending) {
|
|
271
|
+
logCacheEvent('cache.pending_hit', logContext);
|
|
272
|
+
return pending;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const promise = (async () => {
|
|
276
|
+
const provider = resolveCacheProvider(options?.providerName, logContext);
|
|
277
|
+
let cached: CacheEntry<T> | undefined;
|
|
278
|
+
try {
|
|
279
|
+
cached = await provider.get<CacheEntry<T>>(cacheKey);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
logCacheEvent('cache.operation_failed', { ...logContext, operation: 'read', error });
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
if (cached !== undefined) {
|
|
285
|
+
const cachedResult = resolveCachedEntry(cached, errorPolicy, logContext);
|
|
286
|
+
if (cachedResult.type === 'value') {
|
|
287
|
+
return cachedResult.value;
|
|
288
|
+
}
|
|
289
|
+
if (cachedResult.type === 'error') {
|
|
290
|
+
throw cachedResult.error;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
logCacheEvent('cache.miss', logContext);
|
|
294
|
+
|
|
295
|
+
let result: T;
|
|
296
|
+
try {
|
|
297
|
+
result = (await originalMethod.apply(this, args)) as T;
|
|
298
|
+
} catch (error) {
|
|
299
|
+
handleBusinessError({ error, errorPolicy, provider, cacheKey, logContext });
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
dispatchCacheWrite({
|
|
303
|
+
provider,
|
|
304
|
+
cacheKey,
|
|
305
|
+
entry: { value: result },
|
|
306
|
+
entryType: 'value',
|
|
307
|
+
ttl: options?.ttl,
|
|
308
|
+
logContext,
|
|
309
|
+
});
|
|
310
|
+
return result;
|
|
311
|
+
})();
|
|
312
|
+
|
|
313
|
+
pendingCache.set(cacheKey, promise);
|
|
314
|
+
return promise;
|
|
315
|
+
};
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export { CacheProviderRegistry } from '../core/cache-provider-registry';
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export { createIoredisCacheClient } from './adapters/ioredis-cache-client';
|
|
2
|
-
export { createNodeRedisCacheClient } from './adapters/node-redis-cache-client';
|
|
3
|
-
export * from './core/cache-provider';
|
|
4
|
-
export * from './core/cache-provider-registry';
|
|
5
|
-
export * from './core/key-builder';
|
|
6
|
-
export * from './core/native-cache';
|
|
7
|
-
export * from './core/pending-cache';
|
|
8
|
-
export * from './core/redis-cache-client';
|
|
9
|
-
export * from './core/redis-cache';
|
|
10
|
-
export * from './decorators/cache';
|
|
11
|
-
export * from './decorators/cache-evict';
|
|
1
|
+
export { createIoredisCacheClient } from './adapters/ioredis-cache-client';
|
|
2
|
+
export { createNodeRedisCacheClient } from './adapters/node-redis-cache-client';
|
|
3
|
+
export * from './core/cache-provider';
|
|
4
|
+
export * from './core/cache-provider-registry';
|
|
5
|
+
export * from './core/key-builder';
|
|
6
|
+
export * from './core/native-cache';
|
|
7
|
+
export * from './core/pending-cache';
|
|
8
|
+
export * from './core/redis-cache-client';
|
|
9
|
+
export * from './core/redis-cache';
|
|
10
|
+
export * from './decorators/cache';
|
|
11
|
+
export * from './decorators/cache-evict';
|