Crate cache_kit

Crate cache_kit 

Source
Expand description

§cache-kit

A type-safe, fully generic, production-ready caching framework for Rust.

§Features

  • Fully Generic: Cache any type T that implements CacheEntity
  • Backend Agnostic: Support for in-memory, Redis, Memcached, and custom backends
  • Database Agnostic: Works with SQLx, tokio-postgres, Diesel, or custom repositories
  • Framework Independent: Zero dependencies on web frameworks (Axum, Actix, Rocket, etc.)
  • Production Ready: Built-in logging, metrics support, and error handling
  • Type Safe: Compile-time verified, no magic strings

§Quick Start

Use CacheService for easy sharing across threads:

use cache_kit::{
    CacheService, CacheEntity, CacheFeed, DataRepository,
    backend::InMemoryBackend,
    strategy::CacheStrategy,
};
use serde::{Deserialize, Serialize};

// 1. Define your entity
#[derive(Clone, Serialize, Deserialize)]
struct User {
    id: String,
    name: String,
}

// 2. Implement CacheEntity
impl CacheEntity for User {
    type Key = String;
    fn cache_key(&self) -> Self::Key { self.id.clone() }
    fn cache_prefix() -> &'static str { "user" }
}

// 3. Create feeder
struct UserFeeder {
    id: String,
    user: Option<User>,
}

impl CacheFeed<User> for UserFeeder {
    fn entity_id(&mut self) -> String { self.id.clone() }
    fn feed(&mut self, entity: Option<User>) { self.user = entity; }
}

// 4. Create cache (wrap backend in Arc automatically)
let cache = CacheService::new(InMemoryBackend::new());

// 5. Use it - CacheService is Clone for thread sharing
let cache_clone = cache.clone();  // Cheap - just Arc increment
let mut feeder = UserFeeder { id: "user_1".to_string(), user: None };
cache.execute(&mut feeder, &repository, CacheStrategy::Refresh).await?;

§For Custom Patterns (Advanced)

Use CacheExpander for explicit control:

use cache_kit::{CacheExpander, backend::InMemoryBackend};
use std::sync::Arc;

// Lower-level API - wrap in Arc yourself if needed
let expander = CacheExpander::new(InMemoryBackend::new());
let cache = Arc::new(expander);  // Manual Arc wrapping
let cache_clone = cache.clone();

Re-exports§

pub use backend::CacheBackend;
pub use entity::CacheEntity;
pub use error::Error;
pub use error::Result;
pub use expander::CacheExpander;
pub use expander::OperationConfig;
pub use feed::CacheFeed;
pub use repository::DataRepository;
pub use service::CacheService;
pub use strategy::CacheStrategy;

Modules§

backend
Cache backend implementations.
entity
Core entity trait that all cached entities must implement.
error
Error types for the cache framework.
expander
Cache expander - main entry point for cache operations.
feed
Feeder trait for consuming cached data.
key
Cache key management utilities.
observability
Observability and metrics collection for cache operations.
repository
Data repository trait for abstracting database access.
serialization
Postcard-based cache serialization with versioned envelopes.
service
High-level cache service for web applications.
strategy
Cache strategies and decision logic for fetch operations.

Constants§

VERSION
Library version