You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

175 lines
5.6 KiB

import BaseController from "@/base/BaseController.js"
import Router from "utils/router.js"
import routeCache from "utils/cache/RouteCache.js"
/**
* 路由缓存管理控制器
* 提供缓存监控、清理等管理功能
*/
class RouteCacheController extends BaseController {
constructor() {
super()
}
/**
* 获取缓存统计信息
*/
async getStats(ctx) {
const stats = routeCache.getStats()
return this.success(ctx, stats, "获取缓存统计信息成功")
}
/**
* 清除所有缓存
*/
async clearAll(ctx) {
routeCache.clearAll()
return this.success(ctx, null, "所有缓存已清除")
}
/**
* 清除路由匹配缓存
*/
async clearRouteMatches(ctx) {
routeCache.clearRouteMatches()
return this.success(ctx, null, "路由匹配缓存已清除")
}
/**
* 清除控制器实例缓存
*/
async clearControllers(ctx) {
routeCache.clearControllers()
return this.success(ctx, null, "控制器实例缓存已清除")
}
/**
* 清除中间件组合缓存
*/
async clearMiddlewares(ctx) {
routeCache.clearMiddlewares()
return this.success(ctx, null, "中间件组合缓存已清除")
}
/**
* 清除路由注册缓存
*/
async clearRegistrations(ctx) {
routeCache.clearRegistrations()
return this.success(ctx, null, "路由注册缓存已清除")
}
/**
* 根据文件路径清除相关缓存
*/
async clearByFile(ctx) {
const data = this.validateParams(ctx, {
filePath: { required: true, label: '文件路径' }
})
routeCache.clearByFile(data.filePath)
return this.success(ctx, null, `文件 ${data.filePath} 相关缓存已清除`)
}
/**
* 更新缓存配置
*/
async updateConfig(ctx) {
const data = this.validateParams(ctx, {
enabled: { type: 'boolean', label: '启用状态' },
maxMatchCacheSize: { type: 'number', label: '路由匹配缓存最大大小' },
maxControllerCacheSize: { type: 'number', label: '控制器缓存最大大小' },
maxMiddlewareCacheSize: { type: 'number', label: '中间件缓存最大大小' },
maxRegistrationCacheSize: { type: 'number', label: '注册缓存最大大小' }
})
// 过滤掉undefined值
const config = Object.fromEntries(
Object.entries(data).filter(([_, value]) => value !== undefined)
)
routeCache.updateConfig(config)
return this.success(ctx, routeCache.getStats(), "缓存配置已更新")
}
/**
* 启用缓存
*/
async enable(ctx) {
routeCache.enable()
return this.success(ctx, null, "路由缓存已启用")
}
/**
* 禁用缓存
*/
async disable(ctx) {
routeCache.disable()
return this.success(ctx, null, "路由缓存已禁用")
}
/**
* 获取缓存健康状态
*/
async getHealth(ctx) {
const stats = routeCache.getStats()
// 简单的健康检查逻辑
const health = {
status: 'healthy',
issues: [],
recommendations: []
}
// 检查命中率
const overallHitRate = parseFloat(stats.hitRate)
if (overallHitRate < 50) {
health.status = 'warning'
health.issues.push('总体缓存命中率较低')
health.recommendations.push('考虑调整缓存策略或检查路由模式')
}
// 检查缓存大小
Object.entries(stats.caches).forEach(([cacheType, cacheStats]) => {
if (cacheStats.size > 500) {
health.issues.push(`${cacheType} 缓存大小过大 (${cacheStats.size})`)
health.recommendations.push(`考虑清理 ${cacheType} 缓存或调整最大大小`)
}
})
if (health.issues.length > 0 && health.status === 'healthy') {
health.status = 'warning'
}
return this.success(ctx, { ...stats, health }, "获取缓存健康状态成功")
}
/**
* 创建路由
*/
static createRoutes() {
const controller = new RouteCacheController()
const router = new Router({ prefix: '/api/system/route-cache' })
// 缓存统计
router.get('/stats', controller.handleRequest(controller.getStats), { auth: true })
router.get('/health', controller.handleRequest(controller.getHealth), { auth: true })
// 缓存清理
router.delete('/clear/all', controller.handleRequest(controller.clearAll), { auth: true })
router.delete('/clear/routes', controller.handleRequest(controller.clearRouteMatches), { auth: true })
router.delete('/clear/controllers', controller.handleRequest(controller.clearControllers), { auth: true })
router.delete('/clear/middlewares', controller.handleRequest(controller.clearMiddlewares), { auth: true })
router.delete('/clear/registrations', controller.handleRequest(controller.clearRegistrations), { auth: true })
router.delete('/clear/file', controller.handleRequest(controller.clearByFile), { auth: true })
// 缓存配置
router.put('/config', controller.handleRequest(controller.updateConfig), { auth: true })
router.post('/enable', controller.handleRequest(controller.enable), { auth: true })
router.post('/disable', controller.handleRequest(controller.disable), { auth: true })
return router
}
}
export default RouteCacheController