karlseguin / ccache

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

No exposed method to access size of the cache. #39

Closed Devashish0312 closed 4 years ago

Devashish0312 commented 4 years ago

The size of cache is not an exposed variable, therefore couldn't be accessed. If we could define a method which returns the size, it would be helpful for finding overflow or cache miss.

karlseguin commented 4 years ago

It's because the size field isn't thread-safe. It's currently only accessed from the worker goroutine.

I can think of two solutions: 1 - Synchronize access to size. This would add a bit overhead to the worker. 2 - Use a communication channel with the worker to ask it for stuff (like the size). This would make calling Size() blocking and possibly surprisingly slow. For example, if you call Size(), at the worst case, you'd have to wait for the worker to finish its current gc

Any thoughts?

Devashish0312 commented 4 years ago

Okay i see, but is there anyway to find the eviction rate for unexpired keys in the current implementation?

karlseguin commented 4 years ago

Does https://github.com/karlseguin/ccache/commit/1a257a89d65a1ccad5f2acbb1d88e0b8c7682f24 help?

Essentially it adds a GetDropped() function which returns the number of keys removed due to gc since the lat time GetDropped() was called.

GetDropped waits until the current GC is running, so it's meant to be called asynchronously (from logging / monitoring code, for example)

karlseguin commented 4 years ago

I merged this into master.