karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.27k stars 118 forks source link

Add DeleteFunc #45

Closed bep closed 3 years ago

bep commented 3 years ago

This shares DeletePrefixs's implementation.

A little on "why I need this":

I'm in the process of consolidating Hugo's memory caching using this library. Primarily to get better control over memory usage, but I also need to reimplement (and if possible, improve) the cache eviction logic when something changes on the file system.

I started with some elaborate "prefix matching", but I soon realize that the cache items have a much easier way of handling this dependency/change tracking.

karlseguin commented 3 years ago

Reading your code made me realize there's a bug in the initial implementation. I don't think i can take len(lookup) outside of a lock.

https://github.com/karlseguin/ccache/commit/d7846ec7e013d40f8d67424799d7212837d8d653

Or do you think this is getting crazy and just putting it all under a single read-lock is fine? Instead of the above change, something like:

lookup := b.lookup
b.RLock()
items := make([]*Item, 0, len(lookup)/10)

for key, item := range lookup {
  ...
}
b.RUnlock()
bep commented 3 years ago

@karlseguin yes it looks like a potential data race, but at least I'm using these "delete methods" in a single thread. That said, I would just do:

items := make([]*Item, 0)

Saving a few allocations in this method is hardly worth it ...