Expand description
Cache management for Armature framework.
Provides a unified interface for working with various cache backends including Redis and Memcached, with advanced features like tag-based invalidation and multi-tier caching.
§Features
redis- Enable Redis cache support (enabled by default)memcached- Enable Memcached cache support (requires explicit opt-in)- Tag-based invalidation - Invalidate multiple cache entries by tag
- Multi-tier caching - L1 (in-memory) + L2 (distributed) layers
- Cache decorators -
#[cache]attribute for automatic caching
§Examples
§Redis Cache
use armature_cache::*;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), CacheError> {
let redis_config = CacheConfig::redis("redis://localhost:6379")?;
let redis_cache = RedisCache::new(redis_config).await?;
redis_cache.set_json("key", "value".to_string(), Some(Duration::from_secs(60))).await?;
Ok(())
}§Tag-based Invalidation
use armature_cache::*;
use std::sync::Arc;
let tagged = TaggedCache::new(redis_cache);
// Set with tags
tagged.set_with_tags(
"user:123",
r#"{"name":"Alice"}"#.to_string(),
&["users", "active-users"],
None,
).await?;
// Invalidate all entries with "users" tag
tagged.invalidate_tag("users").await?;§Multi-tier Caching
use armature_cache::*;
use std::sync::Arc;
let l1 = Arc::new(InMemoryCache::new());
let l2 = redis_cache;
let tiered = TieredCache::new(l1, l2);
// Automatically uses L1 (fast) and falls back to L2
tiered.set("key", "value".to_string(), None).await?;
let value = tiered.get("key").await?;§Memcached Cache (requires memcached feature)
ⓘ
use armature_cache::*;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), CacheError> {
let memcached_config = CacheConfig::memcached("memcache://localhost:11211")?;
let memcached_cache = MemcachedCache::new(memcached_config).await?;
memcached_cache.set_json("key", "value".to_string(), Some(Duration::from_secs(60))).await?;
Ok(())
}Re-exports§
pub use config::CacheConfig;pub use error::CacheError;pub use error::CacheResult;pub use invalidation::TaggedCache;pub use manager::CacheManager;pub use tiered::InMemoryCache;pub use tiered::TieredCache;pub use tiered::TieredCacheConfig;pub use traits::CacheStore;pub use redis_cache::RedisCache;pub use helpers::*;
Modules§
- config
- Cache configuration types.
- error
- Error types for cache operations.
- helpers
- Helper functions for common cache operations.
- invalidation
- Tag-based cache invalidation
- manager
- High-level cache manager with convenience methods.
- parallel
- Parallel batch operations for cache stores.
- prelude
- Re-export commonly used types
- redis_
cache - Redis cache implementation.
- tiered
- Multi-tier caching (L1/L2 cache layers)
- traits
- Cache store trait definition.