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§
Sourcefn 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 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 hitOk(None)- Cache missErr(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),
}Sourcefn 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 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 keyvalue- Value to store (JSON, HTML, etc.)ttl- Optional expiration duration
§Returns
Ok(())- Successfully storedErr(CacheError)- Backend failure
§Example
cache.set(
"page:123",
"<html>...</html>".to_string(),
Some(Duration::from_secs(3600))
).await.unwrap();