CacheTower icon indicating copy to clipboard operation
CacheTower copied to clipboard

Support for a Counter-caching API

Open Turnerj opened this issue 4 years ago • 1 comments

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?

Turnerj avatar Mar 26 '20 05:03 Turnerj

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.

Turnerj avatar May 12 '20 08:05 Turnerj

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.

Turnerj avatar Oct 29 '22 06:10 Turnerj