mirror of
https://github.com/1f349/cache.git
synced 2025-01-10 09:06:28 +00:00
Remove rescheduler dependency
This commit is contained in:
parent
7147708296
commit
cb931c3fa6
6
cache.go
6
cache.go
@ -1,7 +1,6 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mrmelon54/rescheduler"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -19,7 +18,6 @@ func timeUntil(t time.Time) time.Duration { return t.Sub(timeNow()) }
|
|||||||
type Cache[K comparable, V any] struct {
|
type Cache[K comparable, V any] struct {
|
||||||
items sync.Map
|
items sync.Map
|
||||||
chain *keyed[K] // linked list of to-expire keys
|
chain *keyed[K] // linked list of to-expire keys
|
||||||
sched *rescheduler.Rescheduler
|
|
||||||
close chan struct{}
|
close chan struct{}
|
||||||
chainAdd chan keyed[K]
|
chainAdd chan keyed[K]
|
||||||
chainDel chan K
|
chainDel chan K
|
||||||
@ -54,7 +52,7 @@ func New[K comparable, V any]() *Cache[K, V] {
|
|||||||
chainAdd: make(chan keyed[K], 1),
|
chainAdd: make(chan keyed[K], 1),
|
||||||
chainDel: make(chan K, 1),
|
chainDel: make(chan K, 1),
|
||||||
}
|
}
|
||||||
c.sched = rescheduler.NewRescheduler(c.cleaner)
|
go c.cleaner()
|
||||||
return c
|
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}
|
i := &item[V]{data: value, expires: expires}
|
||||||
c.items.Store(key, i)
|
c.items.Store(key, i)
|
||||||
c.chainAdd <- keyed[K]{item: item[K]{data: key, expires: expires}}
|
c.chainAdd <- keyed[K]{item: item[K]{data: key, expires: expires}}
|
||||||
c.sched.Run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes an item from the cache.
|
// Delete removes an item from the cache.
|
||||||
@ -262,7 +259,6 @@ func (c *Cache[K, V]) Delete(key K) {
|
|||||||
|
|
||||||
c.items.Delete(key)
|
c.items.Delete(key)
|
||||||
c.chainDel <- key
|
c.chainDel <- key
|
||||||
c.sched.Run()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Range calls f with every key-value pair, which has not expired, currently
|
// Range calls f with every key-value pair, which has not expired, currently
|
||||||
|
@ -93,7 +93,7 @@ func TestCache_Delete(t *testing.T) {
|
|||||||
c.Delete("a")
|
c.Delete("a")
|
||||||
|
|
||||||
// scheduler should finish after deleting the item
|
// scheduler should finish after deleting the item
|
||||||
c.sched.Wait()
|
time.Sleep(time.Second)
|
||||||
assert.Nil(t, c.chain)
|
assert.Nil(t, c.chain)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ func TestCache_Cleaner(t *testing.T) {
|
|||||||
assert.Equal(t, "", get)
|
assert.Equal(t, "", get)
|
||||||
|
|
||||||
// scheduler should finish after the chain is empty
|
// scheduler should finish after the chain is empty
|
||||||
c.sched.Wait()
|
time.Sleep(time.Second)
|
||||||
assert.Nil(t, c.chain)
|
assert.Nil(t, c.chain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
go.mod
5
go.mod
@ -2,10 +2,7 @@ module github.com/1f349/cache
|
|||||||
|
|
||||||
go 1.21
|
go 1.21
|
||||||
|
|
||||||
require (
|
require github.com/stretchr/testify v1.10.0
|
||||||
github.com/mrmelon54/rescheduler v0.0.3
|
|
||||||
github.com/stretchr/testify v1.10.0
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
2
go.sum
2
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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
Loading…
Reference in New Issue
Block a user