karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.28k stars 119 forks source link

Add a SecondaryCache which exposes the secondary part of a LayeredCache #8

Closed jdeppe-pivotal closed 7 years ago

jdeppe-pivotal commented 7 years ago

Hi,

Please consider this PR to your project.

I'm working on a caching proxy and would like to use ccache as the LRU cache for in-memory blocks of large files as they are read in and cached to memory and persistent storage.

I'm using the LayeredCache where each URL is mapped to an index of blocks (byte arrays). I'm already tracking the URLs and realized that every time I want to access a particular block, I need to do a double-lookup. If I use this SecondaryCache approach I can associate the SecondaryCache with my URL and then I just need to do a single lookup.

Thanks for your time and project!

karlseguin commented 7 years ago

I'm fine with merging this, but GetOrCreateSecondaryCache isn't thread-safe. It's possible for two thread to execute this at the same time:

if bkt == nil {
  bkt = &bucket{lookup: make(map[string]*Item)}
  primaryBkt.buckets[primary] = bkt
  }

can you fix?

jdeppe-pivotal commented 7 years ago

I think this should do it.