Add setable buffer to handle sets in order to allow a hard max size limit

Previously, items were pushed onto the frequency linked list via the promotable
buffer. As a general rule, you want your protobable buffer to be quite large,
since you don't want to block Gets. But because Set uses the same buffer, the
cache could grow to MaxSize + cap(promotables).

Sets are now "promoted" via a new "setables" buffer. These are handled exactly
the same way as before, but having it be a separate buffer means they can have
different capacity. Thus, using the new `SetableBuffer(int)` configuration
method can help set a hard limit on the maximum size.
This commit is contained in:
Karl Seguin
2023-03-08 12:44:20 +08:00
parent 35052434f3
commit 55899506d5
7 changed files with 43 additions and 12 deletions

View File

@@ -361,6 +361,20 @@ func Test_ConcurrentStop(t *testing.T) {
}
}
func Test_UnbufferedSetable_Enforces_MaxSize(t *testing.T) {
cache := New(Configure[string]().MaxSize(3).SetableBuffer(0).ItemsToPrune(1))
cache.Set("a", "1", time.Minute)
cache.Set("b", "2", time.Minute)
cache.Set("c", "3", time.Minute)
cache.Set("d", "4", time.Minute)
cache.Set("e", "5", time.Minute)
assert.Nil(t, cache.Get("a"))
// "b" could or could not be purged
assert.Equal(t, cache.Get("c").Value(), "3")
assert.Equal(t, cache.Get("d").Value(), "4")
assert.Equal(t, cache.Get("e").Value(), "5")
}
type SizedItem struct {
id int
s int64