Skip to content

Commit 3bd17fb

Browse files
author
zicorn
committed
[update] fix conflict
1 parent 8953de4 commit 3bd17fb

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

internal/utils/cache/redis.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,22 @@ func Expire(key string, time time.Duration, context ...redis.Cmdable) (bool, err
497497
return getCmdable(context...).Expire(ctx, serialKey(key), time).Result()
498498
}
499499

500-
func Transaction(fn func(redis.Pipeliner) error) error {
500+
func Transaction(fn func(redis.Pipeliner) error, keys ...string) error {
501501
if client == nil {
502502
return ErrDBNotInit
503503
}
504504

505+
// 如果没有提供 keys,则返回错误
506+
if len(keys) == 0 {
507+
return errors.New("redis: Watch requires at least one key")
508+
}
509+
510+
// 将 keys 进行序列化处理
511+
watchKeys := make([]string, len(keys))
512+
for i, key := range keys {
513+
watchKeys[i] = serialKey(key)
514+
}
515+
505516
return client.Watch(ctx, func(tx *redis.Tx) error {
506517
_, err := tx.TxPipelined(ctx, func(p redis.Pipeliner) error {
507518
return fn(p)
@@ -510,7 +521,7 @@ func Transaction(fn func(redis.Pipeliner) error) error {
510521
return nil
511522
}
512523
return err
513-
})
524+
}, watchKeys...)
514525
}
515526

516527
func Publish(channel string, message any, context ...redis.Cmdable) error {

internal/utils/cache/redis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestRedisTransaction(t *testing.T) {
5454
}
5555

5656
return errors.New("test transaction error")
57-
})
57+
}, strings.Join([]string{TEST_PREFIX, "key"}, ":"))
5858

5959
if err == nil {
6060
t.Errorf("transaction should return error")
@@ -90,7 +90,7 @@ func TestRedisTransaction(t *testing.T) {
9090
}
9191

9292
return nil
93-
})
93+
}, strings.Join([]string{TEST_PREFIX, "key"}, ":"))
9494

9595
if err != nil {
9696
t.Errorf("transaction should not return error")

0 commit comments

Comments
 (0)