@xfe-repo/bff-cache 1.0.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.
Files changed (41) hide show
  1. package/README.md +3 -0
  2. package/dist/cache-router.decorator.d.mts +3 -0
  3. package/dist/cache-router.decorator.d.ts +3 -0
  4. package/dist/cache-router.decorator.js +45 -0
  5. package/dist/cache-router.decorator.mjs +7 -0
  6. package/dist/cache-router.interceptor.d.mts +23 -0
  7. package/dist/cache-router.interceptor.d.ts +23 -0
  8. package/dist/cache-router.interceptor.js +372 -0
  9. package/dist/cache-router.interceptor.mjs +11 -0
  10. package/dist/cache-service.decorator.d.mts +7 -0
  11. package/dist/cache-service.decorator.d.ts +7 -0
  12. package/dist/cache-service.decorator.js +349 -0
  13. package/dist/cache-service.decorator.mjs +9 -0
  14. package/dist/cache.constants.d.mts +24 -0
  15. package/dist/cache.constants.d.ts +24 -0
  16. package/dist/cache.constants.js +66 -0
  17. package/dist/cache.constants.mjs +18 -0
  18. package/dist/cache.manager.service.d.mts +29 -0
  19. package/dist/cache.manager.service.d.ts +29 -0
  20. package/dist/cache.manager.service.js +298 -0
  21. package/dist/cache.manager.service.mjs +8 -0
  22. package/dist/cache.memory.service.d.mts +20 -0
  23. package/dist/cache.memory.service.d.ts +20 -0
  24. package/dist/cache.memory.service.js +144 -0
  25. package/dist/cache.memory.service.mjs +7 -0
  26. package/dist/cache.module.d.mts +15 -0
  27. package/dist/cache.module.d.ts +15 -0
  28. package/dist/cache.module.js +479 -0
  29. package/dist/cache.module.mjs +10 -0
  30. package/dist/chunk-4UPXDJBM.mjs +20 -0
  31. package/dist/chunk-6MEAHBZM.mjs +127 -0
  32. package/dist/chunk-DNWAJ23C.mjs +159 -0
  33. package/dist/chunk-FNYHTBRP.mjs +43 -0
  34. package/dist/chunk-IJR6EHQT.mjs +50 -0
  35. package/dist/chunk-JZT4EYON.mjs +107 -0
  36. package/dist/chunk-P44QFEDZ.mjs +83 -0
  37. package/dist/index.d.mts +11 -0
  38. package/dist/index.d.ts +11 -0
  39. package/dist/index.js +551 -0
  40. package/dist/index.mjs +36 -0
  41. package/package.json +54 -0
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@xfe-repo/bff-cache`
2
+
3
+ bff cache 缓存库
@@ -0,0 +1,3 @@
1
+ declare function CacheRouter(ttl?: number, cacheWhen?: boolean): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
2
+
3
+ export { CacheRouter };
@@ -0,0 +1,3 @@
1
+ declare function CacheRouter(ttl?: number, cacheWhen?: boolean): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
2
+
3
+ export { CacheRouter };
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/cache-router.decorator.ts
22
+ var cache_router_decorator_exports = {};
23
+ __export(cache_router_decorator_exports, {
24
+ CacheRouter: () => CacheRouter
25
+ });
26
+ module.exports = __toCommonJS(cache_router_decorator_exports);
27
+ var import_common = require("@nestjs/common");
28
+
29
+ // src/cache.constants.ts
30
+ var CACHE_WHEN = "CACHE_WHEN";
31
+ var CACHE_TTL = "CACHE_TTL";
32
+
33
+ // src/cache-router.decorator.ts
34
+ function CacheRouter(ttl = 60 * 1e3, cacheWhen = true) {
35
+ return (0, import_common.applyDecorators)(
36
+ // 是否缓存
37
+ (0, import_common.SetMetadata)(CACHE_WHEN, cacheWhen),
38
+ (0, import_common.SetMetadata)(CACHE_TTL, ttl)
39
+ );
40
+ }
41
+ __name(CacheRouter, "CacheRouter");
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ CacheRouter
45
+ });
@@ -0,0 +1,7 @@
1
+ import {
2
+ CacheRouter
3
+ } from "./chunk-4UPXDJBM.mjs";
4
+ import "./chunk-FNYHTBRP.mjs";
5
+ export {
6
+ CacheRouter
7
+ };
@@ -0,0 +1,23 @@
1
+ import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { CacheManagerService } from './cache.manager.service.mjs';
4
+ import '@nestjs/microservices';
5
+ import 'cacheable';
6
+ import './cache.constants.mjs';
7
+ import './cache.memory.service.mjs';
8
+ import 'keyv';
9
+
10
+ declare class Response<T> {
11
+ code: number;
12
+ data: T;
13
+ }
14
+ declare class CacheRouterInterceptor<T> implements NestInterceptor<T, Response<T>> {
15
+ private readonly reflector;
16
+ private cacheManagerService;
17
+ private readonly logger;
18
+ constructor(reflector: Reflector, cacheManagerService: CacheManagerService);
19
+ intercept(context: ExecutionContext, next: CallHandler): Promise<any>;
20
+ private renderCacheKey;
21
+ }
22
+
23
+ export { CacheRouterInterceptor, Response };
@@ -0,0 +1,23 @@
1
+ import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { CacheManagerService } from './cache.manager.service.js';
4
+ import '@nestjs/microservices';
5
+ import 'cacheable';
6
+ import './cache.constants.js';
7
+ import './cache.memory.service.js';
8
+ import 'keyv';
9
+
10
+ declare class Response<T> {
11
+ code: number;
12
+ data: T;
13
+ }
14
+ declare class CacheRouterInterceptor<T> implements NestInterceptor<T, Response<T>> {
15
+ private readonly reflector;
16
+ private cacheManagerService;
17
+ private readonly logger;
18
+ constructor(reflector: Reflector, cacheManagerService: CacheManagerService);
19
+ intercept(context: ExecutionContext, next: CallHandler): Promise<any>;
20
+ private renderCacheKey;
21
+ }
22
+
23
+ export { CacheRouterInterceptor, Response };
@@ -0,0 +1,372 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+
23
+ // src/cache-router.interceptor.ts
24
+ var cache_router_interceptor_exports = {};
25
+ __export(cache_router_interceptor_exports, {
26
+ CacheRouterInterceptor: () => CacheRouterInterceptor,
27
+ Response: () => Response
28
+ });
29
+ module.exports = __toCommonJS(cache_router_interceptor_exports);
30
+ var import_common3 = require("@nestjs/common");
31
+ var import_core = require("@nestjs/core");
32
+ var import_rxjs = require("rxjs");
33
+ var import_operators = require("rxjs/operators");
34
+
35
+ // src/cache.constants.ts
36
+ var CACHE_MANAGER = "CACHE_MANAGER";
37
+ var MEMORY_CACHE_SERVICE = "MEMORY_CACHE_SERVICE";
38
+ var REDIS_CLIENT_SERVICE = "REDIS_CLIENT_SERVICE";
39
+ var CACHE_WHEN = "CACHE_WHEN";
40
+ var CACHE_TTL = "CACHE_TTL";
41
+ var REDIS_PUB = {
42
+ /** Redis 发布订阅频道 */
43
+ CHANNEL: "CACHE_CHANNEL",
44
+ /** Redis 发布订阅事件 */
45
+ EVENTS: {
46
+ SET: "SET",
47
+ MSET: "MSET",
48
+ DEL: "DEL",
49
+ MDEL: "MDEL",
50
+ CLEAR: "CLEAR"
51
+ }
52
+ };
53
+ var CachePrefix = /* @__PURE__ */ function(CachePrefix2) {
54
+ CachePrefix2["User"] = "User";
55
+ CachePrefix2["QRCode"] = "QRCode";
56
+ CachePrefix2["Router"] = "Router";
57
+ CachePrefix2["Service"] = "Service";
58
+ CachePrefix2["UnionQuery"] = "UnionQuery";
59
+ return CachePrefix2;
60
+ }({});
61
+
62
+ // src/cache.manager.service.ts
63
+ var import_bff_utils = require("@xfe-repo/bff-utils");
64
+ var import_common2 = require("@nestjs/common");
65
+ var import_microservices2 = require("@nestjs/microservices");
66
+ var import_cacheable = require("cacheable");
67
+
68
+ // src/cache.memory.service.ts
69
+ var import_common = require("@nestjs/common");
70
+ var import_microservices = require("@nestjs/microservices");
71
+ var import_keyv = require("keyv");
72
+ function _ts_decorate(decorators, target, key, desc) {
73
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
74
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
75
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
76
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
77
+ }
78
+ __name(_ts_decorate, "_ts_decorate");
79
+ function _ts_metadata(k, v) {
80
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
81
+ }
82
+ __name(_ts_metadata, "_ts_metadata");
83
+ function _ts_param(paramIndex, decorator) {
84
+ return function(target, key) {
85
+ decorator(target, key, paramIndex);
86
+ };
87
+ }
88
+ __name(_ts_param, "_ts_param");
89
+ var _CacheMemoryService = class _CacheMemoryService extends import_keyv.Keyv {
90
+ constructor(keyvOptions) {
91
+ super(keyvOptions);
92
+ __publicField(this, "logger", new import_common.Logger(_CacheMemoryService.name));
93
+ }
94
+ setEventPattern(item) {
95
+ this.logger.log(`EventPattern set: ${item.key}`);
96
+ this.set(item.key, item.value, item.ttl);
97
+ }
98
+ msetEventPattern(list) {
99
+ this.logger.log(`EventPattern mset: ${list.map((item) => item.key).join(", ")}`);
100
+ this.setMany(list);
101
+ }
102
+ delEventPattern(key) {
103
+ this.logger.log(`EventPattern del: ${key}`);
104
+ return this.delete(key);
105
+ }
106
+ mdelEventPattern(keys) {
107
+ this.logger.log(`EventPattern mdel: ${keys.join(", ")}`);
108
+ return this.deleteMany(keys);
109
+ }
110
+ clearEventPattern() {
111
+ this.logger.log(`EventPattern clear`);
112
+ return this.clear();
113
+ }
114
+ };
115
+ __name(_CacheMemoryService, "CacheMemoryService");
116
+ var CacheMemoryService = _CacheMemoryService;
117
+ _ts_decorate([
118
+ (0, import_microservices.EventPattern)(REDIS_PUB.EVENTS.SET),
119
+ _ts_metadata("design:type", Function),
120
+ _ts_metadata("design:paramtypes", [
121
+ typeof SetOptions === "undefined" ? Object : SetOptions
122
+ ]),
123
+ _ts_metadata("design:returntype", void 0)
124
+ ], CacheMemoryService.prototype, "setEventPattern", null);
125
+ _ts_decorate([
126
+ (0, import_microservices.EventPattern)(REDIS_PUB.EVENTS.MSET),
127
+ _ts_metadata("design:type", Function),
128
+ _ts_metadata("design:paramtypes", [
129
+ typeof MSetOptions === "undefined" ? Object : MSetOptions
130
+ ]),
131
+ _ts_metadata("design:returntype", void 0)
132
+ ], CacheMemoryService.prototype, "msetEventPattern", null);
133
+ _ts_decorate([
134
+ (0, import_microservices.EventPattern)(REDIS_PUB.EVENTS.DEL),
135
+ _ts_metadata("design:type", Function),
136
+ _ts_metadata("design:paramtypes", [
137
+ String
138
+ ]),
139
+ _ts_metadata("design:returntype", void 0)
140
+ ], CacheMemoryService.prototype, "delEventPattern", null);
141
+ _ts_decorate([
142
+ (0, import_microservices.EventPattern)(REDIS_PUB.EVENTS.MDEL),
143
+ _ts_metadata("design:type", Function),
144
+ _ts_metadata("design:paramtypes", [
145
+ Array
146
+ ]),
147
+ _ts_metadata("design:returntype", void 0)
148
+ ], CacheMemoryService.prototype, "mdelEventPattern", null);
149
+ _ts_decorate([
150
+ (0, import_microservices.EventPattern)(REDIS_PUB.EVENTS.CLEAR),
151
+ _ts_metadata("design:type", Function),
152
+ _ts_metadata("design:paramtypes", []),
153
+ _ts_metadata("design:returntype", void 0)
154
+ ], CacheMemoryService.prototype, "clearEventPattern", null);
155
+ CacheMemoryService = _ts_decorate([
156
+ (0, import_common.Injectable)(),
157
+ (0, import_common.Controller)(),
158
+ _ts_param(0, (0, import_common.Inject)(MEMORY_CACHE_SERVICE)),
159
+ _ts_metadata("design:type", Function),
160
+ _ts_metadata("design:paramtypes", [
161
+ typeof import_keyv.KeyvOptions === "undefined" ? Object : import_keyv.KeyvOptions
162
+ ])
163
+ ], CacheMemoryService);
164
+
165
+ // src/cache.manager.service.ts
166
+ function _ts_decorate2(decorators, target, key, desc) {
167
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
168
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
169
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
170
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
171
+ }
172
+ __name(_ts_decorate2, "_ts_decorate");
173
+ function _ts_metadata2(k, v) {
174
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
175
+ }
176
+ __name(_ts_metadata2, "_ts_metadata");
177
+ function _ts_param2(paramIndex, decorator) {
178
+ return function(target, key) {
179
+ decorator(target, key, paramIndex);
180
+ };
181
+ }
182
+ __name(_ts_param2, "_ts_param");
183
+ var _CacheManagerService = class _CacheManagerService {
184
+ constructor(redisPublisher, cacheRedis, cacheMemory) {
185
+ __publicField(this, "redisPublisher");
186
+ __publicField(this, "cacheRedis");
187
+ __publicField(this, "cacheMemory");
188
+ this.redisPublisher = redisPublisher;
189
+ this.cacheRedis = cacheRedis;
190
+ this.cacheMemory = cacheMemory;
191
+ }
192
+ getKey(prefix, key) {
193
+ return `${prefix}:${key}`;
194
+ }
195
+ getMKey(prefix, key) {
196
+ return key.map((_key) => this.getKey(prefix, _key));
197
+ }
198
+ async get(prefix, key) {
199
+ const keyWithPrefix = this.getKey(prefix, key);
200
+ const memoryResult = await this.cacheMemory.getRaw(keyWithPrefix);
201
+ if (memoryResult !== void 0) return memoryResult.value;
202
+ const redisResult = await this.cacheRedis.getRaw(keyWithPrefix);
203
+ if (redisResult !== void 0) {
204
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.SET, {
205
+ key: this.getKey(prefix, key),
206
+ value: redisResult.value,
207
+ ttl: redisResult.expires
208
+ });
209
+ }
210
+ return redisResult?.value;
211
+ }
212
+ async mget(prefix, key) {
213
+ const keyWithPrefix = this.getMKey(prefix, key);
214
+ const memoryResult = await this.cacheMemory.getManyRaw(keyWithPrefix);
215
+ if (memoryResult.every((item) => item !== void 0)) {
216
+ return memoryResult.map((item) => item?.value);
217
+ }
218
+ const missingKeys = keyWithPrefix.filter((_, i) => memoryResult[i] === void 0);
219
+ const redisResult = await this.cacheRedis.getManyRaw(missingKeys);
220
+ const missingMemoryCaches = [];
221
+ for (const [i, key2] of keyWithPrefix.entries()) {
222
+ if (memoryResult[i] === void 0 && redisResult[i] !== void 0) {
223
+ memoryResult[i] = redisResult[i];
224
+ missingMemoryCaches.push({
225
+ key: key2,
226
+ value: redisResult[i].value,
227
+ ttl: redisResult[i].expires
228
+ });
229
+ }
230
+ }
231
+ if (missingMemoryCaches.length > 0) {
232
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.MSET, missingMemoryCaches);
233
+ }
234
+ return memoryResult.map((item) => item?.value);
235
+ }
236
+ set(prefix, key, value, ttl) {
237
+ const keyWithPrefix = this.getKey(prefix, key);
238
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.SET, {
239
+ key: keyWithPrefix,
240
+ value,
241
+ ttl
242
+ });
243
+ return this.cacheRedis.set(keyWithPrefix, value, ttl);
244
+ }
245
+ mset(prefix, list, ttl) {
246
+ const listItems = list.map((item) => ({
247
+ key: this.getKey(prefix, item.key),
248
+ value: item.value,
249
+ ttl
250
+ }));
251
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.MSET, listItems);
252
+ return this.cacheRedis.setMany(listItems);
253
+ }
254
+ del(prefix, key) {
255
+ const keyWithPrefix = this.getKey(prefix, key);
256
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.DEL, {
257
+ key: keyWithPrefix
258
+ });
259
+ return this.cacheRedis.delete(keyWithPrefix);
260
+ }
261
+ mdel(prefix, key) {
262
+ const keyWithPrefix = this.getMKey(prefix, key);
263
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.MDEL, {
264
+ keys: keyWithPrefix
265
+ });
266
+ return this.cacheRedis.deleteMany(keyWithPrefix);
267
+ }
268
+ clear() {
269
+ this.redisPublisher.emit(REDIS_PUB.EVENTS.CLEAR, {});
270
+ return this.cacheRedis.clear();
271
+ }
272
+ };
273
+ __name(_CacheManagerService, "CacheManagerService");
274
+ var CacheManagerService = _CacheManagerService;
275
+ _ts_decorate2([
276
+ (0, import_bff_utils.timeIt)(),
277
+ _ts_metadata2("design:type", Function),
278
+ _ts_metadata2("design:paramtypes", [
279
+ typeof CachePrefix === "undefined" ? Object : CachePrefix,
280
+ String
281
+ ]),
282
+ _ts_metadata2("design:returntype", Promise)
283
+ ], CacheManagerService.prototype, "get", null);
284
+ _ts_decorate2([
285
+ (0, import_bff_utils.timeIt)(),
286
+ _ts_metadata2("design:type", Function),
287
+ _ts_metadata2("design:paramtypes", [
288
+ typeof CachePrefix === "undefined" ? Object : CachePrefix,
289
+ Array
290
+ ]),
291
+ _ts_metadata2("design:returntype", Promise)
292
+ ], CacheManagerService.prototype, "mget", null);
293
+ CacheManagerService = _ts_decorate2([
294
+ (0, import_common2.Injectable)(),
295
+ _ts_param2(0, (0, import_common2.Inject)(REDIS_CLIENT_SERVICE)),
296
+ _ts_param2(1, (0, import_common2.Inject)(CACHE_MANAGER)),
297
+ _ts_metadata2("design:type", Function),
298
+ _ts_metadata2("design:paramtypes", [
299
+ typeof import_microservices2.ClientProxy === "undefined" ? Object : import_microservices2.ClientProxy,
300
+ typeof import_cacheable.Cacheable === "undefined" ? Object : import_cacheable.Cacheable,
301
+ typeof CacheMemoryService === "undefined" ? Object : CacheMemoryService
302
+ ])
303
+ ], CacheManagerService);
304
+
305
+ // src/cache-router.interceptor.ts
306
+ function _ts_decorate3(decorators, target, key, desc) {
307
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
308
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
309
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
310
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
311
+ }
312
+ __name(_ts_decorate3, "_ts_decorate");
313
+ function _ts_metadata3(k, v) {
314
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
315
+ }
316
+ __name(_ts_metadata3, "_ts_metadata");
317
+ var _Response = class _Response {
318
+ constructor() {
319
+ __publicField(this, "code");
320
+ __publicField(this, "data");
321
+ }
322
+ };
323
+ __name(_Response, "Response");
324
+ var Response = _Response;
325
+ var _CacheRouterInterceptor = class _CacheRouterInterceptor {
326
+ constructor(reflector, cacheManagerService) {
327
+ __publicField(this, "reflector");
328
+ __publicField(this, "cacheManagerService");
329
+ __publicField(this, "logger", new import_common3.Logger(_CacheRouterInterceptor.name));
330
+ this.reflector = reflector;
331
+ this.cacheManagerService = cacheManagerService;
332
+ }
333
+ async intercept(context, next) {
334
+ const request = context.switchToHttp().getRequest();
335
+ const whenApiCache = this.reflector.get(CACHE_WHEN, context.getHandler()) || this.reflector.get(CACHE_WHEN, context.getClass());
336
+ if (whenApiCache) {
337
+ const ttl = this.reflector.get(CACHE_TTL, context.getHandler()) || this.reflector.get(CACHE_TTL, context.getClass());
338
+ const apiCacheKey = this.renderCacheKey(request.method, request.url);
339
+ const cachedItem = await this.cacheManagerService.get(CachePrefix.Router, apiCacheKey);
340
+ if (cachedItem) {
341
+ this.logger.log(`RouterCache hit for key: ${apiCacheKey}`);
342
+ return (0, import_rxjs.of)(cachedItem);
343
+ }
344
+ return next.handle().pipe((0, import_operators.map)((data) => {
345
+ this.cacheManagerService.set(CachePrefix.Router, apiCacheKey, data, ttl);
346
+ return data;
347
+ }));
348
+ }
349
+ return next.handle();
350
+ }
351
+ renderCacheKey(method, url) {
352
+ if (method !== "GET") {
353
+ throw new import_common3.BadRequestException(`RouterCache \u4E0D\u652F\u6301 ${method} \u65B9\u6CD5-${url}`);
354
+ }
355
+ return `${method}:${url}`;
356
+ }
357
+ };
358
+ __name(_CacheRouterInterceptor, "CacheRouterInterceptor");
359
+ var CacheRouterInterceptor = _CacheRouterInterceptor;
360
+ CacheRouterInterceptor = _ts_decorate3([
361
+ (0, import_common3.Injectable)(),
362
+ _ts_metadata3("design:type", Function),
363
+ _ts_metadata3("design:paramtypes", [
364
+ typeof import_core.Reflector === "undefined" ? Object : import_core.Reflector,
365
+ typeof CacheManagerService === "undefined" ? Object : CacheManagerService
366
+ ])
367
+ ], CacheRouterInterceptor);
368
+ // Annotate the CommonJS export names for ESM import in node:
369
+ 0 && (module.exports = {
370
+ CacheRouterInterceptor,
371
+ Response
372
+ });
@@ -0,0 +1,11 @@
1
+ import {
2
+ CacheRouterInterceptor,
3
+ Response
4
+ } from "./chunk-P44QFEDZ.mjs";
5
+ import "./chunk-DNWAJ23C.mjs";
6
+ import "./chunk-JZT4EYON.mjs";
7
+ import "./chunk-FNYHTBRP.mjs";
8
+ export {
9
+ CacheRouterInterceptor,
10
+ Response
11
+ };
@@ -0,0 +1,7 @@
1
+ interface CacheServiceOptions {
2
+ cacheKey?: string;
3
+ ttl?: number;
4
+ }
5
+ declare function CacheService(options?: CacheServiceOptions): MethodDecorator;
6
+
7
+ export { CacheService };
@@ -0,0 +1,7 @@
1
+ interface CacheServiceOptions {
2
+ cacheKey?: string;
3
+ ttl?: number;
4
+ }
5
+ declare function CacheService(options?: CacheServiceOptions): MethodDecorator;
6
+
7
+ export { CacheService };