-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
171 lines (150 loc) · 3.84 KB
/
cache.go
File metadata and controls
171 lines (150 loc) · 3.84 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
169
170
171
package simcache
import (
"sync"
"time"
)
// Cache holds any items of type T that are cleared after a given TTL.
// The cache clears any expired items upon any retrieval operation.
type Cache[T any] struct {
*cache[T]
}
// New creates an empty Cache where the TTL for item's added will be set to the given duration.
func New[T any](defaultTTL time.Duration) *Cache[T] {
items := make(map[string]item[T])
return &Cache[T]{cache: &cache[T]{
items: items,
defaultTTL: defaultTTL,
mutex: &sync.RWMutex{},
}}
}
// Add inserts the item T into the cache for a given key if no item has been already added with the same key.
// It returns false if the item was not added due to an existing item with the same key being there.
// It returns true if the item was added successfully.
func (c *cache[T]) Add(key string, value T, ttl ...time.Duration) bool {
expiration := calculateExpiration(c.defaultTTL, ttl...)
c.mutex.RLock()
_, found := c.items[key]
if found {
c.mutex.RUnlock()
return false
}
c.mutex.RUnlock()
c.mutex.Lock()
defer c.mutex.Unlock()
c.items[key] = item[T]{
value: value,
expiration: expiration,
}
return true
}
// Set replaces the value in the cache for a given key. If no such key exists, it adds it to the cache.
// If no duration, or a value of 0, is specified it uses the default TTL when the cache was made.
// Only the first duration given is used when multiple are passed in.
func (c *cache[T]) Set(key string, value T, ttl ...time.Duration) {
expiration := calculateExpiration(c.defaultTTL, ttl...)
c.mutex.Lock()
defer c.mutex.Unlock()
c.items[key] = item[T]{
value: value,
expiration: expiration,
}
}
// Get returns the value in the cache for a given key and if it was found. If no such key exists, the returned bool will be false.
func (c *cache[T]) Get(key string) (T, bool) {
c.mutex.RLock()
i, found := c.items[key]
if !found {
c.mutex.RUnlock()
return i.value, false
}
if i.expired() {
c.mutex.RUnlock()
c.Delete(key)
return i.value, false
}
c.mutex.RUnlock()
return i.value, true
}
// Delete removes the item from the cache for the given key.
func (c *cache[T]) Delete(key string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.items, key)
}
// Items returns a copy of the cache's map that holds type T.
func (c *cache[T]) Items() map[string]T {
c.mutex.RLock()
defer c.mutex.RUnlock()
items := make(map[string]T, len(c.items))
for k, i := range c.items {
if i.expired() {
c.mutex.RUnlock()
c.Delete(k)
c.mutex.RLock()
continue
}
items[k] = i.value
}
return items
}
// Keys returns a slice of the cache's keys.
func (c *cache[T]) Keys() []string {
c.mutex.RLock()
defer c.mutex.RUnlock()
var keys []string
for k := range c.items {
keys = append(keys, k)
}
return keys
}
// Values returns a slice of the cache's values of type T.
func (c *cache[T]) Values() []T {
c.mutex.RLock()
defer c.mutex.RUnlock()
var values []T
for k, i := range c.items {
if i.expired() {
c.mutex.RUnlock()
c.Delete(k)
c.mutex.RLock()
continue
}
values = append(values, i.value)
}
return values
}
// Purge removes all expired items from the cache.
func (c *Cache[T]) Purge() int {
c.mutex.RLock()
defer c.mutex.RUnlock()
count := 0
for k, i := range c.items {
if i.expired() {
c.mutex.RUnlock()
c.Delete(k)
c.mutex.RLock()
count++
}
}
return count
}
type item[T any] struct {
value T
expiration time.Time
}
func (i *item[T]) expired() bool {
return time.Now().UTC().After(i.expiration)
}
type cache[T any] struct {
items map[string]item[T]
defaultTTL time.Duration
mutex *sync.RWMutex
}
func calculateExpiration(defaultTTL time.Duration, ttl ...time.Duration) time.Time {
t := time.Now().Add(defaultTTL).UTC()
givenValidTTL := len(ttl) > 0 && ttl[0] > 0
if givenValidTTL {
t = time.Now().Add(ttl[0]).UTC()
}
return t
}