alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Is there a plan to update the API to list all the keys available in cache? #31

Closed saip106 closed 6 years ago

saip106 commented 6 years ago

We have a need to see what all items are in cache. Is there a plan to support this? Can I send a pull request?

burnchar commented 6 years ago

foreach(var item in MemoryCache.Default) { ... }?

saip106 commented 6 years ago

Thanks for the reply @burnchar. Since we are abstracting MemoryCache using the IAppCache, shouldn't we expose a method on it that does this? I thought accessing MemoryCache directly was not a good idea. What are your thoughts on that?

burnchar commented 6 years ago

A good point, @saip106. I believe this is a matter of opinion. I had written my previous reply just after reading this StackOverflow post which describes how iterating a MemoryCache instance is slow, involved, and not recommended in production code. It seems unlikely that LazyCache could do much to improve the experience other than providing a simple wrapper, and doing so would perhaps encourage usage, which Microsoft suggests avoiding.

My impression is that LazyCache is less about abstracting MemoryCache than itself than about providing a well-tested, elegant use of Lazy on top of MemoryCache. While it is true that no calls are made to MemoryCache directly, cache policies are only lightly abstracted, and the particular cache used is merely a constructor parameter which defaults to MemoryCache.Default. LazyCache could have many features it does not have now, but I rather like its simplicity. The code at present is so small and well-written, I could easily maintain it in my projects if alastairtree chose to move on.

saip106 commented 6 years ago

Thanks @burnchar for the detailed reply. I really appreciate you taking time to explain why you think it's not a good idea to add that to the API. I agree with your reasoning. I am closing this issue.