Cache

Trait Cache 

Source
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§

Source

fn get(&self, key: &str) -> Option<CachedResponse>

Retrieve a cached response by key.

Returns Some(CachedResponse) if found and not expired, None otherwise.

Source

fn set(&self, key: &str, response: CachedResponse, ttl: Duration)

Store a response in the cache.

§Arguments
  • key - Cache key for the response
  • response - The response to cache
  • ttl - Time-to-live for the cached response
Source

fn invalidate(&self, key: &str)

Remove a specific entry from the cache.

§Arguments
  • key - Cache key to invalidate
Source

fn clear(&self)

Clear all entries from the cache.

Source

fn size(&self) -> usize

Get the current number of entries in the cache.

Implementors§