fix: 修复 TieredManager 创建 session 时 L1/L2 ID 不一致导致 Redis 写入失败的问题 #131

Merged
huanghaosheng merged 1 commits from fix/redis-use into develop 2026-06-20 11:55:44 +08:00
2 changed files with 8 additions and 3 deletions

View File

@@ -47,7 +47,12 @@ func userSessKey(id string) string { return fmt.Sprintf("user:%s:sessions", id)
// Create 创建新会话。userID 为空表示匿名会话。
func (m *RedisManager) Create(ctx context.Context, userID string, config models.SessionConfig) (string, error) {
id := uuidNew()
return m.CreateWithID(ctx, uuidNew(), userID, config)
}
// CreateWithID 使用指定 ID 创建新会话。
// 供 TieredManager 调用,确保 L1/L2 使用相同的 session ID。
func (m *RedisManager) CreateWithID(ctx context.Context, id string, userID string, config models.SessionConfig) (string, error) {
now := time.Now().UTC()
pipe := m.rdb.Pipeline()

View File

@@ -127,9 +127,9 @@ func (m *TieredManager) Create(ctx context.Context, userID string, config models
return "", err
}
// L2: Redis同步
// L2: Redis同步,使用 L1 生成的 ID 保证一致性
if m.isRedisOK() {
if _, err := m.l2.Create(ctx, userID, config); err != nil {
if _, err := m.l2.CreateWithID(ctx, id, userID, config); err != nil {
logger.Log.Warnw("Redis Create failed, continuing without L2",
"session", id, "error", err)
}