Crate cachified

Source
Expand description

§Cachified-rs

A work-in-progress port of the cachified library from TypeScript to Rust.

§Features

  • moka (default): Enable Moka in-memory cache backend
  • redis: Enable Redis distributed cache backend
  • serde (default): Enable serialization support (required for Redis)
  • tracing: Enable tracing support

§Quick Start

§With Moka (in-memory cache)

use cachified::{cachified, CachifiedOptionsBuilder, MokaCache};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cache = MokaCache::new(1000);
     
    let value: String = cachified(
        CachifiedOptionsBuilder::new(cache, "user-1")
            .ttl(Duration::from_secs(300)) // 5 minutes
            .get_fresh_value(|| async { 
                // This would typically be a database call, API request, etc.
                Ok("fresh-value".to_string())
            })
    ).await?;
     
    println!("Cached value: {}", value);
    Ok(())
}

§With Redis (distributed cache)

use cachified::{cachified, CachifiedOptionsBuilder, RedisCache};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cache = RedisCache::new("redis://localhost:6379").await?;
     
    let value: String = cachified(
        CachifiedOptionsBuilder::new(cache, "user-1")
            .ttl(Duration::from_secs(300)) // 5 minutes
            .get_fresh_value(|| async { 
                // This would typically be a database call, API request, etc.
                Ok("fresh-value".to_string())
            })
    ).await?;
     
    println!("Cached value: {}", value);
    Ok(())
}

Re-exports§

pub use cache::Cache;
pub use cache::MokaCache;
pub use error::CachifiedError;
pub use error::Result;
pub use options::CachifiedOptions;
pub use options::CachifiedOptionsBuilder;
pub use metadata::CacheMetadata;
pub use metadata::CacheEntry;
pub use validation::CheckValue;

Modules§

cache
Cache trait and implementations
error
Error types for cachified operations.
metadata
Cache metadata and entry structures.
options
Configuration options for the cachified function
validation
Value validation for cached entries.

Structs§

SoftPurgeOptions
Soft purge options for controlling soft purging behavior

Functions§

cachified
The main cachified function that provides caching functionality.
soft_purge
Soft purge a cache entry.