create-dp-koa 1.1.1 → 1.1.3

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 (84) hide show
  1. package/package.json +1 -1
  2. package/template/scripts/sync-template.mjs +21 -0
  3. package/template/src/app.ts +1 -2
  4. package/template/src/{framework/plugins → plugins}/registry.ts +2 -2
  5. package/template/src/plugins/weboffice/http/routes.ts +2 -2
  6. package/template/src/plugins/weboffice/index.ts +3 -3
  7. package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +3 -4
  8. package/template/src/utils/testDataInitializer.ts +1 -1
  9. package/template/tsconfig.json +6 -0
  10. package/template/src/annotations/decorators/ConfigManagement.ts +0 -9
  11. package/template/src/annotations/decorators/DistributedTracing.ts +0 -9
  12. package/template/src/annotations/decorators/EnterprisePerformance.ts +0 -9
  13. package/template/src/annotations/decorators/PerformanceMonitor.ts +0 -32
  14. package/template/src/annotations/decorators/SecurityAudit.ts +0 -9
  15. package/template/src/annotations/index.ts +0 -50
  16. package/template/src/annotations/processors/ConfigManagementProcessor.ts +0 -369
  17. package/template/src/annotations/processors/DistributedTracingProcessor.ts +0 -288
  18. package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +0 -189
  19. package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +0 -101
  20. package/template/src/annotations/processors/SecurityAuditProcessor.ts +0 -345
  21. package/template/src/annotations/processors/SwaggerProcessor.ts +0 -612
  22. package/template/src/annotations/processors/index.ts +0 -10
  23. package/template/src/examples/InterceptorExampleRunner.ts +0 -284
  24. package/template/src/examples/ServiceInterceptorExample.ts +0 -214
  25. package/template/src/examples/cacheExamples.ts +0 -155
  26. package/template/src/framework/decorator/controller.ts +0 -311
  27. package/template/src/framework/decorator/processor/AnnotationDecorators.ts +0 -100
  28. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +0 -160
  29. package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +0 -45
  30. package/template/src/framework/decorator/processor/AnnotationRegistry.ts +0 -117
  31. package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +0 -95
  32. package/template/src/framework/decorator/processor/ProcessorManager.ts +0 -76
  33. package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +0 -126
  34. package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +0 -207
  35. package/template/src/framework/decorator/refactored/DecoratorFactory.ts +0 -99
  36. package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +0 -125
  37. package/template/src/framework/decorator/refactored/DecoratorValidator.ts +0 -128
  38. package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +0 -139
  39. package/template/src/framework/decorator/refactored/index.ts +0 -98
  40. package/template/src/framework/decorator/swagger.ts +0 -150
  41. package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +0 -375
  42. package/template/src/framework/interceptors/ServiceCallInterceptor.ts +0 -348
  43. package/template/src/framework/interceptors/index.ts +0 -19
  44. package/template/src/framework/plugins/types.ts +0 -15
  45. package/template/src/framework/types/ServiceResult.ts +0 -151
  46. package/template/src/framework/types/index.ts +0 -16
  47. package/template/src/framework/utils/CacheManager.ts +0 -430
  48. package/template/src/framework/utils/CacheService.ts +0 -248
  49. package/template/src/framework/utils/DtoValidator.ts +0 -164
  50. package/template/src/framework/utils/MigrationHelper.ts +0 -179
  51. package/template/src/framework/utils/MigrationManager.ts +0 -256
  52. package/template/src/framework/utils/NewRouter.ts +0 -207
  53. package/template/src/framework/utils/TransactionManager.ts +0 -172
  54. package/template/src/framework/utils/bootstrap.ts +0 -445
  55. package/template/src/framework/utils/cache.ts +0 -269
  56. package/template/src/framework/utils/databaseConfig.ts +0 -148
  57. package/template/src/framework/utils/db.ts +0 -39
  58. package/template/src/framework/utils/dbMonitor.ts +0 -106
  59. package/template/src/framework/utils/function.ts +0 -61
  60. package/template/src/framework/utils/gracefulShutdown.ts +0 -131
  61. package/template/src/framework/utils/logger.ts +0 -388
  62. package/template/src/framework/utils/metrics.ts +0 -182
  63. package/template/src/framework/utils/router.ts +0 -417
  64. package/template/src/framework/utils/swagger.ts +0 -184
  65. package/template/src/framework/utils/testDb.ts +0 -19
  66. package/template/src/framework/utils/token.ts +0 -23
  67. package/template/src/framework/utils/transform.ts +0 -17
  68. package/template/src/libs/aokEmailSender.ts +0 -42
  69. package/template/src/libs/captcha.ts +0 -37
  70. package/template/src/libs/cos.ts +0 -45
  71. package/template/src/libs/mCache.ts +0 -7
  72. package/template/src/libs/serviceValidate.ts +0 -3
  73. package/template/src/libs/tecentSms.ts +0 -51
  74. package/template/src/middlewares/a.middleware.ts +0 -6
  75. package/template/src/middlewares/error.middleware.ts +0 -14
  76. package/template/src/middlewares/logging.middleware.ts +0 -187
  77. package/template/src/middlewares/static.middleware.ts +0 -79
  78. package/template/src/middlewares/swagger.middleware.ts +0 -70
  79. package/template/src/middlewares/token.middleware.ts +0 -32
  80. package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +0 -172
  81. package/template/src/migrations/index.ts +0 -6
  82. package/template/src/repository/base/BaseRepository.ts +0 -124
  83. package/template/src/repository/interfaces/IBaseRepository.ts +0 -67
  84. package/template/src/service/base.service.ts +0 -116
