- Simple API
- Flexible cache sizing, limiting, and eviction
- Multiple cache strategies for different use cases
- Support for cache compression
- Best-effort cache recovery
Bincache uses a strategy pattern to allow for different caching strategies:
- In-memory cache
- Disk-backed cache
- Hybrid cache (in-memory + disk-backed)
- Custom strategies possible through
CacheStrategy
Bincache supports various compression algorithms:
- Gzip (feature:
comp_gzip) - Brotli (feature:
comp_brotli) - Zstandard (feature:
comp_zstd) - Custom algorithms possible through
CompressionStrategy
Bincache supports multiple async runtimes:
- Futures-compatible (default)
- Tokio 1.x (feature:
rt_tokio_1) - Async-Std 1.x (feature:
rt_async-std_1)
-
Add
bincacheto your project:cargo add bincache # use blocking I/O cargo add bincache --features rt_tokio_1 # enable tokio 1.x support cargo add bincache --features rt_async-std_1 # enable async-std 1.x support
-
Create a
Cacheinstance with your preferred strategy:#[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box<dyn std::error::Error>> { let mut cache = bincache::MemoryCacheBuilder::default().build().await?; // Put a key-value pair into the cache cache.put(&"foo", b"foo".to_vec()).await?; // Read the value back out let foo = cache.get(&"foo").await?; // Make sure it's the same assert_eq!(foo, b"foo".as_slice()); Ok(()) }
-
That's it!
blocking- Enables blocking stdlib I/Ort_tokio_1- Enables tokio 1.x supportrt_async-std_1- Enables async-std 1.x supportcomp_zstd- Enables zstd compression supportcomp_gzip- Enables gzip compression supportcomp_brotli- Enabled brotli compression support
By default, we enable a "soft"
implicit-blockingfeature, which only uses blocking I/O if no other runtime feature is enabled.You can explicitly opt-in to blocking I/O by enabling the
blockingfeature, which will disallow the use ofrt_tokio_1andrt_async-std_1.