forked from shaj13/libcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
163 lines (143 loc) · 3.92 KB
/
cache.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
// Package libcache provides in-memory caches based on different caches replacement algorithms.
package libcache
import (
"sync"
"time"
)
// Cache stores data so that future requests for that data can be served faster.
type Cache interface {
// Load returns key value.
Load(key interface{}) (interface{}, bool)
// Peek returns key value without updating the underlying "recent-ness".
Peek(key interface{}) (interface{}, bool)
// Update the key value without updating the underlying "recent-ness".
Update(key interface{}, value interface{})
// Store sets the key value.
Store(key interface{}, value interface{})
// StoreWithTTL sets the key value with TTL overrides the default.
StoreWithTTL(key interface{}, value interface{}, ttl time.Duration)
// Delete deletes the key value.
Delete(key interface{})
// Expiry returns key value expiry time.
Expiry(key interface{}) (time.Time, bool)
// Keys return cache records keys.
Keys() []interface{}
// Contains Checks if a key exists in cache.
Contains(key interface{}) bool
// Purge Clears all cache entries.
Purge()
// Resize cache, returning number evicted
Resize(int) int
// Len Returns the number of items in the cache.
Len() int
// Cap Returns the cache capacity.
Cap() int
// TTL returns entries default TTL.
TTL() time.Duration
// SetTTL sets entries default TTL.
SetTTL(time.Duration)
// RegisterOnEvicted registers a function,
// to call in its own goroutine when an entry is purged from the cache.
RegisterOnEvicted(f func(key, value interface{}))
// RegisterOnExpired registers a function,
// to call in its own goroutine when an entry TTL elapsed.
// invocation of f, does not mean the entry is purged from the cache,
// if need be, it must coordinate with the cache explicitly.
//
// var cache cache.Cache
// onExpired := func(key, value interface{}) {
// _, _, _ = cache.Peek(key)
// }
//
// This should not be done unless the cache thread-safe.
RegisterOnExpired(f func(key, value interface{}))
}
type cache struct {
mu sync.RWMutex
unsafe Cache
}
func (c *cache) Load(key interface{}) (interface{}, bool) {
c.mu.Lock()
defer c.mu.Unlock()
return c.unsafe.Load(key)
}
func (c *cache) Peek(key interface{}) (interface{}, bool) {
c.mu.Lock()
defer c.mu.Unlock()
return c.unsafe.Peek(key)
}
func (c *cache) Update(key interface{}, value interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.Update(key, value)
}
func (c *cache) Store(key interface{}, value interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.Store(key, value)
}
func (c *cache) StoreWithTTL(key interface{}, value interface{}, ttl time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.StoreWithTTL(key, value, ttl)
}
func (c *cache) Delete(key interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.Delete(key)
}
func (c *cache) Keys() []interface{} {
c.mu.RLock()
defer c.mu.RUnlock()
return c.unsafe.Keys()
}
func (c *cache) Contains(key interface{}) bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.unsafe.Contains(key)
}
func (c *cache) Purge() {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.Purge()
}
func (c *cache) Resize(s int) int {
c.mu.Lock()
defer c.mu.Unlock()
return c.unsafe.Resize(s)
}
func (c *cache) Len() int {
c.mu.RLock()
defer c.mu.RUnlock()
return c.unsafe.Len()
}
func (c *cache) Cap() int {
c.mu.RLock()
defer c.mu.RUnlock()
return c.unsafe.Cap()
}
func (c *cache) TTL() time.Duration {
c.mu.RLock()
defer c.mu.RUnlock()
return c.unsafe.TTL()
}
func (c *cache) SetTTL(ttl time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.SetTTL(ttl)
}
func (c *cache) RegisterOnEvicted(f func(key, value interface{})) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.RegisterOnEvicted(f)
}
func (c *cache) RegisterOnExpired(f func(key, value interface{})) {
c.mu.Lock()
defer c.mu.Unlock()
c.unsafe.RegisterOnExpired(f)
}
func (c *cache) Expiry(key interface{}) (time.Time, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
return c.unsafe.Expiry(key)
}