Commit 0d500fd 1 parent c6653c7 commit 0d500fd Copy full SHA for 0d500fd
File tree 3 files changed +13
-20
lines changed
3 files changed +13
-20
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,6 @@ type Cache interface {
16
16
setWithExpiry (key string , value []byte , expiry time.Duration ) error
17
17
get (key string ) []byte
18
18
delete (key string ) error
19
- cleanUp ()
20
19
}
21
20
22
21
var conn Cache
@@ -32,7 +31,6 @@ func init() {
32
31
panic ("unknown cache driver" )
33
32
}
34
33
35
- go conn .cleanUp ()
36
34
}
37
35
38
36
// Client ...
@@ -64,11 +62,3 @@ func Get[T any](key string) (T, error) {
64
62
func Delete (key string ) error {
65
63
return conn .delete (key )
66
64
}
67
-
68
- // CleanUp ...
69
- func CleanUp () {
70
- tick := time .NewTicker (time .Second )
71
- for range tick .C {
72
- conn .cleanUp ()
73
- }
74
- }
Original file line number Diff line number Diff line change @@ -17,11 +17,14 @@ type memoryCache struct {
17
17
}
18
18
19
19
func newMemoryCache () Cache {
20
- return & memoryCache {
20
+ c := & memoryCache {
21
21
cacheWorkers : env .Int ("CACHE_TOTAL_WORKERS" , 10 ),
22
22
items : make (map [string ]cacheItem ),
23
23
mut : sync.Mutex {},
24
24
}
25
+
26
+ go c .cleanUp ()
27
+ return c
25
28
}
26
29
27
30
// getClient ...
@@ -77,11 +80,15 @@ func (m *memoryCache) delete(key string) error {
77
80
78
81
// cleanUp deletes expired cache items and calls their callbacks
79
82
func (m * memoryCache ) cleanUp () {
80
- m .mut .Lock ()
81
- defer m .mut .Unlock ()
82
- for key , item := range m .items {
83
- if item .expired () {
84
- delete (m .items , key )
83
+ for {
84
+ m .mut .Lock ()
85
+ for key , item := range m .items {
86
+ if item .expired () {
87
+ delete (m .items , key )
88
+ }
85
89
}
90
+
91
+ m .mut .Unlock ()
92
+ <- time .After (time .Second )
86
93
}
87
94
}
Original file line number Diff line number Diff line change @@ -78,7 +78,3 @@ func (r *redisCache) delete(key string) error {
78
78
79
79
return nil
80
80
}
81
-
82
- func (r * redisCache ) cleanUp () {
83
- // TODO
84
- }
You can’t perform that action at this time.
0 commit comments