cache-kit
A type-safe, generic caching framework for Rust.
A trait-based caching framework that works with any Rust type, any backend (InMemory, Redis, Memcached), and any database (SQLx, Diesel, tokio-postgres).
Features
- Generic — Cache any type
Tthat implementsCacheEntity - Backend Agnostic — Switch between InMemory, Redis, Memcached without code changes
- Database Agnostic — Works with any repository implementing
DataRepository - Type Safe — Compile-time verified
- Thread Safe —
Send + Syncguarantees
⚠️ Requirements
- Requires
tokioruntime (async-only) - Use Redis/Memcached for production (InMemory is single-instance only)
- Decimal types (
rust_decimal::Decimal) require custom serialization
For production guidance, see the documentation.
Quick Start
Add to Cargo.toml
[]
= { = "0.9", = ["inmemory"] }
= { = "1.0", = ["derive"] }
= { = "1", = ["full"] }
Basic Usage
use ;
use ;
// 1. Define your entity
// 2. Implement CacheEntity
// 3. Create a feeder
// 4. Create a repository
;
// 5. Use the cache
async
Cache Strategies
| Strategy | Behavior | Use Case |
|---|---|---|
| Refresh (default) | Try cache, fallback to DB | Most common case |
| Fresh | Cache only, no DB fallback | When data must be cached |
| Invalidate | Clear cache, fetch from DB | After mutations |
| Bypass | Skip cache, always use DB | Testing or temporary disable |
Advanced Usage
For complex operations like custom TTL overrides, retry logic, and builder patterns, see the documentation for advanced features including metrics, TTL policies, and CacheService for service-oriented architectures.
Backends
In-Memory (Default)
use InMemoryBackend;
let backend = new;
let expander = new;
Redis
[]
= { = "0.9", = ["redis"] }
use ;
let backend = new?;
let expander = new;
Memcached
[]
= { = "0.9", = ["memcached"] }
use ;
let backend = new?;
let expander = new;
Observability
The framework supports logging (via log crate) and custom metrics:
use ;
use Duration;
// Custom metrics
let expander = new
.with_metrics;
// TTL policies
let expander = new
.with_ttl_policy;
See the documentation for details on metrics and TTL policies.
Examples
# Basic usage
# Multiple backends
# Advanced builder
Testing
For more examples and guides, see the documentation.
Publishing
This runs build, test, audit, and publish. For linting, use make dev.
Contributing
Contributions welcome! See CONTRIBUTING.md for guidelines.
License
MIT License - Copyright (c) 2025 Kishore kumar Neelamegam
See LICENSE file for details.
Support
Maintainer: Kishore kumar Neelamegam Repository: github.com/megamsys/cache-kit.rs Issues: GitHub Issues Crates.io: crates.io/crates/cache-kit Documentation: cachekit.org