Skip to content

Commit

Permalink
增加setglobalredisclient,globalredisclient参数修改
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyu mao committed Jun 18, 2023
1 parent f753b48 commit 7d14e64
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@


### download

```shell
go get github.com/cr-mao/goredislock@v1.0.0
```

### test

```shell
go test -v ./...
````

### demo

Expand Down
12 changes: 8 additions & 4 deletions redis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
var GlobalRedisClient *redis.Client
var redisClientOnce sync.Once

func NewRedisClient(addr string) *redis.Client {
func NewRedisClient(addr string, db int, username, password string) *redis.Client {
redisClientOnce.Do(func() {
GlobalRedisClient = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: addr,
Password: "", //密码
DB: 0, // redis数据库

Password: password, //密码
DB: db, // redis数据库
Username: username,
//连接池容量及闲置连接数量
PoolSize: 15, // 连接池数量
MinIdleConns: 10, //好比最小连接数
Expand Down Expand Up @@ -49,3 +49,7 @@ func NewRedisClient(addr string) *redis.Client {
})
return GlobalRedisClient
}

func SetGlobalRedisClient(redisClient *redis.Client) {
GlobalRedisClient = redisClient
}
38 changes: 35 additions & 3 deletions redis_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// 只有一个go程能获得 锁
func TestRedisLock(t *testing.T) {
//实例化全局redis
NewRedisClient("127.0.0.1:6379")
NewRedisClient("127.0.0.1:6379", 0, "", "")

for i := 0; i < 10; i++ {
go func(num int) {
Expand All @@ -27,7 +27,7 @@ func TestRedisLock(t *testing.T) {

func TestRedisLockWithContext(t *testing.T) {
//实例化全局redis
NewRedisClient("127.0.0.1:6379")
NewRedisClient("127.0.0.1:6379", 0, "", "")
locker, ok := NewLocker("test_lock_key", WithContext(context.Background())).Lock()
defer locker.Unlock()
fmt.Println(ok)
Expand All @@ -49,7 +49,7 @@ true
*/
func TestRedisLockWithExpire(t *testing.T) {
//实例化全局redis
NewRedisClient("127.0.0.1:6379")
NewRedisClient("127.0.0.1:6379", 0, "", "")
locker, ok := NewLocker("test_lock_key", WithExpire(time.Second*2)).Lock()
fmt.Println(ok)
time.Sleep(time.Second * 10)
Expand Down Expand Up @@ -89,3 +89,35 @@ func TestRedisLockWithRedisClient(t *testing.T) {
fmt.Println(ok)
defer locker.Unlock()
}

func TestSetGlobalRedisClient(t *testing.T) {
var redisClient = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "127.0.0.1:6379",
Password: "", //密码
DB: 0, // redis数据库

//连接池容量及闲置连接数量
PoolSize: 15, // 连接池数量
MinIdleConns: 10, //好比最小连接数
//超时
DialTimeout: 5 * time.Second, //连接建立超时时间
ReadTimeout: 3 * time.Second, //读超时,默认3秒, -1表示取消读超时
WriteTimeout: 3 * time.Second, //写超时,默认等于读超时
PoolTimeout: 4 * time.Second, //当所有连接都处在繁忙状态时,客户端等待可用连接的最大等待时长,默认为读超时+1秒。

//闲置连接检查包括IdleTimeout,MaxConnAge
IdleCheckFrequency: 60 * time.Second, //闲置连接检查的周期,默认为1分钟,-1表示不做周期性检查,只在客户端获取连接时对闲置连接进行处理。
IdleTimeout: 5 * time.Minute, //闲置超时
MaxConnAge: 0 * time.Second, //连接存活时长,从创建开始计时,超过指定时长则关闭连接,默认为0,即不关闭存活时长较长的连接

//命令执行失败时的重试策略
MaxRetries: 0, // 命令执行失败时,最多重试多少次,默认为0即不重试
MinRetryBackoff: 8 * time.Millisecond, //每次计算重试间隔时间的下限,默认8毫秒,-1表示取消间隔
MaxRetryBackoff: 512 * time.Millisecond, //每次计算重试间隔时间的上限,默认512毫秒,-1表示取消间隔
})
SetGlobalRedisClient(redisClient)
locker, ok := NewLocker("test_lock_key", WithExpire(time.Second*2)).Lock()
defer locker.Unlock()
fmt.Println(ok)
}

0 comments on commit 7d14e64

Please sign in to comment.