Skip to content

Commit c650bd9

Browse files
committed
add cache with replace
1 parent f1e7f04 commit c650bd9

File tree

3 files changed

+113
-22
lines changed

3 files changed

+113
-22
lines changed

cache/cache_simple.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cache
2+
3+
import (
4+
"sync"
5+
)
6+
7+
type (
8+
cacheReplace[K comparable, V any] struct {
9+
list map[K]V
10+
mux sync.RWMutex
11+
}
12+
)
13+
14+
func NewWithReplace[K comparable, V any]() TCacheReplace[K, V] {
15+
return &cacheReplace[K, V]{
16+
list: make(map[K]V, 1000),
17+
}
18+
}
19+
20+
func (v *cacheReplace[K, V]) Has(key K) bool {
21+
v.mux.RLock()
22+
defer v.mux.RUnlock()
23+
24+
_, ok := v.list[key]
25+
26+
return ok
27+
}
28+
29+
func (v *cacheReplace[K, V]) Get(key K) (V, bool) {
30+
v.mux.RLock()
31+
defer v.mux.RUnlock()
32+
33+
item, ok := v.list[key]
34+
if !ok {
35+
var zeroValue V
36+
return zeroValue, false
37+
}
38+
39+
return item, true
40+
}
41+
42+
func (v *cacheReplace[K, V]) Set(key K, value V) {
43+
v.mux.Lock()
44+
defer v.mux.Unlock()
45+
46+
v.list[key] = value
47+
}
48+
49+
func (v *cacheReplace[K, V]) Replace(data map[K]V) {
50+
v.mux.Lock()
51+
defer v.mux.Unlock()
52+
53+
v.list = data
54+
}
55+
56+
func (v *cacheReplace[K, V]) Del(key K) {
57+
v.mux.Lock()
58+
defer v.mux.Unlock()
59+
60+
delete(v.list, key)
61+
}
62+
63+
func (v *cacheReplace[K, V]) Keys() []K {
64+
v.mux.RLock()
65+
defer v.mux.RUnlock()
66+
67+
result := make([]K, 0, len(v.list))
68+
for k := range v.list {
69+
result = append(result, k)
70+
}
71+
72+
return result
73+
}
74+
75+
func (v *cacheReplace[K, V]) Flush() {
76+
v.mux.Lock()
77+
defer v.mux.Unlock()
78+
79+
for k := range v.list {
80+
delete(v.list, k)
81+
}
82+
}

cache/cache_time.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ import (
99
)
1010

1111
type (
12-
withTTL[K comparable, T interface{}] struct {
12+
cacheWithTTL[K comparable, V any] struct {
1313
ttl time.Duration
14-
list map[K]*ttlItem[T]
14+
list map[K]*itemCacheTTL[V]
1515
mux sync.RWMutex
1616
}
1717

18-
ttlItem[T interface{}] struct {
19-
link T
18+
itemCacheTTL[V interface{}] struct {
19+
link V
2020
ts int64
2121
}
2222
)
2323

24-
func NewWithTTL[K comparable, T interface{}](ctx context.Context, ttl time.Duration) TCache[K, T] {
25-
cache := &withTTL[K, T]{
24+
func NewWithTTL[K comparable, V any](ctx context.Context, ttl time.Duration) TCacheTTL[K, V] {
25+
cache := &cacheWithTTL[K, V]{
2626
ttl: ttl,
27-
list: make(map[K]*ttlItem[T], 1000),
27+
list: make(map[K]*itemCacheTTL[V], 1000),
2828
}
2929
go cache.cleaner(ctx)
3030
return cache
3131
}
3232

33-
func (v *withTTL[K, T]) cleaner(ctx context.Context) {
33+
func (v *cacheWithTTL[K, V]) cleaner(ctx context.Context) {
3434
routine.Interval(ctx, v.ttl, func(ctx context.Context) {
3535
curr := time.Now().Unix()
3636

@@ -42,7 +42,7 @@ func (v *withTTL[K, T]) cleaner(ctx context.Context) {
4242
})
4343
}
4444

45-
func (v *withTTL[K, T]) Has(key K) bool {
45+
func (v *cacheWithTTL[K, V]) Has(key K) bool {
4646
v.mux.RLock()
4747
defer v.mux.RUnlock()
4848

@@ -51,47 +51,47 @@ func (v *withTTL[K, T]) Has(key K) bool {
5151
return ok
5252
}
5353

54-
func (v *withTTL[K, T]) Get(key K) (T, bool) {
54+
func (v *cacheWithTTL[K, V]) Get(key K) (V, bool) {
5555
v.mux.RLock()
5656
defer v.mux.RUnlock()
5757

5858
item, ok := v.list[key]
5959
if !ok {
60-
var zeroValue T
60+
var zeroValue V
6161
return zeroValue, false
6262
}
6363

6464
return item.link, true
6565
}
6666

67-
func (v *withTTL[K, T]) Set(key K, value T) {
67+
func (v *cacheWithTTL[K, V]) Set(key K, value V) {
6868
v.mux.Lock()
6969
defer v.mux.Unlock()
7070

71-
v.list[key] = &ttlItem[T]{
71+
v.list[key] = &itemCacheTTL[V]{
7272
link: value,
7373
ts: time.Now().Add(v.ttl).Unix(),
7474
}
7575
}
7676

77-
func (v *withTTL[K, T]) SetWithTTL(key K, value T, ttl time.Time) {
77+
func (v *cacheWithTTL[K, V]) SetWithTTL(key K, value V, ttl time.Time) {
7878
v.mux.Lock()
7979
defer v.mux.Unlock()
8080

81-
v.list[key] = &ttlItem[T]{
81+
v.list[key] = &itemCacheTTL[V]{
8282
link: value,
8383
ts: ttl.Unix(),
8484
}
8585
}
8686

87-
func (v *withTTL[K, T]) Del(key K) {
87+
func (v *cacheWithTTL[K, V]) Del(key K) {
8888
v.mux.Lock()
8989
defer v.mux.Unlock()
9090

9191
delete(v.list, key)
9292
}
9393

94-
func (v *withTTL[K, T]) Keys() []K {
94+
func (v *cacheWithTTL[K, V]) Keys() []K {
9595
v.mux.RLock()
9696
defer v.mux.RUnlock()
9797

@@ -103,7 +103,7 @@ func (v *withTTL[K, T]) Keys() []K {
103103
return result
104104
}
105105

106-
func (v *withTTL[K, T]) Flush() {
106+
func (v *cacheWithTTL[K, V]) Flush() {
107107
v.mux.Lock()
108108
defer v.mux.Unlock()
109109

cache/common.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package cache
22

33
import "time"
44

5-
type TCache[K comparable, T interface{}] interface {
5+
type TCache[K comparable, V interface{}] interface {
66
Has(key K) bool
7-
Get(key K) (T, bool)
8-
Set(key K, value T)
9-
SetWithTTL(key K, value T, ttl time.Time)
7+
Get(key K) (V, bool)
8+
Set(key K, value V)
109
Del(key K)
1110
Keys() []K
1211
Flush()
1312
}
13+
14+
type TCacheTTL[K comparable, V interface{}] interface {
15+
TCache[K, V]
16+
SetWithTTL(key K, value V, ttl time.Time)
17+
}
18+
19+
type TCacheReplace[K comparable, V interface{}] interface {
20+
TCache[K, V]
21+
Replace(data map[K]V)
22+
}

0 commit comments

Comments
 (0)