-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_storage.go
194 lines (169 loc) · 5.21 KB
/
counter_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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package storage
import (
"time"
"github.com/dropbox/godropbox/errors"
log "github.com/golang/glog"
"github.com/1024casts/go-common/context"
)
type CounterStorage interface {
Get(ctx *context.Context, key Key) (value int64, err error)
Set(ctx *context.Context, key Key, value int64) error
Incr(ctx *context.Context, key Key, step int64) (newValue int64, err error)
Decr(ctx *context.Context, key Key, step int64) (newValue int64, err error)
Delete(ctx *context.Context, key ...Key) error
MultiGet(ctx *context.Context, keys []Key, values map[Key]int64) (err error)
MultiSet(ctx *context.Context, m map[Key]int64) error
}
type CounterRedisStorage struct {
client RedisClient
KeyPrefix string
BenchMarkKeyPrefix string
DefaultExpireTime time.Duration
encoding Encoding
}
func NewCounterRedisStorage(client RedisClient, keyPrefix string, BenchMarkKeyPrefix string, defaultExpireTime time.Duration) CounterStorage {
return CounterRedisStorage{client, keyPrefix, BenchMarkKeyPrefix, defaultExpireTime, Int64Encoding{}}
}
func (this CounterRedisStorage) Incr(ctx *context.Context, key Key, step int64) (newValue int64, err error) {
var cacheKey string
cacheKey, err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
return 0, errors.Wrap(err, "build cache key error")
}
result, errcache := this.client.Incr(cacheKey, step)
if this.DefaultExpireTime > 0 {
this.client.Expire(cacheKey, this.DefaultExpireTime)
}
return int64(result), errcache
}
func (this CounterRedisStorage) Decr(ctx *context.Context, key Key, step int64) (newValue int64, err error) {
var cacheKey string
cacheKey, err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
return 0, errors.Wrap(err, "build cache key error")
}
result, errcache := this.client.Decr(cacheKey, step)
if result < 0 {
return 0, err
}
if this.DefaultExpireTime > 0 {
this.client.Expire(cacheKey, this.DefaultExpireTime)
}
return int64(result), errcache
}
func (this CounterRedisStorage) Get(ctx *context.Context, key Key) (value int64, err error) {
var cacheKey string
cacheKey, err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
return 0, errors.Wrap(err, "build cache key error")
}
data, err := this.client.Get(cacheKey)
if err != nil {
if err.Error() == "redis: nil" {
// log.Infoln(err)
} else {
return 0, errors.Wrapf(err, "get from redis error key is %s", cacheKey)
}
}
if data == nil || len(data) == 0 {
return 0, EmptyObjectError{key.String()}
}
err = this.encoding.Unmarshal(data, &value)
if err != nil {
return 0, errors.Wrapf(err, "unmarshal error , is %s ", string(data))
}
return value, nil
}
func (this CounterRedisStorage) Set(ctx *context.Context, key Key, value int64) error {
var (
cacheKey string
err error
)
cacheKey, err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
return errors.Wrap(err, "build cache key error")
}
buf, err := this.encoding.Marshal(value)
if err != nil {
return errors.Wrapf(err, "marshal error,data is %+v", value)
}
if err = this.client.Set(cacheKey, buf, this.DefaultExpireTime); err != nil {
return errors.Wrap(err, "redis set error")
}
return nil
}
func (this CounterRedisStorage) Delete(ctx *context.Context, keyList ...Key) error {
if len(keyList) == 0 {
return nil
}
var cacheKeyList []string = make([]string, len(keyList))
var err error
for storagekeyIdx, key := range keyList {
cacheKeyList[storagekeyIdx], err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
return errors.Wrapf(err, "build cache key error ,key is %+v", key)
}
}
_, err = this.client.Del(cacheKeyList...)
if err != nil {
return errors.Wrapf(err, "redis delete error,keys is %+v", keyList)
}
return nil
}
func (this CounterRedisStorage) MultiGet(ctx *context.Context, keys []Key, values map[Key]int64) (err error) {
if len(keys) == 0 {
return nil
}
cacheKeys := make([]string, len(keys))
for index, key := range keys {
cacheKey := ""
cacheKey, err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
return errors.Wrapf(err, "build cache key error ,key is %+v", key)
}
cacheKeys[index] = cacheKey
}
val, err := this.client.MGet(cacheKeys...)
if err != nil {
return errors.Wrap(err, "redis get error")
}
for i, value := range val {
if value == nil {
continue
}
var object int64
err := this.encoding.Unmarshal([]byte(value.(string)), &object)
if err != nil {
log.Warningf("cant't unmarshal json ,json string is %+v", value)
continue
}
values[keys[i]] = object
}
return nil
}
func (this CounterRedisStorage) MultiSet(ctx *context.Context, valueMap map[Key]int64) error {
if len(valueMap) == 0 {
return nil
}
values := make([]interface{}, 0, 2*len(valueMap))
for key, value := range valueMap {
buf, err := this.encoding.Marshal(value)
if err != nil {
log.Warningf("cant't unmarshal json ,json string is %+v", value)
continue
}
cacheKey := ""
cacheKey, err = BuildCacheKey(this.KeyPrefix, key)
if err != nil {
log.Warningf("build cache key error ,key is %+v", key)
continue
}
values = append(values, ([]byte(cacheKey)))
values = append(values, (buf))
}
err := this.client.MSet(this.DefaultExpireTime, values...)
if err != nil {
return errors.Wrap(err, "redis set error")
}
return nil
}