oxirs_vec/adaptive_intelligent_caching/
storage.rs

1//! Cache storage implementations
2
3use anyhow::Result;
4use std::collections::HashMap;
5use std::time::Duration;
6
7use super::types::{CacheKey, CacheSizeInfo, CacheValue, StorageStatistics};
8
9/// Cache storage trait for different storage implementations
10pub trait CacheStorage: Send + Sync + std::fmt::Debug {
11    /// Store an item in the cache
12    fn store(&mut self, key: CacheKey, value: CacheValue, ttl: Option<Duration>) -> Result<()>;
13
14    /// Retrieve an item from the cache
15    fn retrieve(&self, key: &CacheKey) -> Option<CacheValue>;
16
17    /// Remove an item from the cache
18    fn remove(&mut self, key: &CacheKey) -> bool;
19
20    /// Get cache size information
21    fn size_info(&self) -> CacheSizeInfo;
22
23    /// Clear the entire cache
24    fn clear(&mut self);
25
26    /// Get storage-specific statistics
27    fn statistics(&self) -> StorageStatistics;
28}
29
30/// In-memory cache storage
31#[derive(Debug)]
32pub struct MemoryStorage {
33    data: HashMap<CacheKey, CacheValue>,
34    max_size: u64,
35    current_size: u64,
36}
37
38impl MemoryStorage {
39    pub fn new(max_size: u64) -> Self {
40        Self {
41            data: HashMap::new(),
42            max_size,
43            current_size: 0,
44        }
45    }
46}
47
48impl CacheStorage for MemoryStorage {
49    fn store(&mut self, key: CacheKey, value: CacheValue, _ttl: Option<Duration>) -> Result<()> {
50        let size = value.metadata.size_bytes;
51        if self.current_size + size <= self.max_size {
52            self.data.insert(key, value);
53            self.current_size += size;
54        }
55        Ok(())
56    }
57
58    fn retrieve(&self, key: &CacheKey) -> Option<CacheValue> {
59        self.data.get(key).cloned()
60    }
61
62    fn remove(&mut self, key: &CacheKey) -> bool {
63        if let Some(value) = self.data.remove(key) {
64            self.current_size -= value.metadata.size_bytes;
65            true
66        } else {
67            false
68        }
69    }
70
71    fn size_info(&self) -> CacheSizeInfo {
72        CacheSizeInfo {
73            used_bytes: self.current_size,
74            available_bytes: self.max_size - self.current_size,
75            total_capacity_bytes: self.max_size,
76            item_count: self.data.len() as u64,
77        }
78    }
79
80    fn clear(&mut self) {
81        self.data.clear();
82        self.current_size = 0;
83    }
84
85    fn statistics(&self) -> StorageStatistics {
86        StorageStatistics::default()
87    }
88}
89
90/// Compressed cache storage (wraps MemoryStorage with compression)
91#[derive(Debug)]
92pub struct CompressedStorage {
93    inner: MemoryStorage,
94}
95
96impl CompressedStorage {
97    pub fn new(max_size: u64) -> Self {
98        Self {
99            inner: MemoryStorage::new(max_size),
100        }
101    }
102}
103
104impl CacheStorage for CompressedStorage {
105    fn store(&mut self, key: CacheKey, value: CacheValue, ttl: Option<Duration>) -> Result<()> {
106        // In a real implementation, this would compress the value
107        self.inner.store(key, value, ttl)
108    }
109
110    fn retrieve(&self, key: &CacheKey) -> Option<CacheValue> {
111        // In a real implementation, this would decompress the value
112        self.inner.retrieve(key)
113    }
114
115    fn remove(&mut self, key: &CacheKey) -> bool {
116        self.inner.remove(key)
117    }
118
119    fn size_info(&self) -> CacheSizeInfo {
120        self.inner.size_info()
121    }
122
123    fn clear(&mut self) {
124        self.inner.clear()
125    }
126
127    fn statistics(&self) -> StorageStatistics {
128        self.inner.statistics()
129    }
130}
131
132/// Persistent cache storage (disk-backed)
133#[derive(Debug)]
134pub struct PersistentStorage {
135    inner: MemoryStorage,
136}
137
138impl PersistentStorage {
139    pub fn new(max_size: u64) -> Result<Self> {
140        Ok(Self {
141            inner: MemoryStorage::new(max_size),
142        })
143    }
144}
145
146impl CacheStorage for PersistentStorage {
147    fn store(&mut self, key: CacheKey, value: CacheValue, ttl: Option<Duration>) -> Result<()> {
148        // In a real implementation, this would persist to disk
149        self.inner.store(key, value, ttl)
150    }
151
152    fn retrieve(&self, key: &CacheKey) -> Option<CacheValue> {
153        // In a real implementation, this would load from disk if not in memory
154        self.inner.retrieve(key)
155    }
156
157    fn remove(&mut self, key: &CacheKey) -> bool {
158        self.inner.remove(key)
159    }
160
161    fn size_info(&self) -> CacheSizeInfo {
162        self.inner.size_info()
163    }
164
165    fn clear(&mut self) {
166        self.inner.clear()
167    }
168
169    fn statistics(&self) -> StorageStatistics {
170        self.inner.statistics()
171    }
172}