-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.go
112 lines (96 loc) · 2.15 KB
/
storage.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
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
package main
import (
"errors"
"time"
"github.com/garyburd/redigo/redis"
)
const (
longmapName = "longToShort"
shortmapName = "shortToLong"
custommapName = "customToLong"
)
var (
pool redis.Pool
)
// StringStore is a map from string to string
type StringStore interface {
Get(key string) (string, error)
Set(key, value string) error
}
func newStringStore(name string) StringStore {
return &redisMap{pool: pool, mapName: name}
}
type redisMap struct {
pool redis.Pool
mapName string
}
func (r *redisMap) Get(key string) (string, error) {
conn := r.pool.Get()
defer conn.Close()
return redis.String(conn.Do("HGET", r.mapName, key))
}
func (r *redisMap) Set(key, value string) error {
conn := r.pool.Get()
defer conn.Close()
_, err := conn.Do("HSET", r.mapName, key, value)
return err
}
// ExpireStore is a map from string to int.
// Keys can be incremented and are expired automatically.
type ExpireStore interface {
Get(key string) (int, error)
Incr(key string, expiry int) error
TTL(key string) (int, error)
}
func newExpireStore() ExpireStore {
return &redisExpire{pool: pool}
}
type redisExpire struct {
pool redis.Pool
}
func (r *redisExpire) Get(key string) (int, error) {
conn := r.pool.Get()
defer conn.Close()
return redis.Int(conn.Do("GET", key))
}
func (r *redisExpire) Incr(key string, expireSecs int) error {
conn := r.pool.Get()
defer conn.Close()
conn.Send("INCR", key)
conn.Send("EXPIRE", key, expireSecs)
conn.Flush()
_, err := conn.Receive()
return err
}
func (r *redisExpire) TTL(key string) (int, error) {
conn := r.pool.Get()
defer conn.Close()
ttl, err := redis.Int(conn.Do("TTL", key))
if err != nil {
return ttl, err
}
if ttl < -1 {
return ttl, errors.New("Key does not exist")
}
return ttl, nil
}
func setupRedis(port string) error {
pool = redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", port)
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
conn := pool.Get()
_, err := conn.Do("PING")
return err
}