pub trait Cache: Send + Sync {
// Required methods
fn get(&self, key: &str) -> Option<CachedResponse>;
fn set(&self, key: &str, response: CachedResponse, ttl: Duration);
fn invalidate(&self, key: &str);
fn clear(&self);
fn size(&self) -> usize;
}Expand description
Trait for cache implementations.
This trait defines the interface for different cache backends, allowing for pluggable caching implementations.
§Examples
use ultrafast_models_sdk::cache::{Cache, InMemoryCache, CachedResponse};
use std::time::Duration;
let cache: Box<dyn Cache> = Box::new(InMemoryCache::new(100));
// Store a response
let response = CachedResponse::new(chat_response, Duration::from_secs(3600));
cache.set("key", response, Duration::from_secs(3600));
// Retrieve from cache
if let Some(cached) = cache.get("key") {
println!("Cache hit: {}", cached.response.choices[0].message.content);
}Required Methods§
Sourcefn get(&self, key: &str) -> Option<CachedResponse>
fn get(&self, key: &str) -> Option<CachedResponse>
Retrieve a cached response by key.
Returns Some(CachedResponse) if found and not expired, None otherwise.
Sourcefn set(&self, key: &str, response: CachedResponse, ttl: Duration)
fn set(&self, key: &str, response: CachedResponse, ttl: Duration)
Store a response in the cache.
§Arguments
key- Cache key for the responseresponse- The response to cachettl- Time-to-live for the cached response