-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis_cli.go
More file actions
157 lines (136 loc) · 4.02 KB
/
redis_cli.go
File metadata and controls
157 lines (136 loc) · 4.02 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package AsyncCache
import (
"github.com/garyburd/redigo/redis"
"log"
)
type rediscli struct {
pool *redis.Pool
}
// 生成连接池方法
func newPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 80,
MaxActive: 12000, // max number of connections
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "192.168.0.1:6379")
if err != nil {
panic(err.Error())
}
return c, err
},
}
}
//SET String to redis
func (this *rediscli) SetString(key string, v string) error {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if ok, err := redis.Bool(c.Do("SET", key, v)); ok {
return nil
} else {
log.Print(err)
return err
}
}
//SET String to redis with expire time.Set the specified expire time, in milliseconds.
func (this *rediscli) SetStringWithExpriePX(key string, v string, exprie int32) error {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if ok, err := redis.Bool(c.Do("SET", key, v, "PX", exprie)); ok {
return nil
} else {
log.Print(err)
return err
}
}
//SETNX Int32 to redis
func (this *rediscli) SetNXInt(key string, v int32) (bool, error) {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if ok, err := redis.Bool(c.Do("SETNX", key, v)); ok {
return ok, err
} else {
log.Print(err)
return false, err
}
}
//SET []byte to redis
func (this *rediscli) SetBytesSlice(key string, v []byte) error {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if ok, err := redis.Bool(c.Do("SET", key, v)); ok {
return nil
} else {
log.Print(err)
return err
}
}
//SET []byte to redis with expire time.Set the specified expire time, in milliseconds.
func (this *rediscli) SetBytesSliceWithExpriePX(key string, v []byte, exprie int32) error {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if ok, err := redis.Bool(c.Do("SET", key, v, "PX", exprie)); ok {
return nil
} else {
log.Print(err)
return err
}
}
//SET interface{} to redis
func (this *rediscli) SetInterface(key string, v interface{}) error {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if ok, err := redis.Bool(c.Do("SET", key, v)); ok {
return nil
} else {
log.Print(err)
return err
}
}
//GET String from redis
func (this *rediscli) GetString(key string) (v string, err error) {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if v, err = redis.String(c.Do("GET", key)); err != nil {
log.Print(err)
return "nil", err
} else {
return v, err
}
}
//GET []byte from redis
func (this *rediscli) GetBytesSlice(key string) (v []byte, err error) {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if v, err = redis.Bytes(c.Do("GET", key)); err != nil {
log.Print(err)
return nil, err
} else {
return v, err
}
}
//Remove specified key
func (this *rediscli) RemoveKey(key string) {
// 从连接池里面获得一个连接
c := this.pool.Get()
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
defer c.Close()
if _, err := c.Do("DEL", key); err != nil {
log.Print(err)
}
}