Skip to content

Commit f755e6f

Browse files
committed
debug numbercache stop
1 parent 83bc149 commit f755e6f

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

writer/utils/numbercache/cache.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package numbercache
22

33
import (
4+
"context"
45
"sync"
56
"time"
67

@@ -16,11 +17,12 @@ type ICache[T any] interface {
1617

1718
type Cache[K any] struct {
1819
nodeMap map[string]*model.DataDatabasesMap
19-
cleanup *time.Ticker
2020
sets *fastcache.Cache
2121
mtx *sync.Mutex
2222
db []byte
2323
isDistributed bool
24+
ctx context.Context
25+
cancel context.CancelFunc
2426
serializer func(t K) []byte
2527
}
2628

@@ -39,7 +41,7 @@ func (c *Cache[T]) CheckAndSet(key T) bool {
3941
}
4042

4143
func (c *Cache[T]) Stop() {
42-
c.cleanup.Stop()
44+
c.cancel()
4345
}
4446

4547
func (c *Cache[T]) DB(db string) ICache[T] {
@@ -59,19 +61,26 @@ func NewCache[T comparable](TTL time.Duration, serializer func(val T) []byte,
5961
if serializer == nil {
6062
panic("NO SER")
6163
}
62-
res := Cache[T]{
64+
res := &Cache[T]{
6365
nodeMap: nodeMap,
64-
cleanup: time.NewTicker(TTL),
6566
sets: fastcache.New(100 * 1024 * 1024),
6667
mtx: &sync.Mutex{},
6768
serializer: serializer,
6869
}
70+
res.ctx, res.cancel = context.WithCancel(context.Background())
71+
cleanup := time.NewTicker(TTL)
6972
go func() {
70-
for range res.cleanup.C {
71-
res.mtx.Lock()
72-
res.sets.Reset()
73-
res.mtx.Unlock()
73+
for {
74+
select {
75+
case <-cleanup.C:
76+
res.mtx.Lock()
77+
res.sets.Reset()
78+
res.mtx.Unlock()
79+
case <-res.ctx.Done():
80+
cleanup.Stop()
81+
return
82+
}
7483
}
7584
}()
76-
return &res
85+
return res
7786
}

0 commit comments

Comments
 (0)