Skip to main content

armature_cache/
lib.rs

1//! Cache management for Armature framework.
2//!
3//! Provides a unified interface for working with various cache backends
4//! including Redis and Memcached, with advanced features like tag-based
5//! invalidation and multi-tier caching.
6//!
7//! # Features
8//!
9//! - `redis` - Enable Redis cache support (enabled by default)
10//! - `memcached` - Enable Memcached cache support (requires explicit opt-in)
11//! - **Tag-based invalidation** - Invalidate multiple cache entries by tag
12//! - **Multi-tier caching** - L1 (in-memory) + L2 (distributed) layers
13//! - **Cache decorators** - `#[cache]` attribute for automatic caching
14//!
15//! # Examples
16//!
17//! ## Redis Cache
18//!
19//! ```no_run
20//! use armature_cache::*;
21//! use std::time::Duration;
22//!
23//! #[tokio::main]
24//! async fn main() -> Result<(), CacheError> {
25//!     let redis_config = CacheConfig::redis("redis://localhost:6379")?;
26//!     let redis_cache = RedisCache::new(redis_config).await?;
27//!
28//!     redis_cache.set_json("key", "value".to_string(), Some(Duration::from_secs(60))).await?;
29//!
30//!     Ok(())
31//! }
32//! ```
33//!
34//! ## Tag-based Invalidation
35//!
36//! ```no_run
37//! use armature_cache::*;
38//! use std::sync::Arc;
39//!
40//! # async fn example() -> Result<(), CacheError> {
41//! # let redis_cache = Arc::new(InMemoryCache::new());
42//! let tagged = TaggedCache::new(redis_cache);
43//!
44//! // Set with tags
45//! tagged.set_with_tags(
46//!     "user:123",
47//!     r#"{"name":"Alice"}"#.to_string(),
48//!     &["users", "active-users"],
49//!     None,
50//! ).await?;
51//!
52//! // Invalidate all entries with "users" tag
53//! tagged.invalidate_tag("users").await?;
54//! # Ok(())
55//! # }
56//! ```
57//!
58//! ## Multi-tier Caching
59//!
60//! ```no_run
61//! use armature_cache::*;
62//! use std::sync::Arc;
63//!
64//! # async fn example() -> Result<(), CacheError> {
65//! # let redis_cache = Arc::new(InMemoryCache::new());
66//! let l1 = Arc::new(InMemoryCache::new());
67//! let l2 = redis_cache;
68//!
69//! let tiered = TieredCache::new(l1, l2);
70//!
71//! // Automatically uses L1 (fast) and falls back to L2
72//! tiered.set("key", "value".to_string(), None).await?;
73//! let value = tiered.get("key").await?;
74//! # Ok(())
75//! # }
76//! ```
77//!
78//! ## Memcached Cache (requires `memcached` feature)
79//!
80//! ```ignore
81//! use armature_cache::*;
82//! use std::time::Duration;
83//!
84//! #[tokio::main]
85//! async fn main() -> Result<(), CacheError> {
86//!     let memcached_config = CacheConfig::memcached("memcache://localhost:11211")?;
87//!     let memcached_cache = MemcachedCache::new(memcached_config).await?;
88//!
89//!     memcached_cache.set_json("key", "value".to_string(), Some(Duration::from_secs(60))).await?;
90//!
91//!     Ok(())
92//! }
93//! ```
94
95pub mod config;
96pub mod error;
97pub mod helpers;
98pub mod invalidation;
99pub mod manager;
100pub mod parallel;
101pub mod tiered;
102pub mod traits;
103
104#[cfg(feature = "redis")]
105pub mod redis_cache;
106
107#[cfg(feature = "memcached")]
108pub mod memcached_cache;
109
110pub use config::CacheConfig;
111pub use error::{CacheError, CacheResult};
112pub use helpers::*;
113pub use invalidation::TaggedCache;
114pub use manager::CacheManager;
115pub use tiered::{InMemoryCache, TieredCache, TieredCacheConfig};
116pub use traits::CacheStore;
117
118#[cfg(feature = "redis")]
119pub use redis_cache::RedisCache;
120
121#[cfg(feature = "memcached")]
122pub use memcached_cache::MemcachedCache;
123
124/// Re-export commonly used types
125pub mod prelude {
126    pub use crate::config::CacheConfig;
127    pub use crate::error::{CacheError, CacheResult};
128    pub use crate::invalidation::TaggedCache;
129    pub use crate::manager::CacheManager;
130    pub use crate::tiered::{InMemoryCache, TieredCache, TieredCacheConfig};
131    pub use crate::traits::CacheStore;
132
133    #[cfg(feature = "redis")]
134    pub use crate::redis_cache::RedisCache;
135
136    #[cfg(feature = "memcached")]
137    pub use crate::memcached_cache::MemcachedCache;
138}