diff --git a/backend/internal/session/redis.go b/backend/internal/session/redis.go index 2210331..c497dd5 100644 --- a/backend/internal/session/redis.go +++ b/backend/internal/session/redis.go @@ -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() diff --git a/backend/internal/session/tiered.go b/backend/internal/session/tiered.go index 5fc008b..9f9d3b7 100644 --- a/backend/internal/session/tiered.go +++ b/backend/internal/session/tiered.go @@ -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) }