stasis/infrastructure/runtime/
in_memory_ai_chat_response_cache.rs1use std::collections::HashMap;
2use std::sync::{Arc, RwLock};
3
4use genai::chat::ChatResponse;
5
6use crate::ports::outbound::ai_chat_response_cache::AiChatResponseCache;
7
8#[derive(Clone, Default)]
9pub struct InMemoryAiChatResponseCache {
10 state: Arc<RwLock<HashMap<String, ChatResponse>>>,
11}
12
13impl AiChatResponseCache for InMemoryAiChatResponseCache {
14 fn get(&self, key: &str) -> Option<ChatResponse> {
15 self.state
16 .read()
17 .ok()
18 .and_then(|state| state.get(key).cloned())
19 }
20
21 fn set(&self, key: &str, response: ChatResponse) {
22 if let Ok(mut state) = self.state.write() {
23 state.insert(key.to_string(), response);
24 }
25 }
26}