优化缓冲区策略初始化;退出时保存内存缓存到磁盘

This commit is contained in:
www.xueximeng.com
2025-08-07 13:14:35 +08:00
parent 6f0f0396d0
commit ad318390ba
10 changed files with 330 additions and 59 deletions

28
main.go
View File

@@ -74,20 +74,19 @@ func initApp() {
if err := globalCacheWriteManager.Initialize(); err != nil {
log.Fatalf("缓存写入管理器初始化失败: %v", err)
}
fmt.Println("✅ 缓存写入管理器已初始化")
// 🔗 将缓存写入管理器注入到service包
// 将缓存写入管理器注入到service包
service.SetGlobalCacheWriteManager(globalCacheWriteManager)
// 🔗 设置缓存写入管理器的主缓存更新函数
if mainCache := service.GetEnhancedTwoLevelCache(); mainCache != nil {
globalCacheWriteManager.SetMainCacheUpdater(func(key string, data []byte, ttl time.Duration) error {
return mainCache.SetBothLevels(key, data, ttl)
})
fmt.Println("✅ 主缓存更新函数已设置")
} else {
fmt.Println("⚠️ 主缓存实例不可用,稍后将重试设置")
}
// 延迟设置主缓存更新函数确保service初始化完成
go func() {
// 等待一小段时间确保service包完全初始化
time.Sleep(100 * time.Millisecond)
if mainCache := service.GetEnhancedTwoLevelCache(); mainCache != nil {
globalCacheWriteManager.SetMainCacheUpdater(func(key string, data []byte, ttl time.Duration) error {
return mainCache.SetBothLevels(key, data, ttl)
})
}
}()
// 确保异步插件系统初始化
plugin.InitAsyncPluginSystem()
@@ -169,6 +168,11 @@ func startServer() {
}
}
// 强制同步内存缓存到磁盘
if mainCache := service.GetEnhancedTwoLevelCache(); mainCache != nil {
mainCache.FlushMemoryToDisk()
}
// 设置关闭超时时间
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()