更新环境配置,优化数据库和Redis连接设置
- 修改`.env.example`文件,更新数据库和Redis的默认主机地址为`192.168.9.127` - 在`docker-compose.yml`中移除不必要的卷挂载配置 - 在`go.mod`中添加`github.com/joho/godotenv`依赖以支持环境变量加载 - 更新`README.md`以反映新的数据库和Redis连接信息 - 在`main.go`中加载环境变量以支持动态配置 - 新增缓存存在性检查功能,避免重复预热缓存 - 优化CSV读取逻辑,支持从S3获取数据
This commit is contained in:
+17
-1
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/luxsin/app-api/internal/cache"
|
||||
"github.com/luxsin/app-api/internal/config"
|
||||
"github.com/luxsin/app-api/internal/database"
|
||||
@@ -26,6 +27,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
_ = godotenv.Load()
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -121,13 +124,26 @@ func main() {
|
||||
log.Info("server stopped")
|
||||
}
|
||||
|
||||
// warmUpCache 启动时从数据库加载数据到 Redis
|
||||
// warmUpCache 启动时从数据库加载数据到 Redis(缓存已存在则跳过,避免多实例重复预热)
|
||||
func warmUpCache(log *zap.Logger, db *sql.DB, rdb *redis.Client) {
|
||||
ctx := context.Background()
|
||||
|
||||
brandCache := cache.NewBrandCache(rdb)
|
||||
modelCache := cache.NewModelCache(rdb)
|
||||
|
||||
brandExists, err := brandCache.Exists(ctx)
|
||||
if err != nil {
|
||||
log.Warn("check brand cache failed, will warm up", zap.Error(err))
|
||||
}
|
||||
modelExists, err := modelCache.Exists(ctx)
|
||||
if err != nil {
|
||||
log.Warn("check model cache failed, will warm up", zap.Error(err))
|
||||
}
|
||||
if brandExists && modelExists {
|
||||
log.Info("cache already warmed, skip warm-up")
|
||||
return
|
||||
}
|
||||
|
||||
brandRepo := repository.NewBrandRepository(db, brandCache)
|
||||
modelRepo := repository.NewModelRepository(db, modelCache)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user