Conditionally prune more than itemsToPrune items

It's possible, though unlikely, that c.size will be larger than
c.maxSize by more than c.itemsToPrune. The most likely case that this
can happen is when using SetMaxSize to dynamically adjust the cache
size. The gc will now always clear to at least c.maxSize.
This commit is contained in:
Karl Seguin
2021-03-18 19:29:04 +08:00
parent f28a7755a1
commit df2d98315c
7 changed files with 24 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ func Test_Cache(t *testing.T) {
func (_ CacheTests) DeletesAValue() {
cache := New(Configure())
defer cache.Stop()
Expect(cache.ItemCount()).To.Equal(0)
cache.Set("spice", "flow", time.Minute)
@@ -32,6 +33,7 @@ func (_ CacheTests) DeletesAValue() {
func (_ CacheTests) DeletesAPrefix() {
cache := New(Configure())
defer cache.Stop()
Expect(cache.ItemCount()).To.Equal(0)
cache.Set("aaa", "1", time.Minute)
@@ -55,6 +57,7 @@ func (_ CacheTests) DeletesAPrefix() {
func (_ CacheTests) DeletesAFunc() {
cache := New(Configure())
defer cache.Stop()
Expect(cache.ItemCount()).To.Equal(0)
cache.Set("a", 1, time.Minute)