-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheimpl.go
208 lines (175 loc) · 4.72 KB
/
cacheimpl.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package encache
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"time"
"github.com/redis/go-redis/v9"
)
type MapCacheImpl struct {
cache map[string]cacheEntry
}
type cacheEntry struct {
value []reflect.Value
expiryTime time.Time
}
// for slice
// size[0] is length and size[1] is capacity
// if size[1] not passed length and capacity are both equal to size[0]
// for map
// size[0] is the size
func NewMapCacheImpl(size ...int) *MapCacheImpl {
if len(size) > 1 {
panic("too many arguments")
}
var cache map[string]cacheEntry
if len(size) > 0 {
cache = make(map[string]cacheEntry, size[0])
} else {
cache = make(map[string]cacheEntry)
}
return &MapCacheImpl{
cache: cache,
}
}
func (cacheImpl *MapCacheImpl) Get(key string, _ reflect.Type) ([]reflect.Value, bool, error) {
if res, ok := cacheImpl.cache[key]; ok && res.expiryTime.After(time.Now()) {
return res.value, true, nil
}
return nil, false, nil
}
func (cacheImpl *MapCacheImpl) Set(key string, value []reflect.Value, expiry time.Duration) error {
cacheImpl.cache[key] = cacheEntry{
value: value,
expiryTime: time.Now().Add(expiry),
}
return nil
}
// just to satisfy the interface
func (cacheImpl *MapCacheImpl) Serialize(res []reflect.Value) (string, error) {
return "", nil
}
// just to satisfy the interface
func (cacheImpl *MapCacheImpl) Deserialize(serializedResult string, fType reflect.Type) ([]reflect.Value, error) {
return nil, nil
}
// expire after a certain duration
func (cacheImpl *MapCacheImpl) Expire(key string, expiry time.Duration) error {
muLockImpl := NewMuLockImpl()
lockerr := muLockImpl.lock()
if lockerr != nil {
// unreachable
log.Println("error in lock: ", lockerr)
panic("error in lock: " + lockerr.Error())
}
defer func() {
unlockerr := muLockImpl.unlock()
if unlockerr != nil {
// unreachable
log.Println("error in unlock: ", unlockerr)
panic("error in unlock: " + unlockerr.Error())
}
}()
cacheEntry, ok := cacheImpl.cache[key]
if ok {
if expiry <= 0 {
delete(cacheImpl.cache, key)
return nil
} else {
seterr := cacheImpl.Set(key, cacheEntry.value, expiry)
return seterr
}
}
return nil
}
// start a goroutine to periodically check and remove expired cache entries
func (cacheImpl *MapCacheImpl) PeriodicExpire(runOnDuration time.Duration) {
go func() {
for {
time.Sleep(runOnDuration)
for key, entry := range cacheImpl.cache {
if entry.expiryTime.Before(time.Now()) {
err := cacheImpl.Expire(key, 0)
if err != nil {
log.Println("error in periodic expire: ", err)
}
}
}
}
}()
}
type RedisCacheImpl struct {
client redis.UniversalClient
}
func NewRedisCacheImpl(client redis.UniversalClient) *RedisCacheImpl {
return &RedisCacheImpl{
client: client,
}
}
func (cacheImpl *RedisCacheImpl) Serialize(res []reflect.Value) (string, error) {
serializedRes, err := json.Marshal(res)
if err != nil {
return "", err
}
return string(serializedRes), nil
}
func (cacheImpl *RedisCacheImpl) Deserialize(serializedResult string, fType reflect.Type) ([]reflect.Value, error) {
var results []interface{}
err := json.Unmarshal([]byte(serializedResult), &results)
if err != nil {
return nil, err
}
res := make([]reflect.Value, len(results))
for i := range results {
res[i] = reflect.New(fType.Out(i)).Elem()
res[i].Set(reflect.ValueOf(results[i]))
}
return res, nil
}
func (cacheImpl *RedisCacheImpl) Get(key string, fType reflect.Type) ([]reflect.Value, bool, error) {
ctx := context.Background()
cachedResult, err := cacheImpl.client.Get(ctx, key).Result()
if err != nil && err != redis.Nil {
return nil, false, err
}
if err == redis.Nil {
return nil, false, nil
}
returnValue, err := cacheImpl.Deserialize(cachedResult, fType)
if err != nil {
return nil, false, err
}
return returnValue, true, nil
}
func (cacheImpl *RedisCacheImpl) Set(key string, value []reflect.Value, expiry time.Duration) error {
ctx := context.Background()
serializedResult, err := cacheImpl.Serialize(value)
if err != nil {
return err
}
err = cacheImpl.client.Set(ctx, key, serializedResult, expiry).Err()
if err != nil {
return err
}
return nil
}
// just to satisfy the interface as expirations happens automatically
func (cacheImpl *RedisCacheImpl) PeriodicExpire(_ time.Duration) {}
func (cacheImpl *RedisCacheImpl) Expire(key string, expiry time.Duration) error {
ctx := context.Background()
err := cacheImpl.client.Expire(ctx, key, expiry).Err()
return err
}
type CacheKeyImpl struct{}
func NewDefaultCacheKeyImpl() *CacheKeyImpl {
return &CacheKeyImpl{}
}
func (cacheImpl *CacheKeyImpl) Key(fName string, args []reflect.Value) string {
key := fName
for _, arg := range args {
key += fmt.Sprintf("%v", arg.Interface())
}
return key
}