diff --git a/cot/src/cache.rs b/cot/src/cache.rs index b06d88f9..c1f282e7 100644 --- a/cot/src/cache.rs +++ b/cot/src/cache.rs @@ -191,8 +191,13 @@ pub type CacheResult = Result; /// ``` #[derive(Debug, Clone)] pub struct Cache { + inner: Arc, +} + +#[derive(Debug)] +struct CacheImpl { #[debug("..")] - store: Arc, + store: Box, prefix: Option, expiry: Timeout, } @@ -219,17 +224,19 @@ impl Cache { /// ); /// ``` pub fn new(store: impl CacheStore, prefix: Option, expiry: Timeout) -> Self { - let store: Arc = Arc::new(store); + let store: Box = Box::new(store); Self { - store, - prefix, - expiry, + inner: Arc::new(CacheImpl { + store, + prefix, + expiry, + }), } } fn format_key>(&self, key: K) -> String { let k = key.as_ref(); - if let Some(pref) = &self.prefix { + if let Some(pref) = &self.inner.prefix { return format!("{pref}:{k}"); } k.to_string() @@ -276,6 +283,7 @@ impl Cache { { let k = self.format_key(key.as_ref()); let result = self + .inner .store .get(&k) .await? @@ -334,8 +342,9 @@ impl Cache { V: Serialize, { let k = self.format_key(key.into()); - self.store - .insert(k, serde_json::to_value(value)?, self.expiry) + self.inner + .store + .insert(k, serde_json::to_value(value)?, self.inner.expiry) .await?; Ok(()) } @@ -387,7 +396,8 @@ impl Cache { V: Serialize, { let k = self.format_key(key.into()); - self.store + self.inner + .store .insert(k, serde_json::to_value(value)?, expiry) .await?; Ok(()) @@ -424,7 +434,7 @@ impl Cache { /// ``` pub async fn remove>(&self, key: K) -> CacheResult<()> { let k = self.format_key(key.as_ref()); - self.store.remove(&k).await?; + self.inner.store.remove(&k).await?; Ok(()) } @@ -465,7 +475,7 @@ impl Cache { /// # } /// ``` pub async fn clear(&self) -> CacheResult<()> { - self.store.clear().await?; + self.inner.store.clear().await?; Ok(()) } @@ -505,7 +515,7 @@ impl Cache { /// # } /// ``` pub async fn approx_size(&self) -> CacheResult { - let result = self.store.approx_size().await?; + let result = self.inner.store.approx_size().await?; Ok(result) } @@ -542,7 +552,7 @@ impl Cache { /// ``` pub async fn contains_key>(&self, key: K) -> CacheResult { let k = self.format_key(key.as_ref()); - let result = self.store.contains_key(&k).await?; + let result = self.inner.store.contains_key(&k).await?; Ok(result) } diff --git a/cot/src/project.rs b/cot/src/project.rs index 50c3675d..de22de50 100644 --- a/cot/src/project.rs +++ b/cot/src/project.rs @@ -1334,8 +1334,8 @@ impl Bootstrapper { } #[cfg(feature = "cache")] - async fn init_cache(config: &CacheConfig) -> cot::Result> { - let cache = Cache::from_config(config).await.map(Arc::new)?; + async fn init_cache(config: &CacheConfig) -> cot::Result { + let cache = Cache::from_config(config).await?; Ok(cache) } } @@ -1652,7 +1652,7 @@ impl BootstrapPhase for WithCache { type Database = Option>; type AuthBackend = ::AuthBackend; #[cfg(feature = "cache")] - type Cache = Arc; + type Cache = Cache; } /// The final phase of bootstrapping a Cot project, the initialized phase. @@ -1808,7 +1808,7 @@ impl ProjectContext { impl ProjectContext { #[must_use] - fn with_cache(self, #[cfg(feature = "cache")] cache: Arc) -> ProjectContext { + fn with_cache(self, #[cfg(feature = "cache")] cache: Cache) -> ProjectContext { ProjectContext { config: self.config, apps: self.apps, @@ -1908,7 +1908,7 @@ impl>> ProjectContext { } #[cfg(feature = "cache")] -impl>> ProjectContext { +impl> ProjectContext { /// Returns the cache for the project. /// /// # Examples @@ -1925,7 +1925,7 @@ impl>> ProjectContext { /// ``` #[must_use] #[cfg(feature = "cache")] - pub fn cache(&self) -> &Arc { + pub fn cache(&self) -> &Cache { &self.cache } } @@ -2521,11 +2521,11 @@ mod tests { #[cot::test] async fn default_auth_backend() { - let cache_memory = Arc::new(Cache::new( + let cache_memory = Cache::new( cache::store::memory::Memory::new(), None, Timeout::default(), - )); + ); let context = ProjectContext::new() .with_config( diff --git a/cot/src/test.rs b/cot/src/test.rs index ad1bea50..1b935dba 100644 --- a/cot/src/test.rs +++ b/cot/src/test.rs @@ -233,7 +233,7 @@ pub struct TestRequestBuilder { json_data: Option, static_files: Vec, #[cfg(feature = "cache")] - cache: Option>, + cache: Option, } /// A wrapper over an auth backend that is cloneable. @@ -774,7 +774,7 @@ impl TestRequestBuilder { #[cfg(feature = "cache")] self.cache .clone() - .unwrap_or_else(|| Arc::new(Cache::new(Memory::new(), None, Timeout::default()))), + .unwrap_or_else(|| Cache::new(Memory::new(), None, Timeout::default())), ); prepare_request(&mut request, Arc::new(context)); @@ -1776,17 +1776,14 @@ enum CacheKind { #[cfg(feature = "cache")] #[derive(Debug, Clone)] pub struct TestCache { - cache: Arc, + cache: Cache, kind: CacheKind, } #[cfg(feature = "cache")] impl TestCache { fn new(cache: Cache, kind: CacheKind) -> Self { - Self { - cache: Arc::new(cache), - kind, - } + Self { cache, kind } } /// Create a new in-memory test cache. @@ -1905,7 +1902,7 @@ impl TestCache { /// # } /// ``` #[must_use] - pub fn cache(&self) -> Arc { + pub fn cache(&self) -> Cache { self.cache.clone() }