Expand description
High-performance data caching layer for Prax ORM.
This module provides a flexible, standalone caching system for query results with support for:
- In-memory caching using moka for high-performance concurrent access
- Tiered caching layering any two
CacheBackendimplementations (e.g. memory L1 over a slower L2) - Automatic invalidation based on TTL, entity changes, or custom patterns
- Cache-aside usage through the standalone
CacheManagerAPI
Not yet available:
- Transparent query integration — there is no
.cache(...)method on query operations; wrap queries manually withCacheManageras shown below.- The Redis backend (
RedisCache) — construction and every operation return an error because no Redis client is compiled in.
§Architecture
┌─────────────────────────────────────────────────────────────────┐
│ Application │
│ (standalone CacheManager; no query integration yet) │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Cache Manager │
│ ┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐ │
│ │ L1: Memory │ -> │ L2: CacheBackend │ -> │ Database │ │
│ │ (< 1ms) │ │ (e.g. NoopCache) │ │ (10-100ms) │ │
│ └─────────────┘ └──────────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────────────────────────┘§Quick Start
ⓘ
use prax_query::data_cache::{CacheKey, CacheManager, MemoryCache};
use std::time::Duration;
// In-memory backend (single instance)
let backend = MemoryCache::builder()
.max_capacity(10_000)
.time_to_live(Duration::from_secs(300))
.build();
let cache = CacheManager::new(backend);
// Standalone cache-aside usage around your own data access
let key = CacheKey::new("User", "find_many:all");
let users: Vec<User> = cache
.get_or_set(
&key,
|| async { Ok(load_all_users().await) },
None,
)
.await?;§Cache Invalidation
ⓘ
use prax_query::data_cache::{EntityTag, KeyPattern};
// Invalidate by entity type
cache.invalidate_entity("User").await?;
// Invalidate by specific record
cache.invalidate_record("User", &user_id).await?;
// Invalidate by pattern
cache.invalidate_pattern(&KeyPattern::entity("User")).await?;
// Tag-based invalidation
cache.invalidate_tags(&[EntityTag::new("User"), EntityTag::new("tenant:123")]).await?;§Performance Characteristics
| Backend | Latency | Capacity | Distribution | Best For |
|---|---|---|---|---|
| Memory | < 1ms | Limited by RAM | Single instance | Hot data, sessions |
| Tiered | < 1ms (L1 hit) | Both layers | Depends on L2 | Layered setups |
A Redis backend for distributed, multi-instance caching is planned but not yet implemented.
Structs§
- Cache
Entry - A cached entry with metadata.
- Cache
Key - A cache key that uniquely identifies a cached value.
- Cache
KeyBuilder - A builder for constructing complex cache keys.
- Cache
Manager - The main cache manager that coordinates caching operations.
- Cache
Manager Builder - Builder for creating cache managers with different configurations.
- Cache
Metrics - Thread-safe cache metrics collector.
- Cache
Options - Options for caching a query result.
- Cache
Stats - A snapshot of cache statistics.
- Entity
Tag - A tag for categorizing and invalidating cache entries.
- Invalidation
Event - An event that triggers cache invalidation.
- KeyPattern
- A pattern for matching cache keys.
- Memory
Cache - High-performance in-memory cache.
- Memory
Cache Builder - Builder for MemoryCache.
- Memory
Cache Config - Configuration for the in-memory cache.
- Redis
Cache - Redis cache backend.
- Redis
Cache Config - Configuration for Redis cache.
- Redis
Connection - Represents a Redis connection (placeholder for actual implementation).
- Tiered
Cache - A tiered cache with L1 (local) and L2 (distributed) layers.
- Tiered
Cache Config - Configuration for tiered cache.
Enums§
- Cache
Error - Errors that can occur during cache operations.
- Cache
Policy - Cache lookup/write policy.
- Invalidation
Strategy - Strategy for cache invalidation.
- Write
Policy - When to write to the cache.
Traits§
- Cache
Backend - The core trait for cache backends.
Type Aliases§
- Cache
Result - Result type for cache operations.