Skip to main content

Module cache

Module cache 

Source
Expand description

响应缓存模块:提供可插拔的缓存后端以减少重复 API 调用。

§Response Caching Module

This module provides flexible response caching capabilities with pluggable backends, reducing API calls and improving response times for repeated requests.

§Overview

Caching is valuable for:

  • Reducing API costs by avoiding duplicate requests
  • Improving response latency for repeated queries
  • Enabling offline or degraded mode operation
  • Supporting development and testing workflows

§Key Components

ComponentDescription
CacheManagerHigh-level cache management with TTL and statistics
CacheConfigConfiguration for cache behavior and limits
CacheBackendTrait for implementing custom cache backends
MemoryCacheIn-memory LRU cache implementation
NullCacheNo-op cache for disabling caching
CacheKeyCache key generation from request parameters

§Example

use ai_lib_rust::cache::{CacheManager, CacheConfig, MemoryCache};
use std::time::Duration;

// Create an in-memory cache with 1-hour TTL
let backend = MemoryCache::new(1000); // max 1000 entries
let config = CacheConfig {
    ttl: Duration::from_secs(3600),
    enabled: true,
    ..Default::default()
};
let cache = CacheManager::new(Box::new(backend), config);

§Cache Key Generation

Cache keys are generated from:

  • Model identifier
  • Message content (hashed)
  • Request parameters (temperature, max_tokens, etc.)

This ensures identical requests return cached responses while different parameters generate new cache entries.

Structs§

CacheConfig
CacheKey
CacheKeyGenerator
CacheManager
CacheStats
MemoryCache
NullCache

Traits§

CacheBackend