-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
96 lines (85 loc) · 1.72 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
/*
* Copyright 2022 The Go Authors<36625090@qq.com>. All rights reserved.
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file.
*/
package lru_cache
import (
"github.com/google/uuid"
lru "github.com/hnlq715/golang-lru"
"sync"
"time"
)
type LRUCache[T any] struct {
*lru.ARCCache
sync.Mutex
keys map[string]map[string]int64
}
func NewLRUCache[T any](size int, expire time.Duration) *LRUCache[T] {
l, _ := lru.NewARCWithExpire(size, expire)
return &LRUCache[T]{ARCCache: l, keys: map[string]map[string]int64{}}
}
func (m *LRUCache[T]) SAdd(key string, val T) {
m.Lock()
defer m.Unlock()
eKey := uuid.New().String()
if m.keys[key] == nil {
m.keys[key] = map[string]int64{}
}
m.keys[key][eKey] = 0
m.ARCCache.Add(eKey, val)
}
func (m *LRUCache[T]) SMembers(key string) []T {
m.Lock()
defer m.Unlock()
var members []T
if m.keys[key] == nil {
return members
}
keys := m.keys[key]
for eKey, _ := range keys {
val, ok := m.ARCCache.Get(eKey)
if !ok {
delete(m.keys[key], eKey)
continue
}
members = append(members, val.(T))
}
return members
}
func (m *LRUCache[T]) SClear(key string) {
m.Lock()
defer m.Unlock()
if m.keys[key] == nil {
return
}
for _, k := range m.keys[key] {
m.ARCCache.Remove(k)
}
}
func (m *LRUCache[T]) SLen(key string) int {
m.Lock()
defer m.Unlock()
if m.keys[key] == nil || len(m.keys[key]) == 0 {
return 0
}
total := 0
keys := m.keys[key]
for eKey, _ := range keys {
_, ok := m.ARCCache.Get(eKey)
if ok {
total++
}
}
return total
}
func (m *LRUCache[T]) Get(key interface{}) (T, bool) {
m.Lock()
defer m.Unlock()
value, ok := m.ARCCache.Get(key)
if ok {
return value.(T), true
}
var v T
return v, false
}