-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachememory.go
212 lines (168 loc) · 3.58 KB
/
cachememory.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
209
210
211
212
package mxcache
import (
"encoding/gob"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type ExpireErrors []error
func (e ExpireErrors) Error() string {
var s strings.Builder
last := len(e) - 1
for i, err := range e {
s.WriteString(err.Error())
if i < last {
s.WriteString(", ")
}
}
return s.String()
}
func (e ExpireErrors) Err() error {
if len(e) > 0 {
return e
}
return nil
}
type cacheStorage map[string]cit
type cit struct {
Tstamp time.Time
Ex time.Duration
Datum interface{}
}
type memoryCache struct {
Cache cacheStorage
mutex *sync.RWMutex
persist string
ticker *time.Ticker
}
func newMemoryCache(u *url.URL) (MXCacher, error) {
ttl := 0
configTtl := u.Query().Get("ttl")
persist := u.Query().Get("persist")
if configTtl == "" {
configTtl = "0"
}
ttl, err := strconv.Atoi(configTtl)
if err != nil {
return nil, err
}
c := &memoryCache{}
c.mutex = &sync.RWMutex{}
c.Cache = make(cacheStorage)
c.persist = persist
c.loadPersistentData()
go func(ttl int) {
if ttl == 0 {
return
}
c.ticker = time.NewTicker(time.Duration(ttl) * time.Second)
//tick := time.Tick(time.Duration(ttl) * time.Second)
tick := c.ticker.C
for range tick {
c.gc()
}
}(ttl)
return c, nil
}
func (c *memoryCache) Set(key string, data interface{}, ex int) error {
c.mutex.Lock()
defer c.mutex.Unlock()
ttl := time.Duration(0)
if ex > 0 {
ttl = time.Duration(ex) * time.Second
}
c.Cache[key] = cit{
Tstamp: time.Now(),
Ex: ttl,
Datum: data,
}
c.persistData()
return nil
}
func (c *memoryCache) Get(key string) (interface{}, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()
cacheUnit, ok := c.Cache[key]
if !ok {
// no error, just no value
return nil, nil
}
if cacheUnit.Ex > 0 && time.Since(cacheUnit.Tstamp) >= cacheUnit.Ex {
//delete(c.cache, key)
return nil, nil
}
//value found
return cacheUnit.Datum, nil
}
func (c *memoryCache) Expire(pattern string) (expiredKeys, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
defer log.Printf("Memory cache after expiring %+v\n", c.Cache)
var exkeys expiredKeys
if !strings.Contains(pattern, "*") {
exkeys = append(exkeys, pattern)
delete(c.Cache, pattern)
c.persistData()
return exkeys, nil
}
var exerr ExpireErrors
for k, cu := range c.Cache {
if match, err := filepath.Match(pattern, k); err != nil {
exerr = append(exerr, err)
} else if match {
if cu.Ex > 0 {
exkeys = append(exkeys, k)
delete(c.Cache, k)
}
}
}
c.persistData()
return exkeys, exerr.Err()
}
func (c *memoryCache) gc() {
c.mutex.Lock()
defer c.mutex.Unlock()
log.Println("Starting GC")
for key, cacheUnit := range c.Cache {
if cacheUnit.Ex > 0 && time.Since(cacheUnit.Tstamp) >= cacheUnit.Ex {
delete(c.Cache, key)
}
}
log.Println("Done GC")
c.persistData()
}
func (c *memoryCache) persistData() {
if c.persist == "" {
return
}
f, err := os.OpenFile(c.persist, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println("cache persist data opening file", c.persist, err)
return
}
defer f.Close()
if err := gob.NewEncoder(f).Encode(c.Cache); err != nil {
log.Println("cache persisting data on", c.persist, err)
return
}
}
func (c *memoryCache) loadPersistentData() {
if c.persist == "" {
return
}
f, err := os.OpenFile(c.persist, os.O_RDONLY, 0644)
if err != nil {
//log.Println("cache load persistent data opening file", c.persist, err)
return
}
defer f.Close()
if err := gob.NewDecoder(f).Decode(&c.Cache); err != nil {
log.Println("cache load persistent data on", c.persist, err)
return
}
}