@@ -1,430 +0,0 @@
1
- import { logger } from '@src/framework/utils/logger';
2
-
3
- /**
4
- * 缓存接口
5
- * 定义通用的缓存操作
6
- */
7
- export interface ICache {
8
- /**
9
- * 获取缓存值
10
- */
11
- get<T>(key: string): Promise<T | null>;
12
-
13
- /**
14
- * 设置缓存值
15
- */
16
- set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
17
-
18
- /**
19
- * 删除缓存
20
- */
21
- delete(key: string): Promise<boolean>;
22
-
23
- /**
24
- * 检查缓存是否存在
25
- */
26
- exists(key: string): Promise<boolean>;
27
-
28
- /**
29
- * 清空所有缓存
30
- */
31
- clear(): Promise<boolean>;
32
-
33
- /**
34
- * 获取多个缓存值
35
- */
36
- mget<T>(keys: string[]): Promise<(T | null)[]>;
37
-
38
- /**
39
- * 设置多个缓存值
40
- */
41
- mset<T>(keyValuePairs: Array<{ key: string; value: T; ttl?: number }>): Promise<boolean>;
42
-
43
- /**
44
- * 删除多个缓存
45
- */
46
- mdelete(keys: string[]): Promise<boolean>;
47
-
48
- /**
49
- * 获取缓存剩余过期时间
50
- */
51
- ttl(key: string): Promise<number>;
52
-
53
- /**
54
- * 设置缓存过期时间
55
- */
56
- expire(key: string, ttl: number): Promise<boolean>;
57
- }
58
-
59
- /**
60
- * 内存缓存实现
61
- * 用于开发环境和简单场景
62
- */
63
- export class MemoryCache implements ICache {
64
- private cache: Map<string, { value: any; expiresAt?: number }> = new Map();
65
- private static instance: MemoryCache;
66
-
67
- private constructor() {}
68
-
69
- static getInstance(): MemoryCache {
70
- if (!MemoryCache.instance) {
71
- MemoryCache.instance = new MemoryCache();
72
- }
73
- return MemoryCache.instance;
74
- }
75
-
76
- async get<T>(key: string): Promise<T | null> {
77
- const item = this.cache.get(key);
78
- if (!item) {
79
- return null;
80
- }
81
-
82
- // 检查是否过期
83
- if (item.expiresAt && Date.now() > item.expiresAt) {
84
- this.cache.delete(key);
85
- return null;
86
- }
87
-
88
- return item.value as T;
89
- }
90
-
91
- async set<T>(key: string, value: T, ttl?: number): Promise<boolean> {
92
- try {
93
- const expiresAt = ttl ? Date.now() + (ttl * 1000) : undefined;
94
- this.cache.set(key, { value, expiresAt });
95
- return true;
96
- } catch (error) {
97
- logger.error('设置缓存失败:', error as Error);
98
- return false;
99
- }
100
- }
101
-
102
- async delete(key: string): Promise<boolean> {
103
- return this.cache.delete(key);
104
- }
105
-
106
- async exists(key: string): Promise<boolean> {
107
- const item = this.cache.get(key);
108
- if (!item) {
109
- return false;
110
- }
111
-
112
- // 检查是否过期
113
- if (item.expiresAt && Date.now() > item.expiresAt) {
114
- this.cache.delete(key);
115
- return false;
116
- }
117
-
118
- return true;
119
- }
120
-
121
- async clear(): Promise<boolean> {
122
- this.cache.clear();
123
- return true;
124
- }
125
-
126
- async mget<T>(keys: string[]): Promise<(T | null)[]> {
127
- return Promise.all(keys.map(key => this.get<T>(key)));
128
- }
129
-
130
- async mset<T>(keyValuePairs: Array<{ key: string; value: T; ttl?: number }>): Promise<boolean> {
131
- try {
132
- for (const { key, value, ttl } of keyValuePairs) {
133
- await this.set(key, value, ttl);
134
- }
135
- return true;
136
- } catch (error) {
137
- logger.error('批量设置缓存失败:', error as Error);
138
- return false;
139
- }
140
- }
141
-
142
- async mdelete(keys: string[]): Promise<boolean> {
143
- try {
144
- for (const key of keys) {
145
- await this.delete(key);
146
- }
147
- return true;
148
- } catch (error) {
149
- logger.error('批量删除缓存失败:', error as Error);
150
- return false;
151
- }
152
- }
153
-
154
- async ttl(key: string): Promise<number> {
155
- const item = this.cache.get(key);
156
- if (!item || !item.expiresAt) {
157
- return -1;
158
- }
159
-
160
- const remaining = Math.ceil((item.expiresAt - Date.now()) / 1000);
161
- return remaining > 0 ? remaining : -2;
162
- }
163
-
164
- async expire(key: string, ttl: number): Promise<boolean> {
165
- const item = this.cache.get(key);
166
- if (!item) {
167
- return false;
168
- }
169
-
170
- item.expiresAt = Date.now() + (ttl * 1000);
171
- return true;
172
- }
173
-
174
- /**
175
- * 获取缓存统计信息
176
- */
177
- getStats(): { size: number; keys: string[] } {
178
- return {
179
- size: this.cache.size,
180
- keys: Array.from(this.cache.keys())
181
- };
182
- }
183
- }
184
-
185
- /**
186
- * Redis缓存实现
187
- * 用于生产环境
188
- */
189
- export class RedisCache implements ICache {
190
- private redis: any;
191
- private static instance: RedisCache;
192
-
193
- private constructor() {}
194
-
195
- static getInstance(): RedisCache {
196
- if (!RedisCache.instance) {
197
- RedisCache.instance = new RedisCache();
198
- }
199
- return RedisCache.instance;
200
- }
201
-
202
- /**
203
- * 初始化Redis连接
204
- */
205
- async initialize(): Promise<void> {
206
- try {
207
- // 这里应该根据实际的Redis客户端库进行初始化
208
- // const Redis = require('ioredis');
209
- // this.redis = new Redis({
210
- // host: process.env.REDIS_HOST || 'localhost',
211
- // port: parseInt(process.env.REDIS_PORT || '6379'),
212
- // password: process.env.REDIS_PASSWORD,
213
- // db: parseInt(process.env.REDIS_DB || '0'),
214
- // });
215
- logger.info('Redis缓存初始化成功');
216
- } catch (error) {
217
- logger.error('Redis缓存初始化失败:', error as Error);
218
- throw error;
219
- }
220
- }
221
-
222
- async get<T>(key: string): Promise<T | null> {
223
- try {
224
- const value = await this.redis.get(key);
225
- return value ? JSON.parse(value) : null;
226
- } catch (error) {
227
- logger.error('获取Redis缓存失败:', error as Error);
228
- return null;
229
- }
230
- }
231
-
232
- async set<T>(key: string, value: T, ttl?: number): Promise<boolean> {
233
- try {
234
- const serializedValue = JSON.stringify(value);
235
- if (ttl) {
236
- await this.redis.setex(key, ttl, serializedValue);
237
- } else {
238
- await this.redis.set(key, serializedValue);
239
- }
240
- return true;
241
- } catch (error) {
242
- logger.error('设置Redis缓存失败:', error as Error);
243
- return false;
244
- }
245
- }
246
-
247
- async delete(key: string): Promise<boolean> {
248
- try {
249
- const result = await this.redis.del(key);
250
- return result > 0;
251
- } catch (error) {
252
- logger.error('删除Redis缓存失败:', error as Error);
253
- return false;
254
- }
255
- }
256
-
257
- async exists(key: string): Promise<boolean> {
258
- try {
259
- const result = await this.redis.exists(key);
260
- return result === 1;
261
- } catch (error) {
262
- logger.error('检查Redis缓存存在性失败:', error as Error);
263
- return false;
264
- }
265
- }
266
-
267
- async clear(): Promise<boolean> {
268
- try {
269
- await this.redis.flushdb();
270
- return true;
271
- } catch (error) {
272
- logger.error('清空Redis缓存失败:', error as Error);
273
- return false;
274
- }
275
- }
276
-
277
- async mget<T>(keys: string[]): Promise<(T | null)[]> {
278
- try {
279
- const values = await this.redis.mget(...keys);
280
- return values.map((value: string) => value ? JSON.parse(value) : null);
281
- } catch (error) {
282
- logger.error('批量获取Redis缓存失败:', error as Error);
283
- return keys.map(() => null);
284
- }
285
- }
286
-
287
- async mset<T>(keyValuePairs: Array<{ key: string; value: T; ttl?: number }>): Promise<boolean> {
288
- try {
289
- const pipeline = this.redis.pipeline();
290
-
291
- for (const { key, value, ttl } of keyValuePairs) {
292
- const serializedValue = JSON.stringify(value);
293
- if (ttl) {
294
- pipeline.setex(key, ttl, serializedValue);
295
- } else {
296
- pipeline.set(key, serializedValue);
297
- }
298
- }
299
-
300
- await pipeline.exec();
301
- return true;
302
- } catch (error) {
303
- logger.error('批量设置Redis缓存失败:', error as Error);
304
- return false;
305
- }
306
- }
307
-
308
- async mdelete(keys: string[]): Promise<boolean> {
309
- try {
310
- const result = await this.redis.del(...keys);
311
- return result > 0;
312
- } catch (error) {
313
- logger.error('批量删除Redis缓存失败:', error as Error);
314
- return false;
315
- }
316
- }
317
-
318
- async ttl(key: string): Promise<number> {
319
- try {
320
- return await this.redis.ttl(key);
321
- } catch (error) {
322
- logger.error('获取Redis缓存TTL失败:', error as Error);
323
- return -1;
324
- }
325
- }
326
-
327
- async expire(key: string, ttl: number): Promise<boolean> {
328
- try {
329
- const result = await this.redis.expire(key, ttl);
330
- return result === 1;
331
- } catch (error) {
332
- logger.error('设置Redis缓存过期时间失败:', error as Error);
333
- return false;
334
- }
335
- }
336
- }
337
-
338
- /**
339
- * 缓存管理器
340
- * 提供统一的缓存接口和策略
341
- */
342
- export class CacheManager {
343
- private static instance: CacheManager;
344
- private cache: ICache;
345
- private cacheType: 'memory' | 'redis';
346
-
347
- private constructor() {
348
- this.cacheType = process.env.CACHE_TYPE === 'redis' ? 'redis' : 'memory';
349
- this.initializeCache();
350
- }
351
-
352
- static getInstance(): CacheManager {
353
- if (!CacheManager.instance) {
354
- CacheManager.instance = new CacheManager();
355
- }
356
- return CacheManager.instance;
357
- }
358
-
359
- /**
360
- * 初始化缓存
361
- */
362
- private initializeCache(): void {
363
- if (this.cacheType === 'redis') {
364
- this.cache = RedisCache.getInstance();
365
- } else {
366
- this.cache = MemoryCache.getInstance();
367
- }
368
-
369
- logger.info(`缓存系统初始化完成,类型: ${this.cacheType}`);
370
- }
371
-
372
- /**
373
- * 获取缓存实例
374
- */
375
- getCache(): ICache {
376
- return this.cache;
377
- }
378
-
379
- /**
380
- * 获取缓存类型
381
- */
382
- getCacheType(): string {
383
- return this.cacheType;
384
- }
385
-
386
- /**
387
- * 生成缓存键
388
- */
389
- generateKey(prefix: string, ...parts: (string | number)[]): string {
390
- return `${prefix}:${parts.join(':')}`;
391
- }
392
-
393
- /**
394
- * 用户缓存键生成器
395
- */
396
- getUserCacheKey(userId: number): string {
397
- return this.generateKey('user', userId);
398
- }
399
-
400
- /**
401
- * 商品缓存键生成器
402
- */
403
- getGoodsCacheKey(goodsId: number): string {
404
- return this.generateKey('goods', goodsId);
405
- }
406
-
407
- /**
408
- * 店铺缓存键生成器
409
- */
410
- getShopCacheKey(shopId: number): string {
411
- return this.generateKey('shop', shopId);
412
- }
413
-
414
- /**
415
- * 清空所有缓存
416
- */
417
- async clearAllCache(): Promise<boolean> {
418
- try {
419
- await this.cache.clear();
420
- logger.info('所有缓存已清空');
421
- return true;
422
- } catch (error) {
423
- logger.error('清空缓存失败:', error as Error);
424
- return false;
425
- }
426
- }
427
- }
428
-
429
- // 导出单例实例
430
- export const cacheManager = CacheManager.getInstance();
@@ -1,248 +0,0 @@
1
- import { Injectable } from "dp-ioc2";
2
- import { cacheManager } from "@src/framework/utils/CacheManager";
3
- import { logger } from "@src/framework/utils/logger";
4
-
5
- /**
6
- * 缓存装饰器
7
- * 提供方法级别的缓存功能
8
- */
9
- export function Cacheable(options: {
10
- key?: string;
11
- ttl?: number;
12
- condition?: (args: any[]) => boolean;
13
- cacheManager?: any;
14
- }) {
15
- return function (target: any, propertyName: string, descriptor: PropertyDescriptor) {
16
- const method = descriptor.value;
17
- const cache = options.cacheManager || cacheManager.getCache();
18
-
19
- descriptor.value = async function (...args: any[]) {
20
- // 检查缓存条件
21
- if (options.condition && !options.condition(args)) {
22
- return await method.apply(this, args);
23
- }
24
-
25
- // 生成缓存键
26
- const cacheKey = options.key || `${target.constructor.name}:${propertyName}:${JSON.stringify(args)}`;
27
-
28
- try {
29
- // 尝试从缓存获取
30
- const cachedResult = await cache.get(cacheKey);
31
- if (cachedResult !== null) {
32
- logger.debug(`缓存命中: ${cacheKey}`);
33
- return cachedResult;
34
- }
35
-
36
- // 执行原方法
37
- const result = await method.apply(this, args);
38
-
39
- // 存储到缓存
40
- await cache.set(cacheKey, result, options.ttl);
41
- logger.debug(`缓存存储: ${cacheKey}`);
42
-
43
- return result;
44
- } catch (error) {
45
- logger.error(`缓存操作失败: ${cacheKey}`, error as Error);
46
- // 缓存失败时仍然执行原方法
47
- return await method.apply(this, args);
48
- }
49
- };
50
- };
51
- }
52
-
53
- /**
54
- * 缓存失效装饰器
55
- * 在方法执行后清除相关缓存
56
- */
57
- export function CacheEvict(options: {
58
- keys?: string[];
59
- keyPattern?: string;
60
- allEntries?: boolean;
61
- cacheManager?: any;
62
- }) {
63
- return function (target: any, propertyName: string, descriptor: PropertyDescriptor) {
64
- const method = descriptor.value;
65
- const cache = options.cacheManager || cacheManager.getCache();
66
-
67
- descriptor.value = async function (...args: any[]) {
68
- try {
69
- // 执行原方法
70
- const result = await method.apply(this, args);
71
-
72
- // 清除缓存
73
- if (options.allEntries) {
74
- await cache.clear();
75
- logger.debug(`清空所有缓存`);
76
- } else if (options.keyPattern) {
77
- // 这里需要根据具体的缓存实现来处理模式匹配
78
- logger.debug(`清除匹配模式的缓存: ${options.keyPattern}`);
79
- } else if (options.keys) {
80
- await cache.mdelete(options.keys);
81
- logger.debug(`清除指定缓存: ${options.keys.join(', ')}`);
82
- }
83
-
84
- return result;
85
- } catch (error) {
86
- logger.error(`缓存清除失败`, error as Error);
87
- // 清除缓存失败时仍然返回原方法结果
88
- return await method.apply(this, args);
89
- }
90
- };
91
- };
92
- }
93
-
94
- /**
95
- * 缓存更新装饰器
96
- * 在方法执行后更新相关缓存
97
- */
98
- export function CachePut(options: {
99
- key: string;
100
- ttl?: number;
101
- cacheManager?: any;
102
- }) {
103
- return function (target: any, propertyName: string, descriptor: PropertyDescriptor) {
104
- const method = descriptor.value;
105
- const cache = options.cacheManager || cacheManager.getCache();
106
-
107
- descriptor.value = async function (...args: any[]) {
108
- try {
109
- // 执行原方法
110
- const result = await method.apply(this, args);
111
-
112
- // 更新缓存
113
- await cache.set(options.key, result, options.ttl);
114
- logger.debug(`缓存更新: ${options.key}`);
115
-
116
- return result;
117
- } catch (error) {
118
- logger.error(`缓存更新失败: ${options.key}`, error as Error);
119
- // 更新缓存失败时仍然返回原方法结果
120
- return await method.apply(this, args);
121
- }
122
- };
123
- };
124
- }
125
-
126
- /**
127
- * 缓存服务
128
- * 提供业务级别的缓存管理
129
- */
130
- @Injectable()
131
- export class CacheService {
132
- private cache = cacheManager.getCache();
133
-
134
- /**
135
- * 缓存用户信息
136
- */
137
- async cacheUser(userId: number, userData: any, ttl: number = 3600): Promise<boolean> {
138
- const key = cacheManager.getUserCacheKey(userId);
139
- return await this.cache.set(key, userData, ttl);
140
- }
141
-
142
- /**
143
- * 获取缓存的用户信息
144
- */
145
- async getCachedUser(userId: number): Promise<any | null> {
146
- const key = cacheManager.getUserCacheKey(userId);
147
- return await this.cache.get(key);
148
- }
149
-
150
- /**
151
- * 清除用户缓存
152
- */
153
- async evictUserCache(userId: number): Promise<boolean> {
154
- const key = cacheManager.getUserCacheKey(userId);
155
- return await this.cache.delete(key);
156
- }
157
-
158
- /**
159
- * 缓存商品信息
160
- */
161
- async cacheGoods(goodsId: number, goodsData: any, ttl: number = 1800): Promise<boolean> {
162
- const key = cacheManager.getGoodsCacheKey(goodsId);
163
- return await this.cache.set(key, goodsData, ttl);
164
- }
165
-
166
- /**
167
- * 获取缓存的商品信息
168
- */
169
- async getCachedGoods(goodsId: number): Promise<any | null> {
170
- const key = cacheManager.getGoodsCacheKey(goodsId);
171
- return await this.cache.get(key);
172
- }
173
-
174
- /**
175
- * 清除商品缓存
176
- */
177
- async evictGoodsCache(goodsId: number): Promise<boolean> {
178
- const key = cacheManager.getGoodsCacheKey(goodsId);
179
- return await this.cache.delete(key);
180
- }
181
-
182
- /**
183
- * 缓存店铺信息
184
- */
185
- async cacheShop(shopId: number, shopData: any, ttl: number = 3600): Promise<boolean> {
186
- const key = cacheManager.getShopCacheKey(shopId);
187
- return await this.cache.set(key, shopData, ttl);
188
- }
189
-
190
- /**
191
- * 获取缓存的店铺信息
192
- */
193
- async getCachedShop(shopId: number): Promise<any | null> {
194
- const key = cacheManager.getShopCacheKey(shopId);
195
- return await this.cache.get(key);
196
- }
197
-
198
- /**
199
- * 清除店铺缓存
200
- */
201
- async evictShopCache(shopId: number): Promise<boolean> {
202
- const key = cacheManager.getShopCacheKey(shopId);
203
- return await this.cache.delete(key);
204
- }
205
-
206
- /**
207
- * 批量清除相关缓存
208
- */
209
- async evictRelatedCaches(keys: string[]): Promise<boolean> {
210
- return await this.cache.mdelete(keys);
211
- }
212
-
213
- /**
214
- * 预热缓存
215
- */
216
- async warmupCache(): Promise<void> {
217
- try {
218
- logger.info('开始缓存预热...');
219
-
220
- // 这里可以添加具体的预热逻辑
221
- // 例如:预加载热门商品、用户信息等
222
-
223
- logger.info('缓存预热完成');
224
- } catch (error) {
225
- logger.error('缓存预热失败:', error as Error);
226
- }
227
- }
228
-
229
- /**
230
- * 获取缓存统计信息
231
- */
232
- async getCacheStats(): Promise<any> {
233
- try {
234
- if (cacheManager.getCacheType() === 'memory') {
235
- const memoryCache = cacheManager.getCache() as any;
236
- return memoryCache.getStats();
237
- }
238
-
239
- return {
240
- type: cacheManager.getCacheType(),
241
- status: 'active'
242
- };
243
- } catch (error) {
244
- logger.error('获取缓存统计信息失败:', error as Error);
245
- return null;
246
- }
247
- }
248
- }