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