When setting a new key, Cache would use c.promote to add new item in c.list. But when c.promotables is full, c.promote would do nothing, which means new item would not be added into c.list.
That would cause item leak until a get operation when c.promotables is not full. If there is no operation about this key in the future before c.promote successfully takes effect, the memory of the item would never be released because of the reference from the map.
// https://github.com/karlseguin/ccache/blob/master/cache.go
// Set the value in the cache for the specified duration
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
c.set(key, value, duration, false)
}
func (c *Cache) set(key string, value interface{}, duration time.Duration, track bool) *Item {
item, existing := c.bucket(key).set(key, value, duration, track)
if existing != nil {
c.deletables <- existing
}
c.promote(item)
return item
}
func (c *Cache) promote(item *Item) {
select {
case c.promotables <- item:
default:
}
}
When setting a new key, Cache would use
c.promote
to add new item inc.list
. But whenc.promotables
is full,c.promote
would do nothing, which means new item would not be added intoc.list
. That would cause item leak until a get operation whenc.promotables
is not full. If there is no operation about this key in the future beforec.promote
successfully takes effect, the memory of the item would never be released because of the reference from the map.