/
redis-redis.txt
111 lines (91 loc) · 2.2 KB
/
redis-redis.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package resource
import (
"fmt"
"git.proway.tech/wallstreet/exchange/internal/errors"
"git.proway.tech/wallstreet/exchange/internal/glog"
"github.com/go-redis/redis"
"time"
)
func NewRedis(opt *redis.Options) (*redis.Client, func(), error) {
c := redis.NewClient(opt)
err := c.Ping().Err()
if err != nil {
return nil, nil, err
}
return c, func() {
glog.Info("[main] clean up redis")
err := c.Close()
if err != nil {
errors.LogError(err)
}
}, nil
}
type RedisLock struct {
resource string
token string
conn *redis.Client
timeout int
Auth string
Addr string
DB int
}
func (lock *RedisLock) tryLock() (ok bool, err error) {
success, err := lock.conn.SetNX(lock.key(), lock.token, 10*time.Second).Result()
//为flase说明已经存在
if success == false {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (lock *RedisLock) Unlock() (err error) {
_, err = lock.conn.Do("del", lock.key()).Int64()
if err != nil {
glog.Error("RedisLock:Unlock错误")
}
return
}
func (lock *RedisLock) key() string {
return fmt.Sprintf("redislock:%s", lock.resource)
}
func TryLock(conn *redis.Client, resource string, token string, DefaulTimeout int) (lock *RedisLock, ok bool, err error) {
return TryLockWithTimeout(conn, resource, token, DefaulTimeout)
}
func TryLockWithTimeout(conn *redis.Client, resource string, token string, timeout int) (lock *RedisLock, ok bool, err error) {
lock = &RedisLock{resource: resource, token: token, conn: conn, timeout: timeout}
ok, err = lock.tryLock()
if !ok || err != nil {
lock = nil
}
return
}
func NewRedisPool(lock *RedisLock) *RedisLock {
opt := &redis.Options{
Addr: lock.Addr,
Password: lock.Auth,
DB: lock.DB,
}
c := redis.NewClient(opt)
err := c.Ping().Err()
if err != nil {
return nil
}
lock.conn = c
return lock
}
func (lock *RedisLock) NewReidsLock(f func(), lockString string) error {
lock, ok, err := TryLock(lock.conn, lockString, "token", int(10))
if err != nil {
return errors.New("redis:" + err.Error())
}
//已经有访问的了退出接口
if !ok {
return errors.New("当前人数过多,请稍后操作")
}
//如果要测试的话 自己加定时器
f()
defer lock.Unlock()
return nil
}