Repository navigation
Expand file tree
/
Copy pathdriver_memory.go
More file actions
168 lines (135 loc) · 2.9 KB
/
Copy pathdriver_memory.go
File metadata and controls
168 lines (135 loc) · 2.9 KB
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
package cache
import (
"sync"
"time"
)
// Item for memory cache
type Item struct {
// Exp expire time
Exp int64
// Val cache value storage
Val any
}
// Expired check whether expired
func (item Item) Expired() bool {
return item.Exp > 1 && item.Exp < time.Now().Unix()
}
// MemoryCache definition.
type MemoryCache struct {
// locker
lock sync.RWMutex
// cache data in memory. or use sync.Map
caches map[string]*Item
// CacheSize TODO set max cache size
CacheSize int
}
// NewMemoryCache create a memory cache instance
func NewMemoryCache() *MemoryCache {
return &MemoryCache{
caches: make(map[string]*Item),
}
}
// Has cache key
func (c *MemoryCache) Has(key string) bool {
c.lock.RLock()
defer c.lock.RUnlock()
return c.get(key) != nil
}
// Get cache value by key
func (c *MemoryCache) Get(key string) any {
c.lock.RLock()
defer c.lock.RUnlock()
return c.get(key)
}
func (c *MemoryCache) get(key string) any {
if item, ok := c.caches[key]; ok {
// check expire time. if has been expired, remove it.
if item.Expired() {
_ = c.del(key)
return nil
}
return item.Val
}
return nil
}
// Set cache value by key
func (c *MemoryCache) Set(key string, val any, ttl time.Duration) (err error) {
c.lock.Lock()
defer c.lock.Unlock()
return c.set(key, val, ttl)
}
func (c *MemoryCache) set(key string, val any, ttl time.Duration) (err error) {
item := &Item{Val: val}
if ttl > 0 {
item.Exp = time.Now().Unix() + int64(ttl/time.Second)
}
c.caches[key] = item
return
}
// Del cache by key
func (c *MemoryCache) Del(key string) error {
c.lock.Lock()
defer c.lock.Unlock()
return c.del(key)
}
func (c *MemoryCache) del(key string) error {
if _, ok := c.caches[key]; ok {
delete(c.caches, key)
}
return nil
}
// GetMulti values by multi key
func (c *MemoryCache) GetMulti(keys []string) map[string]any {
c.lock.RLock()
data := make(map[string]any, len(keys))
for _, key := range keys {
data[key] = c.get(key)
}
c.lock.RUnlock()
return data
}
// SetMulti values by multi key
func (c *MemoryCache) SetMulti(values map[string]any, ttl time.Duration) (err error) {
c.lock.Lock()
for key, val := range values {
if err = c.set(key, val, ttl); err != nil {
return
}
}
c.lock.Unlock()
return
}
// DelMulti values by multi key
func (c *MemoryCache) DelMulti(keys []string) error {
c.lock.Lock()
for _, key := range keys {
_ = c.del(key)
}
c.lock.Unlock()
return nil
}
// Close cache
func (c *MemoryCache) Close() error {
return nil
}
// Clear all caches
func (c *MemoryCache) Clear() error {
c.caches = nil
return nil
}
// Count cache item number
func (c *MemoryCache) Count() int {
return len(c.caches)
}
// Restore DB from a file
func (c *MemoryCache) Restore(file string) error {
return nil
}
// DumpDB to a file
func (c *MemoryCache) DumpDB(file string) error {
return nil
}
// Iter iteration all caches
func (c *MemoryCache) Iter(file string) error {
return nil
}