-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaman.go
262 lines (198 loc) · 4.14 KB
/
caman.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Cache manager
// caman
// ca - cache
// man - manager
package caman
import (
"encoding/json"
"errors"
"os"
"sync"
"time"
)
type Cache struct {
sync.RWMutex
defaultExpiration time.Duration
cleanupInterval time.Duration
items map[string]Item
}
type Item struct {
Value interface{}
Created time.Time
Expiration int64
}
func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
// Initializing map
items := make(map[string]Item)
cache := Cache{
items: items,
defaultExpiration: defaultExpiration,
cleanupInterval: cleanupInterval,
}
// If cleanUp interval is greater than 0, then start GC(process of deleting old elements)
if cleanupInterval > 0 {
cache.startGC()
}
return &cache
}
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
var expiration int64
if duration == 0 {
duration = c.defaultExpiration
}
if duration > 0 {
expiration = time.Now().Add(duration).UnixNano()
}
c.Lock()
defer c.Unlock()
c.items[key] = Item{
Value: value,
Expiration: expiration,
Created: time.Now(),
}
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.RLock()
defer c.RUnlock()
item, found := c.items[key]
if !found {
return nil, false
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
return nil, false
}
}
return item.Value, true
}
func (c *Cache) Delete(key string) error {
c.Lock()
defer c.Unlock()
if _, found := c.items[key]; !found {
return errors.New("Key not found")
}
delete(c.items, key)
return nil
}
func (c *Cache) Count() int {
c.RLock()
defer c.RUnlock()
return len(c.items)
}
func (c *Cache) Rename(oldKey, newKey string) error {
c.Lock()
defer c.Unlock()
if _, found := c.items[oldKey]; !found {
return errors.New("Key not found")
}
item := c.items[oldKey]
delete(c.items, oldKey)
c.items[newKey] = item
return nil
}
func (c *Cache) FlushAll() {
c.Lock()
defer c.Unlock()
for key := range c.items {
delete(c.items, key)
}
}
func (c *Cache) Exist(value interface{}) bool {
c.RLock()
defer c.RUnlock()
for _, v := range c.items {
if v.Value == value {
return true
}
}
return false
}
func (c *Cache) Copy(key string) error {
c.RLock()
defer c.RUnlock()
item, found := c.items[key]
if !found {
return errors.New("Key not found")
}
newKey := key + "_copy"
c.items[newKey] = Item{
Value: item.Value,
Expiration: item.Expiration,
Created: time.Now(),
}
return nil
}
func (c *Cache) Increment(key string, delta int) (int, error) {
c.Lock()
defer c.Unlock()
item, found := c.items[key]
if !found {
return 0, errors.New("Key not found")
}
if intValue, ok := item.Value.(int); ok {
newValue := intValue + delta
c.items[key] = Item{
Value: newValue,
Expiration: item.Expiration,
Created: time.Now(),
}
return newValue, nil
}
return 0, errors.New("Value is not an integer")
}
func (c *Cache) Decrement(key string, delta int) (int, error) {
return c.Increment(key, -delta)
}
func (c *Cache) Expire(key string) bool {
c.RLock()
defer c.RUnlock()
item, found := c.items[key]
if !found {
return true
}
if item.Expiration > 0 {
if time.Now().UnixNano() > item.Expiration {
return true
}
}
return false
}
func (c *Cache) SaveFileJSON(filename string) error {
c.RLock()
defer c.RUnlock()
// Serialize cache to JSON
serialized, err := json.Marshal(c.items)
if err != nil {
return err
}
// Write JSON data to the file
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(serialized)
if err != nil {
return err
}
return nil
}
func (c *Cache) LoadFileJSON(filename string) error {
// Read JSON data from the file
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
// Decode JSON data into map[string]Item
var items map[string]Item
decoder := json.NewDecoder(file)
if err := decoder.Decode(&items); err != nil {
return err
}
c.Lock()
defer c.Unlock()
// Replace the existing cache items with the loaded items
c.items = items
return nil
}