mirror of
https://github.com/1f349/cache.git
synced 2024-12-22 08:04:06 +00:00
Don't delete changed items on original expiry
Use compare and delete to ensure item has not changed before deleting it Fixes #1
This commit is contained in:
parent
7e244a930f
commit
112b816e53
2
cache.go
2
cache.go
@ -116,7 +116,7 @@ func (c *Cache[K, V]) cleaner() {
|
|||||||
|
|
||||||
// remove all expired entries
|
// remove all expired entries
|
||||||
for c.chain != nil && c.chain.HasExpired() {
|
for c.chain != nil && c.chain.HasExpired() {
|
||||||
c.items.Delete(c.chain.data)
|
c.items.CompareAndDelete(c.chain.data, c.chain.item)
|
||||||
c.chain = c.chain.next
|
c.chain = c.chain.next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,3 +132,23 @@ func TestCache_Cleaner(t *testing.T) {
|
|||||||
c.sched.Wait()
|
c.sched.Wait()
|
||||||
assert.Nil(t, c.chain)
|
assert.Nil(t, c.chain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCache_UpdateExpiry(t *testing.T) {
|
||||||
|
timeNow = func() time.Time { return time.Now() }
|
||||||
|
|
||||||
|
c := New[string, string]()
|
||||||
|
c.Set("a", "b", time.Now().Add(2*time.Second))
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
get, b := c.Get("a")
|
||||||
|
assert.True(t, b)
|
||||||
|
assert.Equal(t, "b", get)
|
||||||
|
|
||||||
|
c.Set("a", "b", time.Now().Add(5*time.Second))
|
||||||
|
|
||||||
|
// after expiry of the first set call
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
get, b = c.Get("a")
|
||||||
|
assert.True(t, b)
|
||||||
|
assert.Equal(t, "b", get)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user