create-dp-koa 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.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# 项目架构重构说明
|
|
2
|
+
|
|
3
|
+
## 📁 目录结构优化
|
|
4
|
+
|
|
5
|
+
### 框架级别 (`src/framework/`)
|
|
6
|
+
**职责**: 提供通用的框架功能,不依赖具体业务逻辑
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
src/framework/
|
|
11
|
+
├── decorator/ # 装饰器(控制器、参数等)
|
|
12
|
+
├── utils/ # 框架工具类
|
|
13
|
+
│ ├── bootstrap.ts # 应用启动框架
|
|
14
|
+
│ ├── cache.ts # 统一缓存管理
|
|
15
|
+
│ ├── db.ts # 数据库连接管理
|
|
16
|
+
│ ├── dbMonitor.ts # 数据库监控
|
|
17
|
+
│ ├── databaseConfig.ts # 数据库配置管理 ⭐ 新增
|
|
18
|
+
│ ├── function.ts # 通用函数
|
|
19
|
+
│ ├── logger.ts # 日志工具
|
|
20
|
+
│ ├── router.ts # 路由框架
|
|
21
|
+
│ └── token.ts # Token工具
|
|
22
|
+
└── ...
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 业务级别 (`src/`)
|
|
26
|
+
**职责**: 包含具体的业务逻辑和实体依赖
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
src/
|
|
30
|
+
├── controllers/ # 控制器(业务逻辑)
|
|
31
|
+
├── entity/ # 数据实体
|
|
32
|
+
├── service/ # 业务服务
|
|
33
|
+
├── dto/ # 数据传输对象
|
|
34
|
+
├── libs/ # 业务库
|
|
35
|
+
├── middlewares/ # 中间件
|
|
36
|
+
├── routers/ # 路由配置
|
|
37
|
+
└── utils/ # 业务工具类 ⭐ 新增
|
|
38
|
+
└── testDataInitializer.ts # 测试数据初始化
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 🔧 重构内容
|
|
42
|
+
|
|
43
|
+
### 1. **数据库配置管理** (`src/framework/utils/databaseConfig.ts`)
|
|
44
|
+
- ✅ **框架级别**: 纯配置管理,无业务依赖
|
|
45
|
+
- ✅ **职责单一**: 只负责数据库模式切换和配置生成
|
|
46
|
+
- ✅ **环境检测**: 自动检测内存数据库/MySQL模式
|
|
47
|
+
- ✅ **配置生成**: 根据模式生成对应的DataSourceOptions
|
|
48
|
+
|
|
49
|
+
### 2. **测试数据初始化** (`src/utils/testDataInitializer.ts`)
|
|
50
|
+
- ✅ **业务级别**: 包含具体实体依赖
|
|
51
|
+
- ✅ **职责明确**: 专门处理测试数据创建和管理
|
|
52
|
+
- ✅ **模式检测**: 只在内存数据库模式下工作
|
|
53
|
+
- ✅ **数据管理**: 提供完整的CRUD操作
|
|
54
|
+
|
|
55
|
+
### 3. **应用启动优化** (`src/app.ts`)
|
|
56
|
+
- ✅ **配置解耦**: 使用DatabaseConfigManager管理配置
|
|
57
|
+
- ✅ **逻辑清晰**: 分离配置管理和业务逻辑
|
|
58
|
+
- ✅ **监控优化**: 根据数据库类型决定是否启用监控
|
|
59
|
+
|
|
60
|
+
## 🎯 架构优势
|
|
61
|
+
|
|
62
|
+
### 1. **职责分离**
|
|
63
|
+
- **框架层**: 提供通用功能,无业务依赖
|
|
64
|
+
- **业务层**: 处理具体业务逻辑,可依赖实体
|
|
65
|
+
|
|
66
|
+
### 2. **可维护性**
|
|
67
|
+
- **配置集中**: 数据库配置统一管理
|
|
68
|
+
- **逻辑清晰**: 各模块职责明确
|
|
69
|
+
- **易于扩展**: 新增数据库类型只需修改配置管理器
|
|
70
|
+
|
|
71
|
+
### 3. **可测试性**
|
|
72
|
+
- **框架独立**: 框架功能可独立测试
|
|
73
|
+
- **业务隔离**: 业务逻辑与框架解耦
|
|
74
|
+
- **数据管理**: 测试数据工具独立管理
|
|
75
|
+
|
|
76
|
+
## 📋 使用方式
|
|
77
|
+
|
|
78
|
+
### 环境配置
|
|
79
|
+
```bash
|
|
80
|
+
# .env.development
|
|
81
|
+
db_enable=1
|
|
82
|
+
db_memory=1 # 启用内存数据库模式
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 数据库模式切换
|
|
86
|
+
```typescript
|
|
87
|
+
// 框架自动检测环境变量
|
|
88
|
+
// db_memory=1 -> 内存数据库模式
|
|
89
|
+
// db_memory=0 -> MySQL数据库模式
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 测试数据管理
|
|
93
|
+
```bash
|
|
94
|
+
# 查看测试数据状态
|
|
95
|
+
GET /test/test-data/status
|
|
96
|
+
|
|
97
|
+
# 重新初始化测试数据
|
|
98
|
+
POST /test/test-data/reset
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 🔄 迁移完成
|
|
102
|
+
|
|
103
|
+
- ✅ **删除**: `src/framework/utils/testDataInitializer.ts`
|
|
104
|
+
- ✅ **新增**: `src/utils/testDataInitializer.ts`
|
|
105
|
+
- ✅ **新增**: `src/framework/utils/databaseConfig.ts`
|
|
106
|
+
- ✅ **更新**: `src/app.ts` - 使用新的配置管理器
|
|
107
|
+
- ✅ **更新**: `src/controllers/testData.controller.ts` - 更新导入路径
|
|
108
|
+
|
|
109
|
+
现在项目架构更加合理,框架层和业务层职责分离明确!
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# 统一缓存管理系统使用说明
|
|
2
|
+
|
|
3
|
+
## 📋 概述
|
|
4
|
+
|
|
5
|
+
本项目已全面升级为统一的缓存管理系统,所有缓存都使用相同的配置参数和监控机制,有效防止访问量暴增导致的内存过大问题。
|
|
6
|
+
|
|
7
|
+
## 🔧 已更新的文件
|
|
8
|
+
|
|
9
|
+
### 1. 核心缓存管理
|
|
10
|
+
- ✅ `src/framework/utils/cache.ts` - 统一缓存管理器
|
|
11
|
+
- ✅ `src/framework/utils/router.ts` - 控制器结果缓存
|
|
12
|
+
- ✅ `src/libs/mCache.ts` - 用户缓存
|
|
13
|
+
|
|
14
|
+
### 2. 控制器更新
|
|
15
|
+
- ✅ `src/controllers/home/ytUser.controller.ts` - 用户信息缓存
|
|
16
|
+
- ✅ `src/controllers/home/ytShop.controller.ts` - 店铺用户缓存
|
|
17
|
+
- ✅ `src/controllers/captcha.controller.ts` - 验证码缓存(新增防重复验证)
|
|
18
|
+
|
|
19
|
+
### 3. 监控和管理
|
|
20
|
+
- ✅ `src/controllers/health.controller.ts` - 健康检查(包含缓存监控)
|
|
21
|
+
- ✅ `src/controllers/cacheManagement.controller.ts` - 缓存管理工具
|
|
22
|
+
- ✅ `src/app.ts` - 优雅关闭(包含缓存清理)
|
|
23
|
+
|
|
24
|
+
## 🚀 使用方法
|
|
25
|
+
|
|
26
|
+
### 创建缓存
|
|
27
|
+
```typescript
|
|
28
|
+
import { createCache, CacheType } from '@src/framework/utils/cache';
|
|
29
|
+
|
|
30
|
+
// 使用预定义类型
|
|
31
|
+
const userCache = createCache('user-cache', CacheType.USER);
|
|
32
|
+
|
|
33
|
+
// 自定义配置
|
|
34
|
+
const customCache = createCache('custom', CacheType.TEMP, {
|
|
35
|
+
stdTTL: 60,
|
|
36
|
+
maxKeys: 500
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 缓存操作
|
|
41
|
+
```typescript
|
|
42
|
+
// 设置缓存
|
|
43
|
+
cache.set(key, value, ttl); // ttl 可选,使用默认值
|
|
44
|
+
|
|
45
|
+
// 获取缓存
|
|
46
|
+
const value = cache.get(key);
|
|
47
|
+
|
|
48
|
+
// 删除缓存
|
|
49
|
+
cache.del(key);
|
|
50
|
+
|
|
51
|
+
// 清空所有缓存
|
|
52
|
+
cache.flushAll();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 📊 监控端点
|
|
56
|
+
|
|
57
|
+
### 健康检查
|
|
58
|
+
- `GET /health` - 完整系统状态(包含缓存信息)
|
|
59
|
+
- `GET /health/cache` - 专门的缓存健康检查
|
|
60
|
+
|
|
61
|
+
### 缓存管理
|
|
62
|
+
- `GET /admin/cache/stats` - 获取所有缓存统计信息
|
|
63
|
+
- `POST /admin/cache/clear` - 清理指定缓存
|
|
64
|
+
- `POST /admin/cache/delete` - 删除缓存实例
|
|
65
|
+
- `POST /admin/cache/clear-all` - 清理所有缓存
|
|
66
|
+
- `GET /admin/cache/info` - 获取特定缓存详细信息
|
|
67
|
+
|
|
68
|
+
## 🛡️ 安全特性
|
|
69
|
+
|
|
70
|
+
### 1. 内存保护
|
|
71
|
+
- 每个缓存都有最大条目数限制
|
|
72
|
+
- 自动过期清理机制
|
|
73
|
+
- 实时使用率监控
|
|
74
|
+
|
|
75
|
+
### 2. 防重复验证
|
|
76
|
+
验证码控制器新增防重复验证功能:
|
|
77
|
+
```typescript
|
|
78
|
+
// 验证成功后标记为已使用
|
|
79
|
+
captchaCache.set(verifyKey, true, 300); // 5分钟内不能重复验证
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 3. 优雅关闭
|
|
83
|
+
应用关闭时自动清理所有缓存,防止内存泄露。
|
|
84
|
+
|
|
85
|
+
## 📈 缓存类型配置
|
|
86
|
+
|
|
87
|
+
| 类型 | 默认TTL | 最大条目 | 用途 |
|
|
88
|
+
|------|---------|----------|------|
|
|
89
|
+
| USER | 30分钟 | 500 | 用户信息缓存 |
|
|
90
|
+
| CONTROLLER | 1分钟 | 2000 | 控制器结果缓存 |
|
|
91
|
+
| SESSION | 1小时 | 1000 | 会话缓存 |
|
|
92
|
+
| CAPTCHA | 5分钟 | 100 | 验证码缓存 |
|
|
93
|
+
| TEMP | 1分钟 | 100 | 临时缓存 |
|
|
94
|
+
|
|
95
|
+
## 🔍 监控信息示例
|
|
96
|
+
|
|
97
|
+
### 缓存统计
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"yt-user-controller": {
|
|
101
|
+
"hits": 150,
|
|
102
|
+
"misses": 25,
|
|
103
|
+
"keys": 45,
|
|
104
|
+
"hitRate": "85.71%"
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 内存使用
|
|
110
|
+
```json
|
|
111
|
+
{
|
|
112
|
+
"yt-user-controller": {
|
|
113
|
+
"keyCount": 45,
|
|
114
|
+
"maxKeys": 500,
|
|
115
|
+
"usage": "9.00%"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## ⚠️ 注意事项
|
|
121
|
+
|
|
122
|
+
1. **缓存键命名**: 使用有意义的键名,避免冲突
|
|
123
|
+
2. **过期时间**: 根据业务需求设置合适的TTL
|
|
124
|
+
3. **内存监控**: 定期检查缓存使用率,避免超过80%
|
|
125
|
+
4. **错误处理**: 缓存操作失败时要有降级方案
|
|
126
|
+
|
|
127
|
+
## 🎯 最佳实践
|
|
128
|
+
|
|
129
|
+
1. **分层缓存**: 不同类型数据使用不同缓存类型
|
|
130
|
+
2. **缓存预热**: 系统启动时预加载热点数据
|
|
131
|
+
3. **监控告警**: 设置缓存使用率告警
|
|
132
|
+
4. **定期清理**: 定期清理过期和无用缓存
|
|
133
|
+
|
|
134
|
+
## 🔄 迁移完成
|
|
135
|
+
|
|
136
|
+
所有原有的缓存使用已成功迁移到新的统一管理系统:
|
|
137
|
+
- ✅ 用户信息缓存
|
|
138
|
+
- ✅ 店铺用户缓存
|
|
139
|
+
- ✅ 控制器结果缓存
|
|
140
|
+
- ✅ 验证码缓存(新增功能)
|
|
141
|
+
|
|
142
|
+
系统现在具备了完整的缓存管理能力,可以有效防止内存泄露问题!
|