-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcache_counter.go
53 lines (45 loc) · 1.09 KB
/
memcache_counter.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
package frame
import (
"strconv"
"sync"
)
//定义mc 计数器
type mcCounter struct {
mc *memcached
mcOnce sync.Once
GroupName string
}
func (mcCounter *mcCounter) getMc() *memcached {
if mcCounter.mc == nil {
mcCounter.mcOnce.Do(func() {
mcCounter.mc = &memcached{GroupName: mcCounter.GroupName}
})
}
return mcCounter.mc
}
func (mcCounter *mcCounter) Get(key string) (int, error) {
res, err := mcCounter.getMc().Get(key)
if err != nil {
return 0, err
}
result, err := strconv.Atoi(res)
if err != nil {
if res == "" {
return 0, nil
}
return 0, err
}
return result, nil
}
func (mcCounter *mcCounter) Set(key string, value int, ttl ...int) bool {
return mcCounter.getMc().Set(key, value, ttl...)
}
func (mcCounter *mcCounter) Delete(key string) bool {
return mcCounter.getMc().Delete(key)
}
func (mcCounter *mcCounter) Increment(key string, options ...int) (int, error) {
return mcCounter.getMc().Increment(key, options...)
}
func (mcCounter *mcCounter) Decrement(key string, options ...int) (int, error) {
return mcCounter.getMc().Decrement(key, options...)
}