CacheTower
CacheTower copied to clipboard
Support for a Counter-caching API
Similar to discussions in dotnet/extensions#709 & stefanprodan/AspNetCoreRateLimit#83, add support for a new counter cache stack. This is a unique cache stack type with atomic actions by design.
public interface ICounterCacheStack<TContext>
{
ValueTask<long> GetAsync(string cacheKey);
ValueTask SetAsync(string cacheKey, long value, CacheSettings settings);
ValueTask<long> IncrementAsync(string cacheKey, CacheSettings settings);
ValueTask<long> DecrementAsync(string cacheKey, CacheSettings settings);
ValueTask<long> IncrementByAsync(string cacheKey, long value, CacheSettings settings);
ValueTask<long> DecrementByAsync(string cacheKey, long value, CacheSettings settings);
}
Providers:
- In-memory: Need to research atomic operations for memory storage
- Redis: INCR, INCRBY, DECR & DECRBY. May need to even go further and use Lua scripts in Redis like this example.
- MongoDB: Should be possible with custom commands though not sure if it is even necessary. MongoDB would likely never perform fast enough to be used in this manner.
Other thoughts:
- Still likely need to support auto-evicting etc, at least for the in-memory version. That is unless this new cache stack type has a custom memory provider different from the main one.
- Do I need to support remote evicting too (provided through my Redis extension)?
- Does this actually need a
TContext
?
Thinking about this a little more, I'd probably go down the path of splitting cache layers into categories like:
- Common (This is what already exists)
- Counters (This is what this issue proposes)
- ... (Anything else that may come up later)
Rather than try and squish everything into a single stack and provider, basically have interfaces and providers for each specific "category". Its not like DB or Disk caching would be a high priority for counters but are of reasonable importance for "common" caching.
This is still conceptually interesting but deviates from the main goal of the library too much in my opinion, making it harder to maintain the library in the future.