ayun_cache/support/
cache.rs1use crate::{config, support::drivers, traits::CacheDriver, Cache, CacheResult};
2
3impl Cache {
4 pub fn new(driver: Box<dyn CacheDriver>, config: config::Cache) -> Self {
5 Self { driver, config }
6 }
7
8 pub fn try_from_config(config: config::Cache) -> CacheResult<Self> {
9 let driver = match config.driver {
10 config::Driver::Null => drivers::null::new(),
11 #[cfg(feature = "memory")]
12 config::Driver::Memory => drivers::mem::new(),
13 };
14
15 Ok(Self::new(driver, config))
16 }
17
18 pub fn config(self) -> config::Cache {
19 self.config
20 }
21
22 pub async fn has(&self, key: &str) -> CacheResult<bool> {
23 self.driver.has(key).await
24 }
25
26 pub async fn get(&self, key: &str) -> CacheResult<Option<String>> {
27 self.driver.get(key).await
28 }
29
30 pub async fn insert(&self, key: &str, value: &str) -> CacheResult<()> {
31 self.driver.insert(key, value).await
32 }
33
34 pub async fn remove(&self, key: &str) -> CacheResult<()> {
35 self.driver.remove(key).await
36 }
37
38 pub async fn clear(&self) -> CacheResult<()> {
39 self.driver.clear().await
40 }
41}
42
43impl std::ops::Deref for Cache {
44 type Target = Box<dyn CacheDriver>;
45
46 fn deref(&self) -> &Self::Target {
47 &self.driver
48 }
49}