-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.go
43 lines (37 loc) · 751 Bytes
/
shard.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
package memcache
import "time"
type shard[K Stringer, V any] struct {
hashmap map[K]V
// cache *cache // use if neccessary
}
func (s *shard[K Stringer, V any]) set(k K, v V, d time.Duration) {
s.hashmap[k] = item{
value: v,
expiredAt: time.Now().Add(d),
}
}
func (s *shard[K Stringer, V any]) get(k K) V {
item, ok := s.hashmap[k]
if !ok {
return nil
}
if item.expiredAt.Before(time.Now()) {
delete(s.hashmap, k)
return nil
}
return item.value
}
func (s *shard[K Stringer, V any]) take(k K) V {
item, ok := s.hashmap[k]
if !ok {
return nil
}
delete(s.hashmap, k)
if item.expiredAt.Before(time.Now()) {
return nil
}
return item.value
}
func (s *shard[K Stringer, V any]) delete(k K) {
delete(s.hashmap, k)
}