pub struct LLMCache { /* private fields */ }Expand description
LLM call cache
Caches LLM call inputs/outputs, avoiding duplicate API calls for identical requests.
§Example
ⓘ
use langchainrust::core::cache::LLMCache;
let cache = LLMCache::new();
cache.put("key", llm_result).await;
if let Some(cached) = cache.get("key").await {
println!("缓存命中: {}", cached.result.content);
}Implementations§
Source§impl LLMCache
impl LLMCache
Sourcepub fn with_config(config: CacheConfig) -> Self
pub fn with_config(config: CacheConfig) -> Self
Creates a cache with the given configuration.
Sourcepub fn build_key(
messages: &[Message],
model: &str,
) -> Result<String, CacheError>
pub fn build_key( messages: &[Message], model: &str, ) -> Result<String, CacheError>
Builds a cache key from a message list.
Serializes the message list to a JSON string as the key. Includes the model name so calls to different models do not affect each other. On serialization failure, returns an error instead of falling back to an empty string (M34).
Sourcepub async fn get(&self, key: &str) -> Option<CachedLLMResult>
pub async fn get(&self, key: &str) -> Option<CachedLLMResult>
Fetches a cached result.
An expired entry is removed immediately (H36). A hit refreshes cached_at,
keeping the cache in true LRU semantics (Q7: a hit makes the entry the most-recently used).
Sourcepub async fn evict_expired(&self) -> usize
pub async fn evict_expired(&self) -> usize
Removes expired entries
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for LLMCache
impl !RefUnwindSafe for LLMCache
impl !UnwindSafe for LLMCache
impl Send for LLMCache
impl Sync for LLMCache
impl Unpin for LLMCache
impl UnsafeUnpin for LLMCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more