-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_trait.go
78 lines (67 loc) · 2.06 KB
/
counter_trait.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
package frame
import "runtime/debug"
const CounterTypeMc = "mc"
const CounterTypeRedis = "redis"
//计数器基础结构体 一般放于另外结构体里面当匿名属性以便实现继承
//用法介绍
// type UserCounter struct{
// frame.CounterTrait
// }
// func GetUserCounter() *UserCounter {
// counter:=&UserCounter{}
// counter.Type=frame.CounterTypeRedis
// counter.Group="redis/main"
// counter.Ttl=60
// counter.PreFixKey="userInfo"
// return counter
// }
type CounterTrait struct {
counter Counter
Type string
Group string
Ttl int
PreFixKey string
}
func (counterTrait *CounterTrait) GetCounter() Counter {
if counterTrait.counter != nil {
return counterTrait.counter
}
if counterTrait.Type == CounterTypeMc {
counterTrait.counter = &mcCounter{
GroupName: counterTrait.Group,
}
} else if counterTrait.Type == CounterTypeRedis {
counterTrait.counter = &redisCounter{
GroupName: counterTrait.Group,
}
} else {
msg := map[string]interface{}{
"error": CounterError,
"stack": string(debug.Stack()),
}
App().Log.Error(msg, LogCounterError)
panic(CounterError)
}
return counterTrait.counter
}
func (counterTrait *CounterTrait) Key(key string) string {
return counterTrait.PreFixKey + key
}
func (counterTrait *CounterTrait) Get(key string) (int, error) {
return counterTrait.GetCounter().Get(counterTrait.Key(key))
}
func (counterTrait *CounterTrait) Increment(key string, options ...int) (int, error) {
return counterTrait.GetCounter().Increment(counterTrait.Key(key), options...)
}
func (counterTrait *CounterTrait) Decrement(key string, options ...int) (int, error) {
return counterTrait.GetCounter().Decrement(counterTrait.Key(key), options...)
}
func (counterTrait *CounterTrait) Set(key string, value int, ttl ...int) bool {
if len(ttl) == 0 {
ttl = []int{counterTrait.Ttl}
}
return counterTrait.GetCounter().Set(counterTrait.Key(key), value, ttl...)
}
func (counterTrait *CounterTrait) Delete(key string) bool {
return counterTrait.GetCounter().Delete(counterTrait.Key(key))
}