Skip to content

Commit 1344919

Browse files
authored
update vendors + fix cache (#9)
1 parent f7dc05b commit 1344919

File tree

6 files changed

+109
-34
lines changed

6 files changed

+109
-34
lines changed

cache/cache.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ func New[K comparable, V any](opts ...Option[K, V]) Cache[K, V] {
2222
}
2323

2424
for _, opt := range opts {
25-
opt(obj)
25+
go opt(obj)
2626
}
2727

2828
return obj
2929
}
3030

31+
func (v *_cache[K, V]) Size() int {
32+
v.mux.RLock()
33+
defer v.mux.RUnlock()
34+
35+
return len(v.list)
36+
}
37+
3138
func (v *_cache[K, V]) Has(key K) bool {
3239
v.mux.RLock()
3340
defer v.mux.RUnlock()

cache/cache_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ type testValue struct {
6262

6363
func (v testValue) Timestamp() int64 { return v.TS }
6464

65-
func TestUnit_AutoClean(t *testing.T) {
65+
func TestUnit_OptTimeClean(t *testing.T) {
6666
ctx, cancel := context.WithCancel(context.Background())
6767
defer cancel()
6868

6969
c := cache.New[string, testValue](
70-
cache.AutoClean[string, testValue](ctx, time.Millisecond*100),
70+
cache.OptTimeClean[string, testValue](ctx, time.Millisecond*100),
7171
)
7272

7373
c.Set("foo", testValue{Val: "bar", TS: time.Now().Add(time.Millisecond * 200).Unix()})
@@ -78,12 +78,30 @@ func TestUnit_AutoClean(t *testing.T) {
7878
casecheck.False(t, c.Has("foo"))
7979
}
8080

81+
func TestUnit_OptCountRandomClean(t *testing.T) {
82+
ctx, cancel := context.WithCancel(context.Background())
83+
defer cancel()
84+
85+
c := cache.New[int, int](
86+
cache.OptCountRandomClean[int, int](ctx, 100, time.Second),
87+
)
88+
89+
for i := 0; i < 200; i++ {
90+
c.Set(i, i)
91+
}
92+
casecheck.Equal(t, 200, c.Size())
93+
94+
time.Sleep(time.Second * 2)
95+
96+
casecheck.Equal(t, 100, c.Size())
97+
}
98+
8199
func Benchmark_New(b *testing.B) {
82100
ctx, cancel := context.WithCancel(context.Background())
83101
defer cancel()
84102

85103
c := cache.New[string, testValue](
86-
cache.AutoClean[string, testValue](ctx, time.Millisecond*100),
104+
cache.OptTimeClean[string, testValue](ctx, time.Millisecond*100),
87105
)
88106

89107
b.ReportAllocs()

cache/options.go

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,79 @@ import (
1212
"go.osspkg.com/routine"
1313
)
1414

15-
func AutoClean[K comparable, V Timestamp](ctx context.Context, interval time.Duration) Option[K, V] {
15+
func OptTimeClean[K comparable, V Timestamp](ctx context.Context, interval time.Duration) Option[K, V] {
1616
return func(v *_cache[K, V]) {
17-
routine.Interval(ctx, interval, func(ctx context.Context) {
18-
curr := time.Now().Unix()
19-
keys := make([]K, 0, 10)
20-
21-
v.mux.RLock()
22-
for key, value := range v.list {
23-
if value.Timestamp() < curr {
24-
keys = append(keys, key)
25-
}
26-
}
27-
v.mux.RUnlock()
28-
29-
if len(keys) == 0 {
30-
return
31-
}
32-
33-
v.mux.Lock()
34-
defer v.mux.Unlock()
35-
36-
for _, key := range keys {
37-
delete(v.list, key)
38-
}
39-
})
17+
18+
tik := routine.Ticker{
19+
Interval: interval,
20+
OnStart: false,
21+
Calls: []routine.TickFunc{
22+
func(ctx context.Context, t time.Time) {
23+
curr := t.Unix()
24+
keys := make([]K, 0, 10)
25+
26+
v.mux.RLock()
27+
for key, value := range v.list {
28+
if value.Timestamp() < curr {
29+
keys = append(keys, key)
30+
}
31+
}
32+
v.mux.RUnlock()
33+
34+
if len(keys) == 0 {
35+
return
36+
}
37+
38+
v.mux.Lock()
39+
defer v.mux.Unlock()
40+
41+
for _, key := range keys {
42+
delete(v.list, key)
43+
}
44+
},
45+
},
46+
}
47+
48+
tik.Run(ctx)
49+
}
50+
}
51+
52+
func OptCountRandomClean[K comparable, V any](ctx context.Context, maxCount int, interval time.Duration) Option[K, V] {
53+
return func(v *_cache[K, V]) {
54+
55+
if maxCount < 0 {
56+
panic("OptCountRandomClean: maxCount < 0")
57+
}
58+
59+
tik := routine.Ticker{
60+
Interval: interval,
61+
OnStart: false,
62+
Calls: []routine.TickFunc{
63+
func(ctx context.Context, _ time.Time) {
64+
65+
v.mux.RLock()
66+
removeCount := len(v.list) - maxCount
67+
v.mux.RUnlock()
68+
69+
if removeCount <= 0 {
70+
return
71+
}
72+
73+
v.mux.Lock()
74+
defer v.mux.Unlock()
75+
76+
for key := range v.list {
77+
if removeCount <= 0 {
78+
return
79+
}
80+
81+
delete(v.list, key)
82+
removeCount--
83+
}
84+
},
85+
},
86+
}
87+
88+
tik.Run(ctx)
4089
}
4190
}

cache/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Cache[K comparable, V any] interface {
1313
Replace(data map[K]V)
1414
Del(key K)
1515
Keys() []K
16+
Size() int
1617
Flush()
1718
}
1819

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/BurntSushi/toml v1.5.0
77
go.osspkg.com/casecheck v0.3.0
88
go.osspkg.com/errors v0.3.1
9-
go.osspkg.com/routine v0.3.1
10-
go.osspkg.com/syncing v0.3.1
9+
go.osspkg.com/routine v0.4.0
10+
go.osspkg.com/syncing v0.4.0
1111
gopkg.in/yaml.v3 v3.0.1
1212
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ go.osspkg.com/casecheck v0.3.0 h1:x15blEszElbrHrEH5H02JIIhGIg/lGZzIt1kQlD3pwM=
44
go.osspkg.com/casecheck v0.3.0/go.mod h1:TRFXDMFJEOtnlp3ET2Hix3osbxwPWhvaiT/HfD3+gBA=
55
go.osspkg.com/errors v0.3.1 h1:F9m/EEd/Ot2jba/TV7tvVRIpWXzIpNLc7vRJKcBD86A=
66
go.osspkg.com/errors v0.3.1/go.mod h1:dKXe6Rt07nzY7OyKQNZ8HGBicZ2uQ5TKEoVFnVFOK44=
7-
go.osspkg.com/routine v0.3.1 h1:R0o4P0Ml5eoeHc2DiHjRvHBo/XXrW5nJNqIj3ToRzjg=
8-
go.osspkg.com/routine v0.3.1/go.mod h1:z5AvvTbB19/tt1E5JOb4POhK1tOPgmejajgao/IWn4s=
9-
go.osspkg.com/syncing v0.3.1 h1:zt5o/X5DQ/GE5OQTKkq1nNWJMg7EcYhw0YiwMGuA0f8=
10-
go.osspkg.com/syncing v0.3.1/go.mod h1:Dpe0ljlEG6cI2Y9PxEjKiYEX2sgs1eUjWNVjFu4/iB0=
7+
go.osspkg.com/routine v0.4.0 h1:fEDOI3BTaM/rt5pYT+qPq4gFxvw8WJVY2jMeczB7F9A=
8+
go.osspkg.com/routine v0.4.0/go.mod h1:HUVnPHLFzNCYEGOqUiQWL+av73aFJ6H57XutFxhA+eU=
9+
go.osspkg.com/syncing v0.4.0 h1:9ytMfGHd6Ew69D2n/1syj0FTujNfb1KBiUSZ+KsMTqk=
10+
go.osspkg.com/syncing v0.4.0/go.mod h1:/LBmgCAHFW6nQgVDILpEuo6eRCFK1yyFeNbDs4eVNls=
1111
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1212
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1313
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)