Skip to content

Commit df046cc

Browse files
committed
add cache generic
1 parent f7cbc2f commit df046cc

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

cache/cache_time.go

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"sync"
6+
"time"
7+
8+
"go.osspkg.com/routine"
9+
)
10+
11+
type (
12+
withTTL[K comparable, T interface{}] struct {
13+
ttl time.Duration
14+
list map[K]*ttlItem[T]
15+
mux sync.RWMutex
16+
}
17+
18+
ttlItem[T interface{}] struct {
19+
link T
20+
ts int64
21+
}
22+
)
23+
24+
func NewWithTTL[K comparable, T interface{}](ctx context.Context, ttl time.Duration) TCache[K, T] {
25+
cache := &withTTL[K, T]{
26+
ttl: ttl,
27+
list: make(map[K]*ttlItem[T], 1000),
28+
}
29+
go cache.cleaner(ctx)
30+
return cache
31+
}
32+
33+
func (v *withTTL[K, T]) cleaner(ctx context.Context) {
34+
routine.Interval(ctx, v.ttl, func(ctx context.Context) {
35+
curr := time.Now().Unix()
36+
37+
for k, t := range v.list {
38+
if t.ts < curr {
39+
delete(v.list, k)
40+
}
41+
}
42+
})
43+
}
44+
45+
func (v *withTTL[K, T]) Has(key K) bool {
46+
v.mux.RLock()
47+
defer v.mux.RUnlock()
48+
49+
_, ok := v.list[key]
50+
51+
return ok
52+
}
53+
54+
func (v *withTTL[K, T]) Get(key K) (T, bool) {
55+
v.mux.RLock()
56+
defer v.mux.RUnlock()
57+
58+
item, ok := v.list[key]
59+
if !ok {
60+
return *(new(T)), false
61+
}
62+
63+
return item.link, true
64+
}
65+
66+
func (v *withTTL[K, T]) Set(key K, value T) {
67+
v.mux.Lock()
68+
defer v.mux.Unlock()
69+
70+
v.list[key] = &ttlItem[T]{
71+
link: value,
72+
ts: time.Now().Add(v.ttl).Unix(),
73+
}
74+
}
75+
76+
func (v *withTTL[K, T]) Del(key K) {
77+
v.mux.Lock()
78+
defer v.mux.Unlock()
79+
80+
delete(v.list, key)
81+
}
82+
83+
func (v *withTTL[K, T]) Keys() []K {
84+
v.mux.RLock()
85+
defer v.mux.RUnlock()
86+
87+
result := make([]K, 0, len(v.list))
88+
for k := range v.list {
89+
result = append(result, k)
90+
}
91+
92+
return result
93+
}
94+
95+
func (v *withTTL[K, T]) Flush() {
96+
v.mux.Lock()
97+
defer v.mux.Unlock()
98+
99+
for k := range v.list {
100+
delete(v.list, k)
101+
}
102+
}

cache/cache_time_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"go.osspkg.com/casecheck"
9+
)
10+
11+
func TestUnit_WithTTL_Pointer(t *testing.T) {
12+
type A struct {
13+
Data uint64
14+
}
15+
16+
ctx, _ := context.WithTimeout(context.TODO(), time.Second)
17+
c := NewWithTTL[string, *A](ctx, time.Minute)
18+
v, ok := c.Get("a")
19+
casecheck.False(t, ok)
20+
casecheck.Nil(t, v)
21+
}

cache/common.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cache
2+
3+
type TCache[K comparable, T interface{}] interface {
4+
Has(key K) bool
5+
Get(key K) (T, bool)
6+
Set(key K, value T)
7+
Del(key K)
8+
Keys() []K
9+
Flush()
10+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.21
55
require (
66
go.osspkg.com/casecheck v0.3.0
77
go.osspkg.com/errors v0.3.1
8+
go.osspkg.com/routine v0.3.1
89
go.osspkg.com/syncing v0.3.0
910
gopkg.in/yaml.v3 v3.0.1
1011
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ go.osspkg.com/casecheck v0.3.0 h1:x15blEszElbrHrEH5H02JIIhGIg/lGZzIt1kQlD3pwM=
22
go.osspkg.com/casecheck v0.3.0/go.mod h1:TRFXDMFJEOtnlp3ET2Hix3osbxwPWhvaiT/HfD3+gBA=
33
go.osspkg.com/errors v0.3.1 h1:F9m/EEd/Ot2jba/TV7tvVRIpWXzIpNLc7vRJKcBD86A=
44
go.osspkg.com/errors v0.3.1/go.mod h1:dKXe6Rt07nzY7OyKQNZ8HGBicZ2uQ5TKEoVFnVFOK44=
5+
go.osspkg.com/routine v0.3.1 h1:R0o4P0Ml5eoeHc2DiHjRvHBo/XXrW5nJNqIj3ToRzjg=
6+
go.osspkg.com/routine v0.3.1/go.mod h1:z5AvvTbB19/tt1E5JOb4POhK1tOPgmejajgao/IWn4s=
57
go.osspkg.com/syncing v0.3.0 h1:yBkCsDPEt12a+qagInFFt7+ZongfT+GjSQl7nBmcybI=
68
go.osspkg.com/syncing v0.3.0/go.mod h1:Dpe0ljlEG6cI2Y9PxEjKiYEX2sgs1eUjWNVjFu4/iB0=
79
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

0 commit comments

Comments
 (0)