use std::collections::HashMap;
use cachet::{Cache, CacheEntry, CacheOperation, CacheResponse, GetRequest, InsertRequest, InvalidateRequest};
use layered::Service;
use tick::Clock;
#[derive(Clone, Default)]
struct RemoteCache {
data: HashMap<String, CacheEntry<String>>,
}
impl Service<CacheOperation<String, String>> for RemoteCache {
type Out = Result<CacheResponse<String>, cachet::Error>;
async fn execute(&self, input: CacheOperation<String, String>) -> Self::Out {
match input {
CacheOperation::Get(GetRequest { key }) => Ok(CacheResponse::Get(self.data.get(&key).cloned())),
CacheOperation::Insert(InsertRequest { key, entry }) => {
let _ = (key, entry);
Ok(CacheResponse::Insert)
}
CacheOperation::Invalidate(InvalidateRequest { key }) => {
let _ = key;
Ok(CacheResponse::Invalidate)
}
CacheOperation::Clear => Ok(CacheResponse::Clear),
}
}
}
#[tokio::main]
async fn main() {
let clock = Clock::new_tokio();
let cache = Cache::builder::<String, String>(clock).service(RemoteCache::default()).build();
cache
.insert("key".to_string(), CacheEntry::new("value".to_string()))
.await
.expect("insert failed");
let value = cache.get(&"key".to_string()).await.expect("get failed");
match value {
Some(e) => println!("get(key): {}", e.value()),
None => println!("get(key): not found"),
}
}