use crate::error::StorageError;
use async_trait::async_trait;
use std::time::Duration;
#[async_trait]
pub trait CacheService: Send + Sync {
async fn get(&self, key: &str) -> Result<Option<String>, StorageError>;
async fn set(&self, key: &str, value: &str, ttl: Option<u64>) -> Result<(), StorageError>;
async fn delete(&self, key: &str) -> Result<(), StorageError>;
async fn set_with_ttl(&self, key: &str, value: &str, ttl: Duration)
-> Result<(), StorageError>;
}
#[cfg(test)]
mod tests {
use super::*;
use async_trait::async_trait;
struct MockCacheService;
#[async_trait]
impl CacheService for MockCacheService {
async fn get(&self, _key: &str) -> Result<Option<String>, StorageError> {
Ok(Some("test_value".to_string()))
}
async fn set(
&self,
_key: &str,
_value: &str,
_ttl: Option<u64>,
) -> Result<(), StorageError> {
Ok(())
}
async fn delete(&self, _key: &str) -> Result<(), StorageError> {
Ok(())
}
async fn set_with_ttl(
&self,
_key: &str,
_value: &str,
_ttl: Duration,
) -> Result<(), StorageError> {
Ok(())
}
}
#[tokio::test]
async fn test_mock_cache_service() {
let cache = MockCacheService;
assert!(cache.get("test").await.is_ok());
assert!(cache.set("test", "value", None).await.is_ok());
assert!(cache.delete("test").await.is_ok());
assert!(
cache
.set_with_ttl("test", "value", Duration::from_secs(60))
.await
.is_ok()
);
}
}