Expand description
Multi-backend cache abstraction Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of Ri. The Ri project belongs to the Dunimd Team.
Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
§Cache Module
This module provides a comprehensive caching abstraction for Ri, offering a unified interface with support for multiple backend implementations. It enables efficient data caching with configurable policies and backend selection.
§Key Components
- RiCacheModule: Main cache module implementing both sync and async service module traits
- RiCacheManager: Central cache management component
- RiCache: Unified cache interface implemented by all backends
- RiCacheConfig: Configuration for cache behavior
- Backend Implementations:
- RiMemoryCache: In-memory cache implementation (internal)
- RiRedisCache: Redis-based distributed cache (internal)
- RiHybridCache: Combined memory and Redis cache for optimal performance (internal)
§Design Principles
- Unified Interface: Consistent API across all backend implementations
- Multiple Backends: Support for different cache storage options
- Async Support: Full async/await compatibility
- Configurable: Highly configurable cache behavior
- Non-critical: Cache failures should not break the application
- Stats Collection: Built-in cache statistics for monitoring
- Service Module Integration: Implements both sync and async service module traits
- Thread-safe: Safe for concurrent use across multiple threads
§Usage
use ri::prelude::*;
async fn example() -> RiResult<()> {
// Create cache configuration
let cache_config = RiCacheConfig {
enabled: true,
default_ttl_secs: 3600,
max_memory_mb: 512,
cleanup_interval_secs: 300,
backend_type: RiCacheBackendType::Memory,
redis_url: "redis://127.0.0.1:6379".to_string(),
redis_pool_size: 10,
};
// Create cache module
let cache_module = RiCacheModule::new(cache_config);
// Get cache manager
let cache_manager = cache_module.cache_manager();
// Use cache manager to get cache instance
let cache = cache_manager.read().await.get_cache();
// Set a value in cache
cache.set("key", "value", Some(3600)).await?;
// Get a value from cache
let value = cache.get("key").await?;
println!("Cached value: {:?}", value);
Ok(())
}Structs§
- RiCache
Config - Main cache configuration structure.
- RiCache
Manager - Cache manager that coordinates different cache backends with consistency support
- RiCache
Module - Main cache module for Ri.
- RiCache
Policy - Cache policy for individual cache entries.
- RiCache
Stats - Cache statistics
- RiCached
Value - Cached value wrapper with TTL and LRU support.
- RiHybrid
Cache - Hybrid cache combining in-memory and Redis cache for optimal performance Hybrid cache implementation combining memory and Redis backends.
- RiMemory
Cache - In-memory cache implementation using DashMap for high performance In-memory cache implementation using DashMap for high performance and thread safety.
- RiRedis
Cache - Redis-based cache implementation for distributed systems Redis cache implementation.
Enums§
- Core
Cache Event - Cache event types for monitoring and consistency
- RiCache
Backend Type - Cache backend type enumeration.
- RiCache
Event - Ri Cache Manager
Traits§
- RiCache
- Cache trait for Ri cache implementations.