1pub mod db_loader;
8pub mod l1;
9pub mod l2;
10pub mod two_level;
11
12use crate::error::Result;
13use async_trait::async_trait;
14use std::any::Any;
15
16use crate::serialization::Serializer;
17use serde::{de::DeserializeOwned, Serialize};
18use tracing::instrument;
19
20#[async_trait]
24pub trait CacheExt: CacheOps {
25 #[instrument(skip(self), level = "debug")]
27 async fn get<T: DeserializeOwned + Send>(&self, key: &str) -> Result<Option<T>> {
28 let bytes = self.get_bytes(key).await?;
29 match bytes {
30 Some(data) => {
31 let val = self.serializer().deserialize(&data)?;
32 Ok(Some(val))
33 }
34 None => Ok(None),
35 }
36 }
37
38 #[instrument(skip(self, value), level = "debug")]
40 async fn set<T: Serialize + Send + Sync>(
41 &self,
42 key: &str,
43 value: &T,
44 ttl: Option<u64>,
45 ) -> Result<()> {
46 let bytes = self.serializer().serialize(value)?;
47 self.set_bytes(key, bytes, ttl).await
48 }
49
50 #[instrument(skip(self, value), level = "debug")]
70 async fn set_l1_only<T: Serialize + Send + Sync>(
71 &self,
72 key: &str,
73 value: &T,
74 ttl: Option<u64>,
75 ) -> Result<()> {
76 let bytes = self.serializer().serialize(value)?;
77 self.set_l1_bytes(key, bytes, ttl).await
78 }
79
80 #[instrument(skip(self, value), level = "debug")]
81 async fn set_l2_only<T: Serialize + Send + Sync>(
82 &self,
83 key: &str,
84 value: &T,
85 ttl: Option<u64>,
86 ) -> Result<()> {
87 let bytes = self.serializer().serialize(value)?;
88 self.set_l2_bytes(key, bytes, ttl).await
89 }
90}
91
92impl<T: CacheOps + ?Sized> CacheExt for T {}
93
94#[async_trait]
98pub trait CacheOps: Send + Sync + Any {
99 async fn get_bytes(&self, key: &str) -> Result<Option<Vec<u8>>>;
109
110 async fn get_l1_bytes(&self, _key: &str) -> Result<Option<Vec<u8>>> {
112 Err(crate::error::CacheError::NotSupported(
113 "get_l1_bytes".to_string(),
114 ))
115 }
116
117 async fn get_l2_bytes(&self, _key: &str) -> Result<Option<Vec<u8>>> {
119 Err(crate::error::CacheError::NotSupported(
120 "get_l2_bytes".to_string(),
121 ))
122 }
123
124 async fn set_bytes(&self, key: &str, value: Vec<u8>, ttl: Option<u64>) -> Result<()>;
136
137 async fn set_l1_bytes(&self, _key: &str, _value: Vec<u8>, _ttl: Option<u64>) -> Result<()> {
139 Err(crate::error::CacheError::NotSupported(
140 "set_l1_bytes".to_string(),
141 ))
142 }
143
144 async fn set_l2_bytes(&self, _key: &str, _value: Vec<u8>, _ttl: Option<u64>) -> Result<()> {
146 Err(crate::error::CacheError::NotSupported(
147 "set_l2_bytes".to_string(),
148 ))
149 }
150
151 async fn lock(&self, _key: &str, _value: &str, _ttl: u64) -> Result<bool> {
163 Ok(false)
164 }
165
166 async fn unlock(&self, _key: &str, _value: &str) -> Result<bool> {
177 Ok(false)
178 }
179
180 async fn delete(&self, key: &str) -> Result<()>;
190
191 fn serializer(&self) -> &crate::serialization::SerializerEnum;
195
196 fn as_any(&self) -> &dyn Any;
198
199 fn into_any_arc(self: std::sync::Arc<Self>) -> std::sync::Arc<dyn Any + Send + Sync>;
201
202 async fn clear_l1(&self) -> Result<()> {
208 Err(crate::error::CacheError::NotSupported(
209 "clear_l1".to_string(),
210 ))
211 }
212
213 async fn clear_l2(&self) -> Result<()> {
219 Err(crate::error::CacheError::NotSupported(
220 "clear_l2".to_string(),
221 ))
222 }
223
224 async fn clear_wal(&self) -> Result<()> {
230 Err(crate::error::CacheError::NotSupported(
231 "clear_wal".to_string(),
232 ))
233 }
234
235 async fn shutdown(&self) -> Result<()> {
239 Ok(())
240 }
241}