CacheStore

Trait CacheStore 

Source
pub trait CacheStore: Send + Sync {
Show 14 methods // Required methods fn get_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<String>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn set_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: String, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn exists<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<bool>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<Duration>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn expire<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ttl: Duration, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn increment<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn decrement<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; // Provided methods fn get_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<String>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn set_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, String)], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn delete_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn exists_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<bool>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn ttl_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<Duration>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... }
}
Expand description

Cache store trait for different cache backends.

Required Methods§

Source

fn get_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get a JSON value from the cache.

§Arguments
  • key - The cache key
§Returns

Returns Ok(Some(value)) if the key exists, Ok(None) if not found, or an error if the operation fails.

Source

fn set_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: String, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set a JSON value in the cache.

§Arguments
  • key - The cache key
  • value - The JSON string value
  • ttl - Optional time-to-live duration
Source

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a key from the cache.

§Arguments
  • key - The cache key to delete
Source

fn exists<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if a key exists in the cache.

§Arguments
  • key - The cache key to check
Source

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clear all keys from the cache.

Warning: This operation may be destructive and affect all keys.

Source

fn ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<Duration>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get the TTL (time-to-live) of a key.

§Arguments
  • key - The cache key
§Returns

Returns Ok(Some(duration)) if the key has a TTL, Ok(None) if the key has no expiration or doesn’t exist.

Source

fn expire<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ttl: Duration, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set or update the expiration time for a key.

§Arguments
  • key - The cache key
  • ttl - The new time-to-live duration
Source

fn increment<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Increment a numeric value.

§Arguments
  • key - The cache key
  • delta - The amount to increment by
§Returns

Returns the new value after incrementing.

Source

fn decrement<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Decrement a numeric value.

§Arguments
  • key - The cache key
  • delta - The amount to decrement by
§Returns

Returns the new value after decrementing.

Provided Methods§

Source

fn get_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<String>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get multiple keys in parallel.

This operation fetches multiple cache keys concurrently, significantly reducing total latency compared to sequential gets.

§Arguments
  • keys - Slice of cache keys to fetch
§Returns

Returns a vector of Option<String> in the same order as the input keys. None indicates the key was not found.

§Performance
  • Sequential: O(n * network_latency)
  • Parallel: O(max(network_latencies)) ≈ O(network_latency)
  • Speedup: 10-100x for network-bound operations
§Examples
// Fetch 100 user profiles in parallel
let keys: Vec<String> = (1..=100).map(|i| format!("user:{}", i)).collect();
let key_refs: Vec<&str> = keys.iter().map(|s| s.as_str()).collect();
let profiles = cache.get_many(&key_refs).await?;

// Sequential: ~1000ms (10ms * 100)
// Parallel:   ~15ms (max of all parallel requests)
Source

fn set_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, String)], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Set multiple key-value pairs in parallel.

§Arguments
  • items - Slice of (key, value) tuples
  • ttl - Optional time-to-live for all keys
§Performance

10-100x faster than sequential sets for network-bound operations.

§Examples
use std::time::Duration;

let items = vec![
    ("user:1", r#"{"name":"Alice"}"#.to_string()),
    ("user:2", r#"{"name":"Bob"}"#.to_string()),
];

cache.set_many(&items, Some(Duration::from_secs(3600))).await?;
Source

fn delete_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Delete multiple keys in parallel.

§Arguments
  • keys - Slice of cache keys to delete
§Performance

10-100x faster than sequential deletes.

§Examples
// Bulk cache invalidation
let keys = vec!["session:1", "session:2", "session:3"];
cache.delete_many(&keys).await?;
Source

fn exists_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<bool>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Check existence of multiple keys in parallel.

§Arguments
  • keys - Slice of cache keys to check
§Returns

Returns a vector of booleans in the same order as input keys.

§Examples
let keys = vec!["user:1", "user:2", "user:3"];
let exists = cache.exists_many(&keys).await?;

for (key, exists) in keys.iter().zip(exists.iter()) {
    println!("{}: {}", key, exists);
}
Source

fn ttl_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<Duration>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get TTL for multiple keys in parallel.

§Arguments
  • keys - Slice of cache keys
§Returns

Returns a vector of Option<Duration> for each key.

§Examples
let keys = vec!["session:1", "session:2"];
let ttls = cache.ttl_many(&keys).await?;

for (key, ttl) in keys.iter().zip(ttls.iter()) {
    match ttl {
        Some(duration) => println!("{}: expires in {:?}", key, duration),
        None => println!("{}: no expiration", key),
    }
}

Implementors§