Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
179 lines (123 loc) · 2.74 KB
/
Copy pathcache.go
File metadata and controls
179 lines (123 loc) · 2.74 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
package main
import (
"context"
"hash/fnv"
"log"
"strings"
"sync"
"time"
)
type item struct{
value []byte
expiresAt time.Time
}
type cacheShard struct{
mu sync.RWMutex
data map[string]item
}
const shardCount = 32
type cacheShardMap struct{
shards [shardCount]*cacheShard
maxCount int
windowSec time.Duration
}
func NewShardMap(maxCount int , windowsSec int) *cacheShardMap{
shardMap:= &cacheShardMap{
maxCount: maxCount,
windowSec: time.Duration(windowsSec)*time.Second,
}
for i := 0; i < shardCount; i++ {
shardMap.shards[i] = &cacheShard{
data: make(map[string]item),
}
}
return shardMap
}
func (c *cacheShardMap) getShard(key string) *cacheShard{
h:= fnv.New32a()
h.Write([]byte(key))
shardIndex:= h.Sum32() & (shardCount - 1)
return c.shards[shardIndex]
}
func (c *cacheShardMap) Set(key string , value []byte , ttl time.Duration){
shard:= c.getShard(key)
shard.mu.Lock()
defer shard.mu.Unlock()
var expiresAt time.Time
if ttl > 0 {
expiresAt = time.Now().Add(ttl)
}
shard.data[key] = item{
value: value,
expiresAt: expiresAt,
}
}
func (c *cacheShardMap) Get(key string) ([]byte, bool) {
shard:= c.getShard(key)
shard.mu.RLock()
defer shard.mu.RUnlock()
sanitizedKey := strings.TrimSpace(key)
if sanitizedKey == "" {
return nil, false
}
item, exists := shard.data[sanitizedKey]
if !exists {
return nil, false
}
// Check TTL expiration
if !item.expiresAt.IsZero() && time.Now().After(item.expiresAt) {
return nil, false
}
return item.value, true
}
func (c *cacheShardMap) Delete(key string){
shard:= c.getShard(key)
shard.mu.Lock()
defer shard.mu.Unlock()
sanitizedKey:= strings.TrimSpace(key);
if key == ""{
log.Printf("Invalid key : [%s]",key)
}
delete(shard.data,sanitizedKey)
}
func (c *cacheShardMap) evictExpired(){
now:= time.Now()
var wg sync.WaitGroup
for i := 0; i < shardCount; i++ {
wg.Add(1)
go func (shard *cacheShard) {
defer wg.Done()
shard.mu.Lock()
defer shard.mu.Unlock()
for key, item := range shard.data {
// If expiresAt is set and 'now' is past the expiration time:
if !item.expiresAt.IsZero() && now.After(item.expiresAt) {
delete(shard.data, key)
}
}
}(c.shards[i])
}
wg.Wait()
}
// StartEvictionWorker runs a background loop that actively deletes expired keys
func (c *cacheShardMap) StartEvictionWorker(ctx context.Context,interval time.Duration){
ticker:= time.NewTicker(interval)
defer ticker.Stop()
for{
select{
case<-ctx.Done():
return
case <-ticker.C:
c.evictExpired()
}
}
}
func (c *cacheShardMap) Count() int {
total:= 0
for i := 0; i < shardCount; i++ {
c.shards[i].mu.RLock()
total = total + len(c.shards[i].data)
c.shards[i].mu.Unlock()
}
return total
}