-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod.go
76 lines (67 loc) · 1.77 KB
/
method.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
package gcache
import (
"sync"
"time"
)
// Put save a new k-v data whether it is existing.
// If it existed in singleCache before then update it.
// flush the lru list to make the key in the tail of lru list cause you use it.
// Use NotExpire the key will not be expired
// ex is an option param which set key lifetime and only the first one value is valid
func (c *Cache[K]) Put(k K, v any, ex ...time.Duration) {
defer c.eliminate(c.fs)
if _cache, _ := c.getNode(k); _cache != nil {
_cache.put(k, v, ex...)
}
}
// Get query data by the key you set.
// If not exist will return false in second return value.
// flush the lru list to make the key in the tail of lru list cause you use it.
func (c *Cache[K]) Get(k K) (any, bool) {
if _cache, _ := c.getNode(k); _cache != nil {
return _cache.get(k)
}
return nil, false
}
func (c *Cache[K]) ExpireAt(k K) (time.Time, bool) {
if _cache, _ := c.getNode(k); _cache != nil {
return _cache.expireAt(k)
}
return NoExpire, false
}
// Keys return all keys saved in the singleCache.
// This method not flush lru list.
func (c *Cache[K]) Keys() []K {
ks := make([]K, 0, c.Size())
var wait sync.WaitGroup
for _, _cache := range c.buckets {
wait.Add(1)
go func(_cache *cache[K]) {
_cache.Lock()
defer wait.Done()
defer _cache.Unlock()
for k, v := range _cache.data {
if v.expireAt != NoExpire && v.expireAt.Before(time.Now()) {
go func() {
_cache.ex <- k
}()
continue
}
ks = append(ks, k)
}
}(_cache)
}
wait.Wait()
return ks
}
// Del delete keys from singleCache.
// remove keys from lru list which key is valid.
func (c *Cache[K]) Del(ks ...K) {
if l := len(ks); l > 0 {
for i := 0; i < l; i++ {
if _cache, _ := c.getNode(ks[i]); _cache != nil {
_cache.del(ks[i])
}
}
}
}