-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to get a list / iterate over all items in the cache #16
Comments
Hi and thank you for interest in the library. Listing all items or providing enumerator was intentionally not done for the following reasons:
For bulk operations, you can instead use If that's not what you need, do let me know which use case you have in mind and I will look into implementing it if it's viable to provide simple API for that. |
Thanks for replying. Your library is actually very useful for one of my projects. I'm currently adding items to it using:
Basically, I'm just counting how many calls are being made to a certain function with a certain key in the past minute since first one. Anyway, since this is a Web Api endpoint which can get many different keys which I don't know what they will be ahead of time, I wanted a way to know at any given point at least how many are currently in the cache. It perhaps isn't necessary to iterate over them all although that would be nice Thanks for your work |
@theadin Addressed in https://github.com/neon-sunset/fast-cache/releases/tag/1.6.0 (also fixed publishing XML documentation which should make using the lib. easier) The usage for new API is as following: using FastCache.Services;
// Returns only non-expired
foreach (var cached in CacheManager.EnumerateEntries<string, int>())
{
// Do stuff
} using FastCache.Services;
var totalCount = CacheManager.TotalCount<string, int>(); // includes expired Small comment regarding Exposing As for your particular use case, I would like to note that such counting logic is non-atomic because it reads last cached Therefore, you may want to use internal class Counter
{
private uint _value = 1;
public int Value => _value;
public void Increment() => Interlocked.Increment(ref _value);
} if (Cached<Counter>.TryGet(a, b, c, out var cached)) {
cached.Value.Increment();
// some logic...
} else {
cached.Save(new Counter(), TimeSpan.FromMinutes(1));
} This way we ensure atomicity and precision of the counter at the cost of having to allocate a separate "holder" object for it. And the last note - the library exposes methods for "multi-argument keys" which means you don't have to concat a string like For example, if variables a, b and c are |
Thanks. I'm sure anyone who uses this library will appreciate the added functionally :) You are right. I should be using Interlocked.Increment. And thanks for pointing out "multi-argument keys" I did see this option in prior to using the library but was updating existing code so I did use it but it is a good idea. If It is good you are putting speed at the top of your priority 👍 |
The key itself must not be null (hence Also thanks, I'm happy this library was useful to you :) |
Excellent work |
Hi,
Is there a way to get a list /iterate over all items in the cache?
Cheers
The text was updated successfully, but these errors were encountered: