4
0
mirror of https://github.com/1f349/cache.git synced 2025-01-09 16:46:28 +00:00

Remove rescheduler dependency

This commit is contained in:
Melon 2024-12-19 23:23:20 +00:00
parent 7147708296
commit cb931c3fa6
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 4 additions and 13 deletions

View File

@ -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

View File

@ -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)
}

5
go.mod
View File

@ -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

2
go.sum
View File

@ -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=