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

Feature Request: PurgeExpire()

sean- opened this issue · comments

The current behavior of Purge() is such that the EvictedFunc() is not called when items are removed from the cache (the cached items lists are just replaced and handed off to the GC). Without a higher-level locking primitive, there is not a way to purge a cache and call the evict handler.

It would be nice if the interface was updated to include PurgeExpire() that would take c.mu.Lock() and, if evictedFunc() is not nil, call GetALL() + evictedFunc() on each of the items while c.mu held and before c.init() is called.

Or Purge() could be updated to implicitly call evictFunc() on each item. Without this change to make Purge() symmetric w/ calls to LoaderFunc(), it is difficult to use this cache for finite resources (e.g. file descriptors). Resorting to finalizers used in the LoaderFunc() feels very distasteful and like a hack.

I'm happy to PR this change but want to inquire re: the API first before submitting a PR.