Skip to main content

Cache

Trait Cache 

Source
pub trait Cache: Send + Sync {
    // Required methods
    fn get<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, SimpleAgentsError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn set<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        value: Vec<u8>,
        ttl: Duration,
    ) -> Pin<Box<dyn Future<Output = Result<(), SimpleAgentsError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn delete<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<(), SimpleAgentsError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn clear<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), SimpleAgentsError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;

    // Provided methods
    fn is_enabled(&self) -> bool { ... }
    fn name(&self) -> &str { ... }
}
Expand description

Trait for caching LLM responses.

Implementations can use various backends:

  • In-memory (HashMap, LRU)
  • Redis
  • Disk-based
  • Distributed caches

§Example Implementation

use simple_agent_type::cache::Cache;
use simple_agent_type::error::Result;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

struct InMemoryCache {
    store: Arc<Mutex<HashMap<String, Vec<u8>>>>,
}

#[async_trait]
impl Cache for InMemoryCache {
    async fn get(&self, key: &str) -> Result<Option<Vec<u8>>> {
        let store = self.store.lock().unwrap();
        Ok(store.get(key).cloned())
    }

    async fn set(&self, key: &str, value: Vec<u8>, _ttl: Duration) -> Result<()> {
        let mut store = self.store.lock().unwrap();
        store.insert(key.to_string(), value);
        Ok(())
    }

    async fn delete(&self, key: &str) -> Result<()> {
        let mut store = self.store.lock().unwrap();
        store.remove(key);
        Ok(())
    }

    async fn clear(&self) -> Result<()> {
        let mut store = self.store.lock().unwrap();
        store.clear();
        Ok(())
    }
}

let cache = InMemoryCache {
    store: Arc::new(Mutex::new(HashMap::new())),
};

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
    cache
        .set("request:abc123", b"ok".to_vec(), Duration::from_secs(60))
        .await
        .unwrap();
    let value = cache.get("request:abc123").await.unwrap();
    assert_eq!(value, Some(b"ok".to_vec()));
});

Required Methods§

Source

fn get<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, SimpleAgentsError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Get a value from the cache.

Returns Ok(None) if the key doesn’t exist or has expired.

§Arguments
  • key: Cache key
§Example
use simple_agent_type::cache::Cache;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

cache
    .set("request:abc123", b"ok".to_vec(), Duration::from_secs(60))
    .await
    .unwrap();
let value = cache.get("request:abc123").await.unwrap();
assert_eq!(value, Some(b"ok".to_vec()));
Source

fn set<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: Vec<u8>, ttl: Duration, ) -> Pin<Box<dyn Future<Output = Result<(), SimpleAgentsError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Set a value in the cache with TTL.

§Arguments
  • key: Cache key
  • value: Serialized value (typically JSON bytes)
  • ttl: Time-to-live (expiration duration)
§Example
use simple_agent_type::cache::Cache;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

cache
    .set("request:abc123", b"payload".to_vec(), Duration::from_secs(3600))
    .await
    .unwrap();
Source

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), SimpleAgentsError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Delete a value from the cache.

§Arguments
  • key: Cache key
§Example
use simple_agent_type::cache::Cache;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

cache
    .set("request:abc123", b"payload".to_vec(), Duration::from_secs(60))
    .await
    .unwrap();
cache.delete("request:abc123").await.unwrap();
Source

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), SimpleAgentsError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Clear all values from the cache.

§Warning

This is a destructive operation. Use with caution.

§Example
use simple_agent_type::cache::Cache;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

cache
    .set("request:abc123", b"payload".to_vec(), Duration::from_secs(60))
    .await
    .unwrap();
cache.clear().await.unwrap();

Provided Methods§

Source

fn is_enabled(&self) -> bool

Check if caching is enabled.

This allows for a “no-op” cache implementation that always returns false, disabling caching without changing call sites.

Source

fn name(&self) -> &str

Get the cache name/type.

Used for logging and debugging.

Implementors§