use crate::adapters::redis::RedisClient;
use redis::AsyncCommands;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct SubmissionCache {
redis: Arc<RedisClient>,
prefix: String,
}
impl SubmissionCache {
#[must_use]
pub fn new(redis: Arc<RedisClient>) -> Self {
Self { redis, prefix: "idempotency:submission:".to_string() }
}
pub async fn get_response(&self, key: &str) -> anyhow::Result<Option<Vec<u8>>> {
let mut conn = self.redis.publisher();
let full_key = format!("{}{key}", self.prefix);
let response: Option<Vec<u8>> = conn.get(full_key).await?;
Ok(response)
}
pub async fn save_response(&self, key: &str, response: &[u8], ttl_secs: u64) -> anyhow::Result<()> {
let mut conn = self.redis.publisher();
let full_key = format!("{}{key}", self.prefix);
let _: () = conn.set_ex(full_key, response, ttl_secs).await?;
Ok(())
}
}