This commit is contained in:
2025-07-28 01:46:39 +08:00
parent b03129667a
commit 357639462a
219 changed files with 21634 additions and 8138 deletions

View File

@@ -26,25 +26,35 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
BloomFilter: false,
AutoInvalidate: true,
InvalidateDelay: 100 * time.Millisecond,
// 配置启用缓存的表
EnabledTables: []string{
"users",
"products",
"product_categories",
"product",
"product_category",
"enterprise_info_submit_records",
"sms_codes",
"wallets",
"subscription",
"product_category",
"product_documentation",
"enterprise_infos",
"api_users",
// 添加更多需要缓存的表
},
// 配置禁用缓存的表(日志表等)
DisabledTables: []string{
"sms_codes", // 短信验证码变化频繁
"audit_logs", // 审计日志
"system_logs", // 系统日志
"operation_logs", // 操作日志
"audit_logs", // 审计日志
"system_logs", // 系统日志
"operation_logs", // 操作日志
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
// 创建缓存插件
cachePlugin := cache.NewGormCachePlugin(cacheService, logger, cacheConfig)
@@ -54,7 +64,7 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
return err
}
logger.Info("GORM缓存插件已成功注册",
logger.Info("GORM缓存插件已成功注册",
zap.Duration("default_ttl", cacheConfig.DefaultTTL),
zap.Strings("enabled_tables", cacheConfig.EnabledTables),
zap.Strings("disabled_tables", cacheConfig.DisabledTables),
@@ -67,52 +77,67 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
func GetCacheConfig(cfg *config.Config) cache.CacheConfig {
// 生产环境配置
if cfg.Server.Mode == "release" {
return cache.CacheConfig{
DefaultTTL: 60 * time.Minute, // 生产环境延长缓存时间
cacheConfig := cache.CacheConfig{
DefaultTTL: 60 * time.Minute, // 生产环境延长缓存时间
TablePrefix: "prod_cache",
MaxCacheSize: 5000, // 生产环境增加缓存大小
CacheComplexSQL: false, // 生产环境不缓存复杂SQL
MaxCacheSize: 5000, // 生产环境增加缓存大小
CacheComplexSQL: false, // 生产环境不缓存复杂SQL
EnableStats: true,
EnableWarmup: true,
PenetrationGuard: true,
BloomFilter: true, // 生产环境启用布隆过滤器
BloomFilter: true, // 生产环境启用布隆过滤器
AutoInvalidate: true,
InvalidateDelay: 50 * time.Millisecond,
EnabledTables: []string{
"users", "products", "product_categories",
"enterprise_info_submit_records", "certifications",
"product_documentations",
"users",
"product",
"product_category",
"enterprise_info_submit_records",
"sms_codes",
},
DisabledTables: []string{
"sms_codes", "audit_logs", "system_logs",
"operation_logs", "sessions", "api_keys",
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
return cacheConfig
}
// 开发环境配置
return cache.CacheConfig{
DefaultTTL: 10 * time.Minute, // 开发环境缩短缓存时间,便于测试
cacheConfig := cache.CacheConfig{
DefaultTTL: 10 * time.Minute, // 开发环境缩短缓存时间,便于测试
TablePrefix: "dev_cache",
MaxCacheSize: 500,
CacheComplexSQL: true, // 开发环境允许缓存复杂SQL便于调试
CacheComplexSQL: true, // 开发环境允许缓存复杂SQL便于调试
EnableStats: true,
EnableWarmup: false, // 开发环境关闭预热
PenetrationGuard: false, // 开发环境关闭穿透保护
EnableWarmup: false, // 开发环境关闭预热
PenetrationGuard: false, // 开发环境关闭穿透保护
BloomFilter: false,
AutoInvalidate: true,
InvalidateDelay: 200 * time.Millisecond,
EnabledTables: []string{
"users", "products", "product_categories",
"users",
"product",
"product_category",
"enterprise_info_submit_records",
"sms_codes",
},
DisabledTables: []string{
"sms_codes", "audit_logs",
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
return cacheConfig
}
// CacheMetrics 缓存性能指标
@@ -136,7 +161,7 @@ func GetCacheMetrics(cacheService interfaces.CacheService) (*CacheMetrics, error
total := stats.Hits + stats.Misses
hitRate := float64(0)
missRate := float64(0)
if total > 0 {
hitRate = float64(stats.Hits) / float64(total) * 100
missRate = float64(stats.Misses) / float64(total) * 100
@@ -150,4 +175,4 @@ func GetCacheMetrics(cacheService interfaces.CacheService) (*CacheMetrics, error
CacheSize: stats.Memory,
CachedTables: int(stats.Keys),
}, nil
}
}