diff --git a/cache.go b/cache.go index ab11cb4..59fb830 100644 --- a/cache.go +++ b/cache.go @@ -1,7 +1,6 @@ package cache import ( - "github.com/mrmelon54/rescheduler" "sync" "time" ) @@ -19,7 +18,6 @@ func timeUntil(t time.Time) time.Duration { return t.Sub(timeNow()) } type Cache[K comparable, V any] struct { items sync.Map chain *keyed[K] // linked list of to-expire keys - sched *rescheduler.Rescheduler close chan struct{} chainAdd chan keyed[K] chainDel chan K @@ -54,7 +52,7 @@ func New[K comparable, V any]() *Cache[K, V] { chainAdd: make(chan keyed[K], 1), chainDel: make(chan K, 1), } - c.sched = rescheduler.NewRescheduler(c.cleaner) + go c.cleaner() return c } @@ -248,7 +246,6 @@ func (c *Cache[K, V]) Set(key K, value V, expires time.Time) { i := &item[V]{data: value, expires: expires} c.items.Store(key, i) c.chainAdd <- keyed[K]{item: item[K]{data: key, expires: expires}} - c.sched.Run() } // Delete removes an item from the cache. @@ -262,7 +259,6 @@ func (c *Cache[K, V]) Delete(key K) { c.items.Delete(key) c.chainDel <- key - c.sched.Run() } // Range calls f with every key-value pair, which has not expired, currently diff --git a/cache_test.go b/cache_test.go index 0d8ee23..910941b 100644 --- a/cache_test.go +++ b/cache_test.go @@ -93,7 +93,7 @@ func TestCache_Delete(t *testing.T) { c.Delete("a") // scheduler should finish after deleting the item - c.sched.Wait() + time.Sleep(time.Second) assert.Nil(t, c.chain) } @@ -129,7 +129,7 @@ func TestCache_Cleaner(t *testing.T) { assert.Equal(t, "", get) // scheduler should finish after the chain is empty - c.sched.Wait() + time.Sleep(time.Second) assert.Nil(t, c.chain) } diff --git a/go.mod b/go.mod index 037c87c..7c16e3d 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,7 @@ module github.com/1f349/cache go 1.21 -require ( - github.com/mrmelon54/rescheduler v0.0.3 - github.com/stretchr/testify v1.10.0 -) +require github.com/stretchr/testify v1.10.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index ee39547..713a0b4 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/mrmelon54/rescheduler v0.0.3 h1:TrkJL6S7PKvXuo1mvdgRgsILA/pk5L1lrXhV/q7IEzQ= -github.com/mrmelon54/rescheduler v0.0.3/go.mod h1:q415n6W1xcePPP5Rix6FOiADgcN66BYMyNOsFnNyoWQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=