hitbox-backend
Backend abstraction layer for the Hitbox caching framework.
This crate provides the core traits and utilities for implementing cache backends. It defines how cached data is stored, retrieved, serialized, and compressed.
Overview
The crate is organized around several key concepts:
- [
Backend] - Low-level dyn-compatible trait for raw byte storage operations (read/write/remove) - [
CacheBackend] - High-level trait with typed operations that handle serialization Format- Serialization format abstraction (JSON, Bincode, RON, Rkyv)- [
Compressor] - Compression abstraction (Passthrough, Gzip, Zstd) - [
CompositionBackend] - Multi-tier caching (L1/L2)
Implementing a Backend
To implement your own backend, implement the [Backend] trait:
use HashMap;
use RwLock;
use ;
use ;
use async_trait;
Once you implement [Backend], you get [CacheBackend] for free via blanket implementation.
This provides typed get, set, and delete operations with automatic serialization.
Feature Flags
gzip- Enable Gzip compression viaGzipCompressorzstd- Enable Zstd compression viaZstdCompressormetrics- Enable observability metrics for backend operationsrkyv_format- Enable zero-copy Rkyv serialization viaRkyvFormat
Serialization Formats
| Format | Speed | Size | Human-readable |
|---|---|---|---|
BincodeFormat |
Fast | Compact | No |
JsonFormat |
Slow | Large | Partial* |
RonFormat |
Medium | Medium | Yes |
RkyvFormat |
Fastest | Compact | No |
* JSON serializes binary data as byte arrays [104, 101, ...], not readable strings.
Compression Options
| Compressor | Ratio | Speed |
|---|---|---|
[PassthroughCompressor] |
None | Fastest |
GzipCompressor |
Good | Medium |
ZstdCompressor |
Best | Fast |
Multi-Tier Caching
Use [CompositionBackend] to combine backends into
L1/L2/L3 hierarchies:
use Compose;
// Fast local cache (L1) with distributed cache fallback (L2)
let backend = moka.compose;
See the [composition] module for details on read/write/refill policies.