From b8fd6a933078028189dbe8699366c33dbc2ce6f0 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sat, 20 Jun 2026 11:54:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20TieredManager=20?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=20session=20=E6=97=B6=20L1/L2=20ID=20?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E5=AF=BC=E8=87=B4=20Redis=20?= =?UTF-8?q?=E5=86=99=E5=85=A5=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/session/redis.go | 7 ++++++- backend/internal/session/tiered.go | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) 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) } -- 2.49.1