This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
109 lines (83 loc) · 2.21 KB
/
simple.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
package lru
import (
"time"
)
// New creates a new Cache of the given size
func New(size int) (Cache, error) {
lru, err := NewLRU(size)
if err != nil {
return nil, err
}
c := &SimpleCache{
lru: lru,
}
return c, nil
}
// Adds a value to the cache, or updates an item in the cache.
// It returns true if an item needed to be removed for storing the new item.
func (c *SimpleCache) Add(key interface{}, value interface{}, ttl int64) bool {
c.Lock()
defer c.Unlock()
return c.lru.Add(key, value, ttl)
}
// Returns the value of the provided key, and updates status of the item
// in the cache.
func (c *SimpleCache) Get(key interface{}) (value interface{}, ok bool) {
c.Lock()
defer c.Unlock()
return c.lru.Get(key)
}
// Check if a key exsists in the cache.
func (c *SimpleCache) Contains(key interface{}) (ok bool) {
c.Lock()
defer c.Unlock()
return c.lru.Contains(key)
}
// Expires returns the time of expiration.
func (c *SimpleCache) Expires(key interface{}) (expires time.Time, ok bool) {
c.RLock()
defer c.RUnlock()
return c.lru.Expires(key)
}
// Fetches a value which has expired, or does not exits and fills the cache.
func (c *SimpleCache) Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error) {
c.Lock()
defer c.Unlock()
return c.lru.Fetch(key, ttl, call)
}
// Removes a key from the cache.
func (c *SimpleCache) Remove(key interface{}) bool {
c.Lock()
defer c.Unlock()
return c.lru.Remove(key)
}
// Removes the oldest entry from cache.
func (c *SimpleCache) RemoveOldest() (interface{}, interface{}, bool) {
c.Lock()
defer c.Unlock()
return c.lru.RemoveOldest()
}
// Returns the oldest entry from the cache.
func (c *SimpleCache) GetOldest() (interface{}, interface{}, bool) {
c.RLock()
defer c.RUnlock()
return c.lru.GetOldest()
}
// Returns a slice of the keys in the cache, from oldest to newest.
func (c *SimpleCache) Keys() []interface{} {
c.RLock()
defer c.RUnlock()
return c.lru.Keys()
}
// Returns the number of items in the cache.
func (c *SimpleCache) Len() int {
c.RLock()
defer c.RUnlock()
return c.lru.Len()
}
// Purge is purging the full cache.
func (c *SimpleCache) Purge() {
c.Lock()
defer c.Unlock()
c.lru.Purge()
}