Skip to main content

Crate fncache

Crate fncache 

Source
Expand description

§fncache

A zero-boilerplate Rust library for function-level caching with pluggable backends, inspired by functools.lru_cache and request-cache.

§Features

  • Zero Boilerplate: Simple #[fncache] attribute for instant caching
  • Pluggable Backends: Memory, File, Redis, RocksDB support
  • Async/Sync: Seamless support for both synchronous and asynchronous functions
  • Type Safety: Strong typing throughout the caching layer with compile-time guarantees
  • Advanced Metrics: Built-in instrumentation with latency, hit rates, and size tracking
  • Cache Invalidation: Tag-based and prefix-based cache invalidation
  • Background Warming: Proactive cache population for improved performance

§Quick Start

// Example usage (not actually run in tests due to proc-macro limitations)
use fncache::{fncache, init_global_cache, MemoryBackend};

// Initialize the global cache with an in-memory backend
init_global_cache(MemoryBackend::new()).unwrap();

#[fncache(ttl = 60)] // Cache for 60 seconds
fn expensive_operation(x: u64) -> u64 {
    println!("Performing expensive operation for {}", x);
    x * x
}

fn main() {
    // First call executes the function
    let result1 = expensive_operation(5);
    println!("Result 1: {}", result1); // Takes time
     
    // Second call returns cached result
    let result2 = expensive_operation(5);
    println!("Result 2: {}", result2); // Returns immediately
}

Re-exports§

pub use error::Error as FncacheError;
pub use backends::memory::MemoryBackend;

Modules§

backends
Backend implementations for different storage systems.
error
Error types for the fncache library.
eviction
Eviction policy implementations.
invalidation
Cache Invalidation
key_derivation
Key derivation strategies for cache keys
metrics
Metrics collection for cache operations.
prelude
Common prelude for using the library.
serialization
Serialization support for fncache.
warming
Background cache warming implementation.

Structs§

GlobalCache
Internal structure to hold the cache backend

Functions§

global_cache
Get a reference to the global cache (test version).
init_global_cache
Initialize the global cache with the specified backend (test version).
reset_global_cache_for_testing
Reset the global cache for testing purposes.

Type Aliases§

Result
The main cache result type.

Attribute Macros§

fncache
Re-export of the proc macro for convenience.