Crate quick_cache

source ·
Expand description

Lightweight, high performance concurrent cache. It allows very fast access to the cached items with little overhead compared to a plain concurrent hash table. No allocations are ever performed unless the cache internal state table needs growing (which will eventually stabilize).

§Eviction policy

The current eviction policy is a modified version of the Clock-PRO algorithm, very similar to the later published S3-FIFO algorithm. It’s “scan resistent” and provides high hit rates, significantly better than a LRU eviction policy and comparable to other state-of-the art algorithms like W-TinyLFU.

§Thread safety and Concurrency

Both sync (thread-safe) and unsync (non thread-safe) implementations are provided. The latter offers slightly better performance when thread safety is not required.

§Equivalent keys

The cache uses the Equivalent trait for gets/removals. It can helps work around the Borrow limitations. For example, if the cache key is a tuple (K, Q), you wouldn’t be access to access such keys without building a &(K, Q) and thus potentially cloning K and/or Q.

§User defined weight

By implementing the Weighter trait the user can define different weights for each cache entry.

§Atomic operations

By using the get_or_insert or get_value_or_guard family of functions (both sync and async variants are available, they can be mix and matched) the user can coordinate the insertion of entries, so only one value is “computed” and inserted after a cache miss.

§Lifecycle hooks

A user can optionally provide a custom Lifecycle implementation to hook into the lifecycle of cache entries.

Example use cases:

  • send evicted items to a channel, achieving the equivalent to an eviction listener feature.
  • zero out item weights so they are left in the cache instead of evicted.

§Hasher

By default the crate uses ahash, which is enabled (by default) via a crate feature with the same name. If the ahash feature is disabled the crate defaults to the std lib implementation instead (currently Siphash13). Note that a custom hasher can also be provided if desirable.

§Synchronization primitives

By default the crate uses parking_lot, which is enabled (by default) via a crate feature with the same name. If the parking_lot feature is disabled the crate defaults to the std lib implementation instead.

Modules§

  • Concurrent cache variants that can be used from multiple threads.
  • Non-concurrent cache variants.

Structs§

Traits§

Type Aliases§