Module cache

Module cache 

Source
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.0

Unless 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

  1. Unified Interface: Consistent API across all backend implementations
  2. Multiple Backends: Support for different cache storage options
  3. Async Support: Full async/await compatibility
  4. Configurable: Highly configurable cache behavior
  5. Non-critical: Cache failures should not break the application
  6. Stats Collection: Built-in cache statistics for monitoring
  7. Service Module Integration: Implements both sync and async service module traits
  8. 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§

DMSCCacheConfig
Main cache configuration structure.
DMSCCacheManager
Cache manager that coordinates different cache backends with consistency support
DMSCCacheModule
Main cache module for DMSC.
DMSCCachePolicy
Cache policy for individual cache entries.
DMSCCacheStats
Cache statistics
DMSCCachedValue
Cached value wrapper with TTL and LRU support.
DMSCHybridCache
Hybrid cache combining in-memory and Redis cache for optimal performance Hybrid cache implementation combining memory and Redis backends.
DMSCMemoryCache
In-memory cache implementation using DashMap for high performance In-memory cache implementation using DashMap for high performance and thread safety.
DMSCRedisCache
Redis-based cache implementation for distributed systems Redis cache implementation.

Enums§

CoreCacheEvent
Cache event types for monitoring and consistency
DMSCCacheBackendType
Cache backend type enumeration.
DMSCCacheEvent
DMSC Cache Manager

Traits§

DMSCCache
Cache trait for DMSC cache implementations.