ソースを参照

chore: redislock use stringx.randn replace randomStr func (#1220)

an 3 年 前
コミット
3a510a9138
2 ファイル変更5 行追加11 行削除
  1. 3 2
      core/stores/redis/redis_test.go
  2. 2 9
      core/stores/redis/redislock.go

+ 3 - 2
core/stores/redis/redis_test.go

@@ -3,6 +3,7 @@ package redis
 import (
 	"crypto/tls"
 	"errors"
+	"github.com/tal-tech/go-zero/core/stringx"
 	"io"
 	"strconv"
 	"testing"
@@ -186,7 +187,7 @@ func TestRedis_Hscan(t *testing.T) {
 		key := "hash:test"
 		fieldsAndValues := make(map[string]string)
 		for i := 0; i < 1550; i++ {
-			fieldsAndValues["filed_"+strconv.Itoa(i)] = randomStr(i)
+			fieldsAndValues["filed_"+strconv.Itoa(i)] = stringx.Randn(i)
 		}
 		err := client.Hmset(key, fieldsAndValues)
 		assert.Nil(t, err)
@@ -550,7 +551,7 @@ func TestRedis_Sscan(t *testing.T) {
 		key := "list"
 		var list []string
 		for i := 0; i < 1550; i++ {
-			list = append(list, randomStr(i))
+			list = append(list, stringx.Randn(i))
 		}
 		lens, err := client.Sadd(key, list)
 		assert.Nil(t, err)

+ 2 - 9
core/stores/redis/redislock.go

@@ -1,6 +1,7 @@
 package redis
 
 import (
+	"github.com/tal-tech/go-zero/core/stringx"
 	"math/rand"
 	"strconv"
 	"sync/atomic"
@@ -45,7 +46,7 @@ func NewRedisLock(store *Redis, key string) *RedisLock {
 	return &RedisLock{
 		store: store,
 		key:   key,
-		id:    randomStr(randomLen),
+		id:    stringx.Randn(randomLen),
 	}
 }
 
@@ -92,11 +93,3 @@ func (rl *RedisLock) Release() (bool, error) {
 func (rl *RedisLock) SetExpire(seconds int) {
 	atomic.StoreUint32(&rl.seconds, uint32(seconds))
 }
-
-func randomStr(n int) string {
-	b := make([]byte, n)
-	for i := range b {
-		b[i] = letters[rand.Intn(len(letters))]
-	}
-	return string(b)
-}