Skip to main content

mx_cache/
lib.rs

1//! Shared cache utilities for `MultiversX` Rust services.
2//!
3//! Provides a simple async-friendly `Cache` trait plus Redis and local
4//! implementations with TTL semantics.
5//!
6//! # Re-exports
7//!
8//! This crate re-exports the `redis` crate for use by dependent crates,
9//! allowing them to access low-level Redis functionality without adding
10//! a direct dependency.
11
12use std::time::Duration;
13
14// Re-export redis crate for dependent crates to use
15pub use ::redis;
16
17pub trait Cache: Send + Sync {
18    fn set_nx_px(
19        &self,
20        key: &[u8],
21        value: &[u8],
22        ttl: Duration,
23    ) -> impl Future<Output = anyhow::Result<bool>> + Send;
24
25    fn set(
26        &self,
27        key: &[u8],
28        value: &[u8],
29        ttl: Duration,
30    ) -> impl Future<Output = anyhow::Result<()>> + Send;
31
32    fn get(&self, key: &[u8]) -> impl Future<Output = anyhow::Result<Option<Vec<u8>>>> + Send;
33
34    fn del(&self, key: &[u8]) -> impl Future<Output = anyhow::Result<()>> + Send;
35}
36
37mod local;
38mod redis_cache;
39mod utils;
40
41pub use local::LocalCache;
42pub use redis_cache::RedisCache;
43pub use utils::namespaced;
44pub mod set;
45pub use set::RedisSet;
46
47/// Mock implementations for testing without external dependencies.
48pub mod mock;
49
50#[cfg(test)]
51mod tests;