Expand description
Multi-backend cache abstraction Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of DMSC. The DMSC 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 DMSC, offering a unified interface with support for multiple backend implementations. It enables efficient data caching with configurable policies and backend selection.
§Key Components
- DMSCCacheModule: Main cache module implementing both sync and async service module traits
- DMSCCacheManager: Central cache management component
- DMSCCache: Unified cache interface implemented by all backends
- DMSCCacheConfig: Configuration for cache behavior
- Backend Implementations:
- DMSCMemoryCache: In-memory cache implementation (internal)
- DMSCRedisCache: Redis-based distributed cache (internal)
- DMSCHybridCache: 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 dmsc::prelude::*;
async fn example() -> DMSCResult<()> {
// Create cache configuration
let cache_config = DMSCCacheConfig {
enabled: true,
default_ttl_secs: 3600,
max_memory_mb: 512,
cleanup_interval_secs: 300,
backend_type: DMSCCacheBackendType::Memory,
redis_url: "redis://127.0.0.1:6379".to_string(),
redis_pool_size: 10,
};
// Create cache module
let cache_module = DMSCCacheModule::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§
- DMSC
Cache Config - Main cache configuration structure.
- DMSC
Cache Manager - Cache manager that coordinates different cache backends with consistency support
- DMSC
Cache Module - Main cache module for DMSC.
- DMSC
Cache Policy - Cache policy for individual cache entries.
- DMSC
Cache Stats - Cache statistics
- DMSC
Cached Value - Cached value wrapper with TTL and LRU support.
- DMSC
Hybrid Cache - Hybrid cache combining in-memory and Redis cache for optimal performance Hybrid cache implementation combining memory and Redis backends.
- DMSC
Memory Cache - In-memory cache implementation using DashMap for high performance In-memory cache implementation using DashMap for high performance and thread safety.
- DMSC
Redis Cache - Redis-based cache implementation for distributed systems Redis cache implementation.
Enums§
- Core
Cache Event - Cache event types for monitoring and consistency
- DMSC
Cache Backend Type - Cache backend type enumeration.
- DMSC
Cache Event - DMSC Cache Manager
Traits§
- DMSC
Cache - Cache trait for DMSC cache implementations.