Skip to main content

Crate memento_cache

Crate memento_cache 

Source
Expand description

§Memento

A flexible caching library with tiered storage and cost-based placement.

§Features

  • Tiered Storage: Local (Memory), Shared (Redis), and Durable (Postgres) tiers
  • Cost-Based Placement: Automatic tier selection based on computation cost
  • Zero-Config: Works out of the box with just memory caching
  • Structured Keys: Build cache keys from segments with wildcard invalidation support
  • Safe Key Parts: Use KeyPart::hash() for unbounded values like URLs
  • Negative Caching: Automatically caches None results to prevent repeated lookups
  • TTL Support: Configurable time-to-live per operation or globally

§Quick Start

use memento_cache::{Cache, TieredPlanBuilder, KeyPart, Result};

#[tokio::main]
async fn main() -> Result<()> {
    // Zero-config: memory only
    let cache = Cache::new(TieredPlanBuilder::new().build()?);

    // Cache a value with a structured key
    let user: Option<String> = cache
        .key(["users", "123"])
        .get(|| async { Some("Alice".to_string()) })
        .await?;

    // Use KeyPart::hash() for unbounded values like URLs
    let url = "https://example.com/very/long/path?with=params";
    let data: Vec<u8> = cache
        .key([KeyPart::literal("convertPCMtoMP3"), KeyPart::hash(url)])
        .get(|| async { vec![1, 2, 3] })
        .await?;

    // Invalidate with wildcards
    cache.key(["users", "*"]).invalidate().await?;

    Ok(())
}

Re-exports§

pub use error::MementoError;
pub use error::Result;
pub use core::ByteSize;
pub use core::CacheCost;
pub use core::CacheEntry;
pub use core::CacheExpires;
pub use core::CacheKey;
pub use core::CacheKeyPattern;
pub use core::Cached;
pub use core::KeyPart;
pub use core::Tier;
pub use store::CacheStore;
pub use store::MemoryStore;
pub use store::MemoryStoreBuilder;
pub use store::MemoryStoreConfig;
pub use store::PostgresBuilder;
pub use store::PostgresStore;
pub use store::RedisStore;
pub use store::ResolvedTierPlan;
pub use store::StoreStats;
pub use store::TierTTLs;
pub use store::TieredPlanBuilder;
pub use store::TieredStore;

Modules§

core
error
Error types for memento.
store

Structs§

Cache
A flexible caching system with pluggable storage strategies.
CacheQuery
A cache query scoped to a structured key.
NamespacedCache
A namespaced view of a cache.