1pub mod db_loader;
8
9#[cfg(feature = "l1-moka")]
10pub mod l1;
11
12#[cfg(feature = "l2-redis")]
13pub mod l2;
14
15#[cfg(all(feature = "l1-moka", feature = "l2-redis"))]
16pub mod two_level;
17
18#[cfg(feature = "redis-native")]
19pub mod redis_native;
20
21#[cfg(feature = "ttl-control")]
22pub mod ttl_control;
23
24#[cfg(feature = "tiered-cache")]
25pub mod tiered_cache;
26
27use crate::error::Result;
28use async_trait::async_trait;
29use std::any::Any;
30
31use crate::serialization::Serializer;
32use serde::{de::DeserializeOwned, Serialize};
33use tracing::instrument;
34
35#[async_trait]
39pub trait CacheExt: CacheOps {
40 #[instrument(skip(self), level = "debug")]
42 async fn get<T: DeserializeOwned + Send>(&self, key: &str) -> Result<Option<T>> {
43 let bytes = self.get_bytes(key).await?;
44 match bytes {
45 Some(data) => {
46 let val = self.serializer().deserialize(&data)?;
47 Ok(Some(val))
48 }
49 None => Ok(None),
50 }
51 }
52
53 #[instrument(skip(self, value), level = "debug")]
55 async fn set<T: Serialize + Send + Sync>(
56 &self,
57 key: &str,
58 value: &T,
59 ttl: Option<u64>,
60 ) -> Result<()> {
61 let bytes = self.serializer().serialize(value)?;
62 self.set_bytes(key, bytes, ttl).await
63 }
64
65 #[instrument(skip(self, value), level = "debug")]
67 async fn set_l1_only<T: Serialize + Send + Sync>(
68 &self,
69 key: &str,
70 value: &T,
71 ttl: Option<u64>,
72 ) -> Result<()> {
73 let bytes = self.serializer().serialize(value)?;
74 self.set_l1_bytes(key, bytes, ttl).await
75 }
76
77 #[instrument(skip(self, value), level = "debug")]
78 async fn set_l2_only<T: Serialize + Send + Sync>(
79 &self,
80 key: &str,
81 value: &T,
82 ttl: Option<u64>,
83 ) -> Result<()> {
84 let bytes = self.serializer().serialize(value)?;
85 self.set_l2_bytes(key, bytes, ttl).await
86 }
87
88 #[instrument(skip(self, fetch), level = "debug")]
110 async fn get_or_fetch<T, F, Fut>(&self, key: &str, ttl: Option<u64>, fetch: F) -> Result<T>
111 where
112 T: DeserializeOwned + Serialize + Send + Sync + Clone,
113 F: FnOnce() -> Fut + Send,
114 Fut: std::future::Future<Output = Result<T>> + Send,
115 {
116 if let Some(cached) = self.get::<T>(key).await? {
118 return Ok(cached);
119 }
120
121 let value = fetch().await?;
123
124 self.set(key, &value, ttl).await?;
126
127 Ok(value)
128 }
129
130 #[instrument(skip(self), level = "debug")]
134 async fn try_get<T: DeserializeOwned + Send>(&self, key: &str) -> Result<Option<T>> {
135 self.get(key).await
136 }
137
138 #[instrument(skip(self), level = "debug")]
148 async fn remove<T: DeserializeOwned + Send>(&self, key: &str) -> Result<Option<T>> {
149 let old_value = self.get::<T>(key).await?;
150 self.delete(key).await?;
151 Ok(old_value)
152 }
153
154 #[instrument(skip(self), level = "debug")]
164 async fn contains(&self, key: &str) -> Result<bool> {
165 Ok(self.get_bytes(key).await?.is_some())
166 }
167}
168
169impl<T: CacheOps + ?Sized> CacheExt for T {}
170
171#[async_trait]
175pub trait CacheOps: Send + Sync + Any {
176 async fn get_bytes(&self, key: &str) -> Result<Option<Vec<u8>>>;
186
187 async fn get_l1_bytes(&self, _key: &str) -> Result<Option<Vec<u8>>> {
189 Err(crate::error::CacheError::NotSupported(
190 "get_l1_bytes".to_string(),
191 ))
192 }
193
194 async fn get_l2_bytes(&self, _key: &str) -> Result<Option<Vec<u8>>> {
196 Err(crate::error::CacheError::NotSupported(
197 "get_l2_bytes".to_string(),
198 ))
199 }
200
201 async fn set_bytes(&self, key: &str, value: Vec<u8>, ttl: Option<u64>) -> Result<()>;
213
214 async fn set_l1_bytes(&self, _key: &str, _value: Vec<u8>, _ttl: Option<u64>) -> Result<()> {
216 Err(crate::error::CacheError::NotSupported(
217 "set_l1_bytes".to_string(),
218 ))
219 }
220
221 async fn set_l2_bytes(&self, _key: &str, _value: Vec<u8>, _ttl: Option<u64>) -> Result<()> {
223 Err(crate::error::CacheError::NotSupported(
224 "set_l2_bytes".to_string(),
225 ))
226 }
227
228 async fn lock(&self, _key: &str, _ttl: u64) -> Result<Option<String>> {
243 Ok(None)
244 }
245
246 async fn unlock(&self, _key: &str, _value: &str) -> Result<bool> {
257 Ok(false)
258 }
259
260 async fn delete(&self, key: &str) -> Result<()>;
270
271 fn serializer(&self) -> &crate::serialization::SerializerEnum;
275
276 fn as_any(&self) -> &dyn Any;
278
279 fn into_any_arc(self: std::sync::Arc<Self>) -> std::sync::Arc<dyn Any + Send + Sync>;
281
282 async fn clear_l1(&self) -> Result<()> {
288 Err(crate::error::CacheError::NotSupported(
289 "clear_l1".to_string(),
290 ))
291 }
292
293 async fn clear_l2(&self) -> Result<()> {
299 Err(crate::error::CacheError::NotSupported(
300 "clear_l2".to_string(),
301 ))
302 }
303
304 async fn clear_wal(&self) -> Result<()> {
310 Err(crate::error::CacheError::NotSupported(
311 "clear_wal".to_string(),
312 ))
313 }
314
315 async fn shutdown(&self) -> Result<()> {
319 Ok(())
320 }
321}