-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemory.go
135 lines (117 loc) · 2.9 KB
/
memory.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
package cachita
import (
"sync"
"time"
)
var mCache Cache
type memory struct {
recordsMu sync.RWMutex
records map[string]*record
tagsMu sync.Mutex
tags map[string][]string
ttl time.Duration
}
func Memory() Cache {
if mCache == nil {
mCache = NewMemoryCache(1*time.Minute, 1*time.Minute)
}
return mCache
}
func NewMemoryCache(ttl, tickerTtl time.Duration) Cache {
c := &memory{
records: make(map[string]*record),
tags: make(map[string][]string),
ttl: ttl,
}
runEvery(tickerTtl, func() {
c.deleteExpired()
})
return c
}
func (c *memory) Get(key string, i interface{}) error {
c.recordsMu.RLock()
r, exists := c.records[key]
c.recordsMu.RUnlock()
if !exists {
return ErrNotFound
}
if r.ExpiredAt.Before(time.Now()) {
return ErrExpired
}
return TypeAssert(r.Data, i)
}
func (c *memory) Put(key string, i interface{}, ttl time.Duration) error {
r := &record{Data: i, ExpiredAt: expiredAt(ttl, c.ttl)}
c.recordsMu.Lock()
defer c.recordsMu.Unlock()
c.records[key] = r
return nil
}
func (c *memory) Incr(key string, ttl time.Duration) (int64, error) {
n := int64(1)
c.recordsMu.Lock()
defer c.recordsMu.Unlock()
r, exists := c.records[key]
if !exists || r.ExpiredAt.Before(time.Now()) {
r = &record{Data: n, ExpiredAt: expiredAt(ttl, c.ttl)}
} else {
n = r.Data.(int64) + 1
}
r.Data = n
c.records[key] = r
return n, nil
}
func (c *memory) Invalidate(key string) error {
c.recordsMu.Lock()
defer c.recordsMu.Unlock()
delete(c.records, key)
return nil
}
func (c *memory) Exists(key string) bool {
c.recordsMu.RLock()
defer c.recordsMu.RUnlock()
r, exists := c.records[key]
return exists && r.ExpiredAt.After(time.Now())
}
func (c *memory) deleteExpired() {
records := make(map[string]*record)
c.recordsMu.Lock()
defer c.recordsMu.Unlock()
for k, r := range c.records {
if r.ExpiredAt.After(time.Now()) {
records[k] = r
}
}
c.records = records
}
func (c *memory) InvalidateMulti(keys ...string) error {
c.recordsMu.Lock()
defer c.recordsMu.Unlock()
for _, key := range keys {
delete(c.records, key)
}
return nil
}
func (c *memory) Tag(key string, tags ...string) error {
tags = uniqueTags(tags)
c.tagsMu.Lock()
defer c.tagsMu.Unlock()
for _, t := range tags {
if inArr(c.tags[t], key) {
continue
}
c.tags[t] = append(c.tags[t], key)
}
return nil
}
func (c *memory) InvalidateTags(tags ...string) error {
tags = uniqueTags(tags)
c.tagsMu.Lock()
var keys []string
for _, t := range tags {
keys = append(keys, c.tags[t]...)
delete(c.tags, t)
}
c.tagsMu.Unlock()
return c.InvalidateMulti(keys...)
}