-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcache.go
157 lines (136 loc) · 3.08 KB
/
cache.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
package kuu
import (
"encoding/binary"
"time"
)
// DefaultCache
var DefaultCache Cache
// Cache todo
type Cache interface {
SetString(string, string, ...time.Duration)
HasPrefix(string, int) map[string]string
HasSuffix(string, int) map[string]string
Contains(string, int) map[string]string
GetString(string) string
SetInt(string, int, ...time.Duration)
GetInt(string) int
Incr(string) int
Del(...string)
Close()
HGetAll(string) map[string]string
HGet(string, string) string
HSet(string, ...string)
Publish(channel string, message interface{}) error
Subscribe(channels []string, handler func(string, string)) error
PSubscribe(patterns []string, handler func(string, string)) error
}
func init() {
if C().Has("redis") {
// 初始化redis
DefaultCache = NewCacheRedis()
} else {
// 初始化bolt
DefaultCache = NewCacheBolt()
}
_ = DefaultCache.Subscribe([]string{intlMessagesChangedChannel}, func(c string, d string) {
ReloadIntlMessages()
})
}
func releaseCacheDB() {
if DefaultCache != nil {
DefaultCache.Close()
}
}
func itob(v int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
func btoi(b []byte) (v int) {
if b != nil {
v = int(binary.BigEndian.Uint64(b))
}
return
}
// SetCacheString
func SetCacheString(key, val string, expiration ...time.Duration) {
if DefaultCache != nil {
DefaultCache.SetString(key, val, expiration...)
}
}
// GetCacheString
func GetCacheString(key string) (val string) {
if DefaultCache != nil {
val = DefaultCache.GetString(key)
}
return
}
// SetCacheInt
func SetCacheInt(key string, val int, expiration ...time.Duration) {
if DefaultCache != nil {
DefaultCache.SetInt(key, val, expiration...)
}
}
// GetCacheInt
func GetCacheInt(key string) (val int) {
if DefaultCache != nil {
val = DefaultCache.GetInt(key)
}
return
}
// IncrCache
func IncrCache(key string) (val int) {
if DefaultCache != nil {
val = DefaultCache.Incr(key)
}
return
}
// HasPrefixCache
func HasPrefixCache(key string, limit int) (val map[string]string) {
if DefaultCache != nil {
val = DefaultCache.HasPrefix(key, limit)
}
return
}
// HasSuffixCache
func HasSuffixCache(key string, limit int) (val map[string]string) {
if DefaultCache != nil {
val = DefaultCache.HasSuffix(key, limit)
}
return
}
// ContainsCache
func ContainsCache(key string, limit int) (val map[string]string) {
if DefaultCache != nil {
val = DefaultCache.Contains(key, limit)
}
return
}
// DelCache
func DelCache(keys ...string) {
if DefaultCache != nil {
DefaultCache.Del(keys...)
}
return
}
// PublishCache
func PublishCache(channel string, message interface{}) error {
if DefaultCache != nil {
return DefaultCache.Publish(channel, message)
}
return nil
}
// SubscribeCache
func SubscribeCache(channels []string, handler func(string, string)) error {
if DefaultCache != nil {
return DefaultCache.Subscribe(channels, handler)
}
return nil
}
// PSubscribeCache
func PSubscribeCache(patterns []string, handler func(string, string)) error {
if DefaultCache != nil {
return DefaultCache.PSubscribe(patterns, handler)
}
return nil
}