Skip to content

GC will remove incorrect item, espically for multiple reset sall cache case #96

@ceclinux

Description

@ceclinux

GC will remove incorrect items, especially for multiple resets in small cache cases.

Please check the following test case for the expected cache value.Get("1").value should be "900" for each iteration. However, sometimes cache.Get("1") will return nil.

func Test_SmallCacheGetGC(t *testing.T) {
	for i := 0; i < 10; i++ {
		cache := New(Configure[string]().MaxSize(2).ItemsToPrune(1).DeleteBuffer(20).PromoteBuffer(1).GetsPerPromote(2))
		cache.Set("1", "1000", 10*time.Minute)
		cache.Set("2", "1000", 10*time.Minute)
		cache.Set("3", "1000", 10*time.Minute)
		cache.Set("1", "900", 10*time.Minute)

		time.Sleep(100 * time.Millisecond)
		assert.Equal(t, cache.Get("1").value, "900")
	}
}

The reason is the bucket updating of items and list updating of items are not synchronized. Let's say '1 2 3' has already been added to the bucket, and "1" has been updated to "900". However, these updated changes have not been sent to channels. Meanwhile, the worker's goroutine is trying to gc the '1' as the cache size is 2. This can be wrong because calling c.bucket(item.key).delete(item.key) will delete the newly updated entry for "1" in the bucket.

Not sure how to fix this. It would be nice if you give me some idea :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions