karlseguin / ccache

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

get all keys in cache #34

Closed primalmotion closed 4 years ago

primalmotion commented 4 years ago

Hi,

Is there any way to get all keys the cache contains? I need to do some forced cache eviction based on a prefix of the key, and I'm having a hard time making it work by using another structure to keep track of the keys. Just being able to list all registered keys from ccache would make my life much easier.

Thanks for the nice piece of software btw

primalmotion commented 4 years ago

the PR #35 would actually also make my life manageable :)

karlseguin commented 4 years ago

LayeredCache isn't otherwise convenient? (I could imagine a lot of cases where it isn't, just double checking).

If you specifically want a prefix delete, I think I'd like to add a DeletePrefix() method since we could do that a lot more efficiently (don't have to collect all the keys). Thoughts?

primalmotion commented 4 years ago

Hi,

I've looked at LayeredCache, but I'm sure how it could help me in that particular case, but maybe I'm missing something

Adding DeletePrefix() would be absolutely wonderful

karlseguin commented 4 years ago

I should have looked more into this before commenting. Only way to avoid collection is to hold a long-lived write lock on the bucket.

What do you think of this implementation: https://github.com/karlseguin/ccache/commit/356e164dd58b46ac277540b8b14b9936ec3d64e2

I went for a shorter-lived write lock in exchange for more memory (collecting) and cpu (double iteration).

Doesn't support the onDelete callback right now, not sure if it should or not.

primalmotion commented 4 years ago

woh! that was fast :) For my very use case your implementation seems fine since I don't have millions of items and I prefer sucking a bit more on cpu rather than longer locking.

Let me give it a try on our product

karlseguin commented 4 years ago

I'll polish it up (documentation + merge) in the next couple days

primalmotion commented 4 years ago

Would it be possible to make that api DeletePrefixes(key ...string)? If i need to delete multiple prefixes, we would collect all the keys at once

primalmotion commented 4 years ago

I believe I can close my pr since this is a way more elegant way to achieve what we need. What do you think?

karlseguin commented 4 years ago

Sounds good :)

primalmotion commented 4 years ago

I just tried this patch in our app it works perfectly and I removed half of my code. I like that :)