-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemkey.go
More file actions
200 lines (182 loc) · 3.95 KB
/
Copy pathmemkey.go
File metadata and controls
200 lines (182 loc) · 3.95 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2026 Byterio
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package memkey
import (
"sync"
"sync/atomic"
"time"
)
var (
updaterOnce sync.Once
timestamp uint32
)
type memkey struct {
cfg Config
mu sync.RWMutex
db map[string]entry
done chan struct{}
}
type entry struct {
data []byte
expiry uint32 // max value is 4294967295 -> Sun Feb 07 2106 06:28:15 GMT+0000
}
// New creates a new memkey.
func New(config ...Config) *memkey {
cfg := configDefault(config...)
mk := &memkey{
db: make(map[string]entry),
cfg: cfg,
done: make(chan struct{}),
}
startTimeStampUpdater()
go mk.startCleanupLoop()
return mk
}
// Get value by key.
func (mk *memkey) Get(key string) ([]byte, error) {
if len(key) <= 0 {
return nil, nil
}
mk.mu.RLock()
v, ok := mk.db[key]
mk.mu.RUnlock()
if !ok || (v.expiry != 0 && v.expiry <= atomic.LoadUint32(×tamp)) {
return nil, nil
}
return v.data, nil
}
// Has returns true if entry for the given key exists.
func (mk *memkey) Has(key string) bool {
if len(key) <= 0 {
return false
}
mk.mu.RLock()
v, ok := mk.db[key]
mk.mu.RUnlock()
if !ok || (v.expiry != 0 && v.expiry <= atomic.LoadUint32(×tamp)) {
return false
}
return true
}
// Set key with value.
func (mk *memkey) Set(key string, val []byte, exp time.Duration) error {
if len(key) <= 0 || len(val) <= 0 {
return nil
}
var expire uint32
if exp != 0 {
expire = uint32(exp.Seconds()) + atomic.LoadUint32(×tamp)
}
e := entry{val, expire}
mk.mu.Lock()
mk.db[key] = e
mk.mu.Unlock()
return nil
}
// Delete key by key.
func (mk *memkey) Delete(key string) error {
if len(key) <= 0 {
return nil
}
mk.mu.Lock()
delete(mk.db, key)
mk.mu.Unlock()
return nil
}
// Reset all keys.
func (mk *memkey) Reset() error {
ndb := make(map[string]entry)
mk.mu.Lock()
mk.db = ndb
mk.mu.Unlock()
return nil
}
// Close the memkey.
func (mk *memkey) Close() error {
mk.done <- struct{}{}
return nil
}
// Keys returns all the keys.
func (mk *memkey) Keys() ([][]byte, error) {
mk.mu.RLock()
defer mk.mu.RUnlock()
if len(mk.db) == 0 {
return nil, nil
}
ts := atomic.LoadUint32(×tamp)
keys := make([][]byte, 0, len(mk.db))
for key, v := range mk.db {
if v.expiry == 0 || v.expiry > ts {
keys = append(keys, []byte(key))
}
}
if len(keys) == 0 {
return nil, nil
}
return keys, nil
}
// Size returns the number of valid entries.
func (mk *memkey) Size() int {
mk.mu.RLock()
defer mk.mu.RUnlock()
ts := atomic.LoadUint32(×tamp)
count := 0
for _, v := range mk.db {
if v.expiry == 0 || v.expiry > ts {
count++
}
}
return count
}
func startTimeStampUpdater() {
updaterOnce.Do(func() {
atomic.StoreUint32(×tamp, uint32(time.Now().Unix()))
go func(sleep time.Duration) {
ticker := time.NewTicker(sleep)
defer ticker.Stop()
for t := range ticker.C {
atomic.StoreUint32(×tamp, uint32(t.Unix()))
}
}(1 * time.Second)
})
}
func (mk *memkey) startCleanupLoop() {
ticker := time.NewTicker(mk.cfg.CleanupInterval)
defer ticker.Stop()
var exp []string
for {
select {
case <-mk.done:
return
case <-ticker.C:
ts := atomic.LoadUint32(×tamp)
exp = exp[:0]
mk.mu.RLock()
for id, v := range mk.db {
if v.expiry != 0 && v.expiry < ts {
exp = append(exp, id)
}
}
mk.mu.RUnlock()
mk.mu.Lock()
for i := range exp {
v := mk.db[exp[i]]
if v.expiry != 0 && v.expiry <= ts {
delete(mk.db, exp[i])
}
}
mk.mu.Unlock()
}
}
}