hit-box / hitbox

A high-performance caching framework suitable for single-machine and for distributed applications in Rust
MIT License
73 stars 6 forks source link

Add defaut non-distributed cache lock implementation. #19

Open singulared opened 3 years ago

singulared commented 3 years ago

Cache locks are the main protection from the dogpile effect. As main functionality (not related to some backend implementation) I suppose next simple structure.

Use something like a HasMap<&str, AsyncRwLock<CachedValue>> where the key is a cache key of the current request and AsyncRWLock is an async version of classic RwLock (or some light pub-sub mechanic like tokio watch or one-shot channels).

How it should work:

  1. First request with cache key A to Upstream will create a record in a Hash table. and send a request to Upstream
  2. Second and next requests to Upstream with cache key A will check records on the hash table and subscribe to value changes (or asynchronous wait for changes)
  3. After first request will be resolved in Upstream cache should publish result to all consumers/waiters and remove record from HashMap

We should extend Backend trait with cache lock related methods with a default implementation. I think this implementation should integrate with the stale cache mechanic in case of returns stale data while the lock not released (if the stale cache is enabled).