bluele / gcache

An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Len() function of Cache interface unnecessarily (presumably) calls GetALL

kogan69 opened this issue · comments

Apologies upfront for a possible misunderstanding of some fundamental concept, but I just couldn't figure out why not to return len(items) instead?
Or in other words, why to create a full copy of the cache when the only thing that is required to know is it's size?

Len (items) will return items that have all item(include expired item).
This is inconvenient especially when setting the expiration date, so we used the size of the result of GetALL.
However, this is unnecessarily expensive if you do not set an expiration date, so we might as well prepare methods separately.

The Len function on the LRU cache has an unfortunate side effect because of how GetALL works: Because it calls GetIfPresent for each key in the cache via a range on items each item in the cache will be randomly reordered in the eviction list because of the randomness of ranging over a map.