CachePort

Trait CachePort 

Source
pub trait CachePort: Send + Sync {
    // Required methods
    fn get<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn set<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
        value: String,
        ttl: Option<Duration>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn invalidate<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn exists<'life0, 'life1, 'async_trait>(
        &'life0 self,
        key: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Cache port for storing/retrieving data with idempotency key support

Provides a key-value store interface for caching scraped content, API responses, and idempotency tracking.

§Example Implementation

use stygian_graph::ports::CachePort;
use stygian_graph::domain::error::Result;
use async_trait::async_trait;
use std::time::Duration;

struct MyCache;

#[async_trait]
impl CachePort for MyCache {
    async fn get(&self, key: &str) -> Result<Option<String>> {
        // Fetch from cache backend
        Ok(Some("cached_value".to_string()))
    }

    async fn set(&self, key: &str, value: String, ttl: Option<Duration>) -> Result<()> {
        // Store in cache backend
        Ok(())
    }

    async fn invalidate(&self, key: &str) -> Result<()> {
        // Remove from cache
        Ok(())
    }

    async fn exists(&self, key: &str) -> Result<bool> {
        // Check existence
        Ok(true)
    }
}

Required Methods§

Source

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

Get value from cache

§Arguments
  • key - Cache key (URL, idempotency key, etc.)
§Returns
  • Ok(Some(String)) - Cache hit
  • Ok(None) - Cache miss
  • Err(CacheError) - Backend failure
§Example
match cache.get("page:123").await {
    Ok(Some(content)) => println!("Cache hit: {}", content),
    Ok(None) => println!("Cache miss"),
    Err(e) => eprintln!("Cache error: {}", e),
}
Source

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

Set value in cache with optional TTL

§Arguments
  • key - Cache key
  • value - Value to store (JSON, HTML, etc.)
  • ttl - Optional expiration duration
§Returns
  • Ok(()) - Successfully stored
  • Err(CacheError) - Backend failure
§Example
cache.set(
    "page:123",
    "<html>...</html>".to_string(),
    Some(Duration::from_secs(3600))
).await.unwrap();
Source

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

Invalidate cache entry

§Arguments
  • key - Cache key to invalidate
§Returns
  • Ok(()) - Successfully invalidated (or key didn’t exist)
  • Err(CacheError) - Backend failure
§Example
cache.invalidate("page:123").await.unwrap();
Source

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

Check if key exists in cache

§Arguments
  • key - Cache key to check
§Returns
  • Ok(true) - Key exists
  • Ok(false) - Key does not exist or expired
  • Err(CacheError) - Backend failure
§Example
if cache.exists("page:123").await.unwrap() {
    println!("Page is cached");
}

Implementors§