Expand description
High-performance data caching layer for Prax ORM.
This module provides a flexible, multi-tier caching system for query results with support for:
- In-memory caching using moka for high-performance concurrent access
- Redis caching for distributed cache across multiple instances
- Tiered caching combining L1 (memory) and L2 (Redis) for optimal performance
- Automatic invalidation based on TTL, entity changes, or custom patterns
- Cache-aside pattern with transparent integration into queries
§Architecture
┌─────────────────────────────────────────────────────────────────┐
│ Application │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Prax Query Builder │
│ .cache(CacheOptions::new()) │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Cache Manager │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ L1: Memory │ -> │ L2: Redis │ -> │ Database │ │
│ │ (< 1ms) │ │ (1-5ms) │ │ (10-100ms) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘§Quick Start
ⓘ
use prax_query::data_cache::{CacheManager, MemoryCache, RedisCache, TieredCache};
use std::time::Duration;
// In-memory only (single instance)
let cache = MemoryCache::builder()
.max_capacity(10_000)
.time_to_live(Duration::from_secs(300))
.build();
// Redis only (distributed)
let redis = RedisCache::new("redis://localhost:6379").await?;
// Tiered: Memory (L1) + Redis (L2)
let tiered = TieredCache::new(cache, redis);
// Use with queries
let users = client
.user()
.find_many()
.cache(CacheOptions::ttl(Duration::from_secs(60)))
.exec()
.await?;§Cache Invalidation
ⓘ
use prax_query::data_cache::{InvalidationStrategy, EntityTag};
// 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("user:*:profile").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 |
| Redis | 1-5ms | Large | Multi-instance | Shared state, large datasets |
| Tiered | < 1ms (L1 hit) | Both | Multi-instance | Production systems |
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.