Refactor control messages + Stop handling

Move the control API shared between Cache and LayeredCache into its own struct.
But keep the control logic handling separate - it requires access to the local
values, like dropped and deleteItem.

Stop is now a control message. Channels are no longer closed as part of the stop
process.
This commit is contained in:
Karl Seguin
2023-01-04 10:40:19 +08:00
parent ece93bf87d
commit 22776be1ee
5 changed files with 220 additions and 197 deletions

View File

@@ -337,6 +337,30 @@ func Test_CachePrune(t *testing.T) {
}
}
func Test_ConcurrentStop(t *testing.T) {
for i := 0; i < 100; i++ {
cache := New(Configure[string]())
r := func() {
for {
key := strconv.Itoa(int(rand.Int31n(100)))
switch rand.Int31n(3) {
case 0:
cache.Get(key)
case 1:
cache.Set(key, key, time.Minute)
case 2:
cache.Delete(key)
}
}
}
go r()
go r()
go r()
time.Sleep(time.Millisecond * 10)
cache.Stop()
}
}
type SizedItem struct {
id int
s int64