/
cache.go
169 lines (137 loc) · 3.22 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
158
159
160
161
162
163
164
165
166
167
168
169
package growl
import (
"reflect"
"sync"
"time"
"github.com/go-redis/cache"
"github.com/go-redis/redis"
gocache "github.com/patrickmn/go-cache"
msgpack "gopkg.in/vmihailenco/msgpack.v2"
)
var connRedis *redis.Client
var LocalCache = gocache.New(YamlConfig.Growl.Redis.duration, 30*time.Minute)
var redisOnce sync.Once
type codecStruct struct {
codec *cache.Codec
sync sync.Mutex
}
var codec *codecStruct
func Cache() *codecStruct {
return codec
}
func connectRedis() *redis.Client {
config := YamlConfig.Growl
return redis.NewClient(&redis.Options{
Addr: config.Redis.Host + ":" + config.Redis.Port,
Password: config.Redis.Password,
DB: 0,
})
}
func Redis() *redis.Client {
redisOnce.Do(func() {
connRedis = connectRedis()
})
return connRedis
}
func Codec() *codecStruct {
return &codecStruct{
codec: &cache.Codec{
Redis: connRedis,
Marshal: func(v interface{}) ([]byte, error) {
return msgpack.Marshal(v)
},
Unmarshal: func(b []byte, v interface{}) error {
return msgpack.Unmarshal(b, v)
},
},
}
}
func PingCache() error {
_, err := Redis().Ping().Result()
return err
}
func FlushCache() {
config := YamlConfig.Growl
if config.Misc.LocalCache {
LocalCache.Flush()
}
if config.Redis.Enable {
Redis().FlushDB()
}
}
func GetCache(key string, data interface{}, options ...interface{}) error {
return codec.GetCache(key, data)
}
func (codec *codecStruct) GetCache(key string, data interface{}) (err error) {
codec.sync.Lock()
defer codec.sync.Unlock()
config := YamlConfig.Growl
if config.Misc.LocalCache {
cacheData, found := LocalCache.Get(key)
if config.Misc.Log {
// fmt.Println("get local key", key)
// log.Println("get local ", key, " found : ", found)
// fmt.Println("get local cachedata", cacheData)
}
if found {
x := reflect.ValueOf(data)
x.Elem().Set(reflect.ValueOf(cacheData).Elem())
return
} else {
err = ErrCacheNotFound
}
}
if config.Redis.Enable {
err = codec.codec.Get(key, data)
if config.Misc.Log {
// fmt.Println("get redis key", key)
// log.Println("get redis ", key, " error : ", err)
// fmt.Println("get redis data", data)
}
if err == nil {
LocalCache.Set(key, data, YamlConfig.Growl.Redis.duration)
return
}
}
if !config.Redis.Enable && !config.Misc.LocalCache {
err = ErrCacheDisabled
}
return
}
func SetCache(key string, data interface{}, options ...interface{}) {
codec.SetCache(key, data, options...)
}
func (codec *codecStruct) SetCache(key string, data interface{}, options ...interface{}) {
codec.sync.Lock()
defer codec.sync.Unlock()
config := YamlConfig.Growl
duration := config.Redis.duration
if len(options) >= 1 && len(options) == 1 {
if val, ok := options[0].(time.Duration); ok {
if val.String() != "0s" {
duration = val
}
}
}
if config.Misc.LocalCache {
LocalCache.Set(key, data, duration)
}
if config.Redis.Enable {
codec.codec.Set(&cache.Item{
Key: key,
Object: data,
Expiration: duration,
})
}
}
func (codec *codecStruct) DeleteCache(key string) {
codec.sync.Lock()
defer codec.sync.Unlock()
config := YamlConfig.Growl
if config.Misc.LocalCache {
LocalCache.Delete(key)
}
if config.Redis.Enable {
codec.codec.Delete(key)
}
}