-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_cache.go
More file actions
157 lines (139 loc) · 2.99 KB
/
local_cache.go
File metadata and controls
157 lines (139 loc) · 2.99 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
package cache
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
var (
errKeyNotFound = errors.New("cache:键不存在")
//errKeyExpired = errors.New("cache:键过期")
)
type MapCacheOption func(cache *MapCache)
type MapCache struct {
data map[string]*item
//data sync.Map
mutex sync.RWMutex
close chan struct{}
// 删除回调方法
onEvicted func(key string, val any)
//onEvicted func(ctx context.Context, key string, val any)
//onEvicteds []func(key string, val any)
}
func NewMapCache(interval time.Duration, opts ...MapCacheOption) *MapCache {
res := &MapCache{
data: make(map[string]*item, 100),
close: make(chan struct{}),
onEvicted: func(key string, val any) {},
}
for _, opt := range opts {
opt(res)
}
go func() {
ticker := time.NewTicker(interval)
for {
// 定时检测批量数据, 删除过期 K-V
select {
case t := <-ticker.C:
res.mutex.Lock()
i := 0
for key, val := range res.data {
if i > 10000 {
break
}
if val.deadlineBefore(t) {
res.delete(key)
}
i++
}
res.mutex.Unlock()
case <-res.close:
return
}
}
}()
return res
}
func BuildInMapCacheWithEvictedCallback(fn func(key string, val any)) MapCacheOption {
return func(cache *MapCache) {
cache.onEvicted = fn
}
}
func (b *MapCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error {
b.mutex.Lock()
defer b.mutex.Unlock()
return b.set(key, val, expiration)
}
func (b *MapCache) set(key string, val any, expiration time.Duration) error {
var dl time.Time
if expiration > 0 {
dl = time.Now().Add(expiration)
}
b.data[key] = &item{
val: val,
deadline: dl,
}
return nil
}
func (b *MapCache) Get(ctx context.Context, key string) (any, error) {
b.mutex.RLock()
res, ok := b.data[key]
b.mutex.RUnlock()
if !ok {
return nil, fmt.Errorf("%w, key: %s", errKeyNotFound, key)
}
now := time.Now()
if res.deadlineBefore(now) {
b.mutex.Lock()
defer b.mutex.Unlock()
res, ok = b.data[key]
if !ok {
return nil, fmt.Errorf("%w, key: %s", errKeyNotFound, key)
}
if res.deadlineBefore(now) {
b.delete(key)
return nil, fmt.Errorf("%w, key: %s", errKeyNotFound, key)
}
}
return res.val, nil
}
func (b *MapCache) Delete(ctx context.Context, key string) error {
b.mutex.Lock()
defer b.mutex.Unlock()
b.delete(key)
return nil
}
func (b *MapCache) LoadAndDelete(ctx context.Context, key string) (any, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
val, ok := b.data[key]
if !ok {
return nil, errKeyNotFound
}
b.delete(key)
return val.val, nil
}
func (b *MapCache) delete(key string) {
itm, ok := b.data[key]
if !ok {
return
}
delete(b.data, key)
b.onEvicted(key, itm.val)
}
func (b *MapCache) Close() error {
select {
case b.close <- struct{}{}:
default:
return errors.New("重复关闭")
}
return nil
}
type item struct {
val any
deadline time.Time
}
func (i *item) deadlineBefore(t time.Time) bool {
return !i.deadline.IsZero() && i.deadline.Before(t)
}