/
redis.go
81 lines (67 loc) · 1.54 KB
/
redis.go
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
package main
import (
"github.com/vys/Go-Redis/redis"
"log"
"time"
)
type ClientPool struct {
size int
clientChan chan redis.AsyncClient
}
func (p *ClientPool) Get() redis.AsyncClient {
return <-p.clientChan
}
func (p *ClientPool) Release(c redis.AsyncClient) {
p.clientChan <- c
}
func NewClientPool(size int) *ClientPool {
p := &ClientPool{size: size}
p.clientChan = make(chan redis.AsyncClient, size)
for i := 0; i < size; i++ {
client := NewRedisAsyncClient()
p.clientChan <- client
}
return p
}
func NewRedisAsyncClient() redis.AsyncClient {
spec := redis.DefaultSpec().Db(0).Host("127.0.0.1")
client, err := redis.NewAsynchClientWithSpec(spec)
if err != nil {
panic(err)
}
return client
}
const REDIS_GET_TIMEOUT = 20 * time.Millisecond
const REDIS_SET_TIMEOUT = 20 * time.Millisecond
func RedisGet(r redis.AsyncClient, k string) (value []byte, timeout bool) {
f, rerr := r.Get(k)
if rerr != nil {
log.Fatal("RedisGet failed: ", rerr)
}
value, rerr, timeout = f.TryGet(REDIS_GET_TIMEOUT)
if rerr != nil {
log.Fatal("RedisGet failed: ", rerr)
}
if timeout {
loadtimeout++
log.Println("load timeout! count: ", loadtimeout)
return
}
return
}
func RedisSet(r redis.AsyncClient, k string, val []byte) (timeout bool) {
f, rerr := r.Set(key, val)
if rerr != nil {
log.Fatal("RedisSet failed: ", rerr)
}
_, rerr, timeout = f.TryGet(REDIS_SET_TIMEOUT)
if rerr != nil {
log.Fatal("RedisSet failed: ", rerr)
}
if timeout {
savetimeout++
log.Println("save timeout! count: ", savetimeout)
return
}
return
}