feat: 实现 Redis 会话存储,支持内存/Redis 双模式自动切换
- 新增 go-redis/v9 依赖 - 新增 config.yaml redis 配置段 + RedisConfig 结构体 + REDIS_PASSWORD 环境变量覆盖 - 新增 common/redis.go Redis 客户端初始化 - 完整实现 session/redis_store.go,实现 Store 接口全部 7 个方法 - Redis TTL 自动过期,无须后台清理 goroutine - deps_core.go 根据 cfg.Redis.Enabled 自动选择 RedisStore/MemoryStore - router.go + main.go 透传 redisClient 参数
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
"metazone.cc/metalab/internal/theme"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -69,6 +70,18 @@ func main() {
|
||||
r.Static("/uploads/avatars", "./storage/uploads/avatars")
|
||||
r.Static("/uploads/posts", "./storage/uploads/posts")
|
||||
|
||||
// Redis 客户端(可选)
|
||||
var redisClient *redis.Client
|
||||
if cfg.Redis.Enabled {
|
||||
redisClient, err = common.NewRedisClient(cfg.Redis)
|
||||
if err != nil {
|
||||
log.Fatalf("Redis 连接失败: %v", err)
|
||||
}
|
||||
log.Printf("Redis 已连接 %s:%s (DB=%d)", cfg.Redis.Host, cfg.Redis.Port, cfg.Redis.DB)
|
||||
} else {
|
||||
log.Println("Redis 未启用,使用内存存储")
|
||||
}
|
||||
|
||||
// 站点设置管理器(DB 持久化 + 内存缓存)
|
||||
siteSettings, err := config.NewSiteSettings(db)
|
||||
if err != nil {
|
||||
@ -76,7 +89,7 @@ func main() {
|
||||
}
|
||||
|
||||
// 路由注册
|
||||
router.Setup(r, db, cfg, siteSettings)
|
||||
router.Setup(r, db, cfg, siteSettings, redisClient)
|
||||
|
||||
// 启动
|
||||
log.Printf("MetaZone.FAN 启动于 0.0.0.0:%s", cfg.Server.Port)
|
||||
|
||||
Reference in New Issue
Block a user