Files
mytheclipse/crates/mytheclipse-cache/src/lib.rs
T
asepharyana 5717f8aaaa
CI / Rustfmt (push) Canceled after 0s
CI / Clippy (push) Canceled after 0s
CI / Test (workspace all features) (push) Canceled after 0s
CI / Test (workspace default features) (push) Canceled after 0s
CI / Test (mytheclipse / bg only) (push) Canceled after 0s
CI / Test (mytheclipse / compute only) (push) Canceled after 0s
CI / Test (mytheclipse / io only) (push) Canceled after 0s
CI / Test (mytheclipse / lifecycle only) (push) Canceled after 0s
CI / Test (mytheclipse / observability only) (push) Canceled after 0s
CI / Test (mytheclipse / resiliency only) (push) Canceled after 0s
CI / Test (mytheclipse / traffic only) (push) Canceled after 0s
CI / Test (mytheclipse-cache / l2-redis) (push) Canceled after 0s
CI / Test (mytheclipse-cache / l1-moka) (push) Canceled after 0s
CI / Test (mytheclipse-cache / default) (push) Canceled after 0s
CI / Test (mytheclipse-config / default) (push) Canceled after 0s
CI / Test (mytheclipse-crypto / default) (push) Canceled after 0s
CI / Test (mytheclipse-event / amqp) (push) Canceled after 0s
CI / Test (mytheclipse-event / nats) (push) Canceled after 0s
CI / Test (mytheclipse-event / default (mem)) (push) Canceled after 0s
CI / Test (mytheclipse-storage / gcs) (push) Canceled after 0s
CI / Test (mytheclipse-storage / s3) (push) Canceled after 0s
CI / Test (mytheclipse-storage / default (local)) (push) Canceled after 0s
CI / Run mytheclipse example (push) Canceled after 0s
CI / Docs check (push) Canceled after 0s
CI / Cargo package dry-run (mytheclipse) (push) Canceled after 0s
CI / Cargo package dry-run (mytheclipse-cache) (push) Canceled after 0s
CI / Cargo package dry-run (mytheclipse-config) (push) Canceled after 0s
CI / Cargo package dry-run (mytheclipse-crypto) (push) Canceled after 0s
CI / Cargo package dry-run (mytheclipse-event) (push) Canceled after 0s
CI / Cargo package dry-run (mytheclipse-storage) (push) Canceled after 0s
Release / Semantic Release (push) Canceled after 0s
fix(ci): gate cache & storage crate doctests behind their features
Same E0432 class as mytheclipse-event: the crate-root doctests referenced
feature-gated types that are absent in reduced-feature test builds flagged by
the CI matrix.

- mytheclipse-cache: example used MemoryCache (l1-memory) plus
  MultiLayerCache/CacheAside (cache-aside); the l1-moka and l2-redis builds
  (which don't enable cache-aside) failed the doctest. Gated the body behind
  all(l1-memory, cache-aside) with a no-op fallback.
- mytheclipse-storage: example used LocalFileStorage (local feature); the
  gcs and s3 builds failed the doctest. Gated the body behind the local
  feature with a no-op fallback.

Examples remain compile-checked & runnable under default features.
2026-08-28 22:49:39 +07:00

79 lines
2.3 KiB
Rust

//! # mytheclipse-cache
//!
//! A unified multi-layer cache abstraction that keeps your application from
//! being locked to any single cache provider.
//!
//! - **L1 (in-process) caches**: [`memory::MemoryCache`] (zero-dependency,
//! default) or [`moka_cache::MokaL1`] (high-performance, TTL/max-capacity).
//! - **L2 (distributed) caches**: [`redis::RedisCache`] backed by Redis/Valkey.
//! - **Multi-layer composition**: [`multilayer::MultiLayerCache`] layers an L1
//! over an L2 behind one [`Cache`] face; reads fall through to L2 and
//! backfill L1.
//! - **Cache-aside / auto-refresh**: [`cache_aside::CacheAside`] reads through
//! to a data source on a miss and caches the result.
//!
//! The core [`Cache`] trait is byte-oriented; typed convenience (JSON) is
//! layered on top via [`memory::typed::TypedCache`].
//!
//! ## Example
//!
//! Multi-layer + cache-aside composition (default features):
//!
//! ```no_run
//! # #[cfg(all(feature = "l1-memory", feature = "cache-aside"))]
//! # async fn run() {
//! use mytheclipse_cache::{Cache, MemoryCache, MultiLayerCache, CacheAside};
//! let l1 = MemoryCache::new();
//! let l2 = MemoryCache::new(); // in a real app: a RedisCache
//! let cache = MultiLayerCache::new(l1, l2);
//!
//! cache.set("user:1", b"payload".to_vec(), None).await.unwrap();
//! assert_eq!(cache.get("user:1").await.unwrap(), Some(b"payload".to_vec()));
//!
//! // Cache-aside: fill misses from a source of truth.
//! let aside = CacheAside::new(
//! MemoryCache::new(),
//! |key| async move { Some(format!("data-for-{key}").into_bytes()) },
//! );
//! let _v = aside.get("orders:42").await.unwrap();
//! # }
//! # #[cfg(not(all(feature = "l1-memory", feature = "cache-aside")))]
//! # fn run() {}
//! ```
#![forbid(unsafe_code)]
pub mod traits;
#[cfg(feature = "l1-memory")]
pub mod memory;
#[cfg(feature = "l1-moka")]
pub mod moka_cache;
#[cfg(feature = "l2-redis")]
pub mod redis;
#[cfg(feature = "cache-aside")]
pub mod cache_aside;
#[cfg(feature = "cache-aside")]
pub mod multilayer;
pub use traits::{Cache, CacheError};
#[cfg(feature = "l1-memory")]
pub use memory::MemoryCache;
#[cfg(feature = "l1-moka")]
pub use moka_cache::MokaL1;
#[cfg(feature = "l2-redis")]
pub use redis::RedisCache;
#[cfg(feature = "cache-aside")]
pub use cache_aside::CacheAside;
#[cfg(feature = "cache-aside")]
pub use multilayer::MultiLayerCache;