Skip to content

Commit 54e7894

Browse files
authored
refactor: Cache instead of Arc<Cache> in public APIs (#433)
1 parent 49db251 commit 54e7894

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

cot/src/cache.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,13 @@ pub type CacheResult<T> = Result<T, CacheError>;
191191
/// ```
192192
#[derive(Debug, Clone)]
193193
pub struct Cache {
194+
inner: Arc<CacheImpl>,
195+
}
196+
197+
#[derive(Debug)]
198+
struct CacheImpl {
194199
#[debug("..")]
195-
store: Arc<dyn BoxCacheStore>,
200+
store: Box<dyn BoxCacheStore>,
196201
prefix: Option<String>,
197202
expiry: Timeout,
198203
}
@@ -219,17 +224,19 @@ impl Cache {
219224
/// );
220225
/// ```
221226
pub fn new(store: impl CacheStore, prefix: Option<String>, expiry: Timeout) -> Self {
222-
let store: Arc<dyn BoxCacheStore> = Arc::new(store);
227+
let store: Box<dyn BoxCacheStore> = Box::new(store);
223228
Self {
224-
store,
225-
prefix,
226-
expiry,
229+
inner: Arc::new(CacheImpl {
230+
store,
231+
prefix,
232+
expiry,
233+
}),
227234
}
228235
}
229236

230237
fn format_key<K: AsRef<str>>(&self, key: K) -> String {
231238
let k = key.as_ref();
232-
if let Some(pref) = &self.prefix {
239+
if let Some(pref) = &self.inner.prefix {
233240
return format!("{pref}:{k}");
234241
}
235242
k.to_string()
@@ -276,6 +283,7 @@ impl Cache {
276283
{
277284
let k = self.format_key(key.as_ref());
278285
let result = self
286+
.inner
279287
.store
280288
.get(&k)
281289
.await?
@@ -334,8 +342,9 @@ impl Cache {
334342
V: Serialize,
335343
{
336344
let k = self.format_key(key.into());
337-
self.store
338-
.insert(k, serde_json::to_value(value)?, self.expiry)
345+
self.inner
346+
.store
347+
.insert(k, serde_json::to_value(value)?, self.inner.expiry)
339348
.await?;
340349
Ok(())
341350
}
@@ -387,7 +396,8 @@ impl Cache {
387396
V: Serialize,
388397
{
389398
let k = self.format_key(key.into());
390-
self.store
399+
self.inner
400+
.store
391401
.insert(k, serde_json::to_value(value)?, expiry)
392402
.await?;
393403
Ok(())
@@ -424,7 +434,7 @@ impl Cache {
424434
/// ```
425435
pub async fn remove<K: AsRef<str>>(&self, key: K) -> CacheResult<()> {
426436
let k = self.format_key(key.as_ref());
427-
self.store.remove(&k).await?;
437+
self.inner.store.remove(&k).await?;
428438
Ok(())
429439
}
430440

@@ -465,7 +475,7 @@ impl Cache {
465475
/// # }
466476
/// ```
467477
pub async fn clear(&self) -> CacheResult<()> {
468-
self.store.clear().await?;
478+
self.inner.store.clear().await?;
469479
Ok(())
470480
}
471481

@@ -505,7 +515,7 @@ impl Cache {
505515
/// # }
506516
/// ```
507517
pub async fn approx_size(&self) -> CacheResult<usize> {
508-
let result = self.store.approx_size().await?;
518+
let result = self.inner.store.approx_size().await?;
509519
Ok(result)
510520
}
511521

@@ -542,7 +552,7 @@ impl Cache {
542552
/// ```
543553
pub async fn contains_key<K: AsRef<str>>(&self, key: K) -> CacheResult<bool> {
544554
let k = self.format_key(key.as_ref());
545-
let result = self.store.contains_key(&k).await?;
555+
let result = self.inner.store.contains_key(&k).await?;
546556
Ok(result)
547557
}
548558

cot/src/project.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,8 +1334,8 @@ impl Bootstrapper<WithDatabase> {
13341334
}
13351335

13361336
#[cfg(feature = "cache")]
1337-
async fn init_cache(config: &CacheConfig) -> cot::Result<Arc<Cache>> {
1338-
let cache = Cache::from_config(config).await.map(Arc::new)?;
1337+
async fn init_cache(config: &CacheConfig) -> cot::Result<Cache> {
1338+
let cache = Cache::from_config(config).await?;
13391339
Ok(cache)
13401340
}
13411341
}
@@ -1652,7 +1652,7 @@ impl BootstrapPhase for WithCache {
16521652
type Database = Option<Arc<Database>>;
16531653
type AuthBackend = <WithApps as BootstrapPhase>::AuthBackend;
16541654
#[cfg(feature = "cache")]
1655-
type Cache = Arc<Cache>;
1655+
type Cache = Cache;
16561656
}
16571657

16581658
/// The final phase of bootstrapping a Cot project, the initialized phase.
@@ -1808,7 +1808,7 @@ impl ProjectContext<WithApps> {
18081808

18091809
impl ProjectContext<WithDatabase> {
18101810
#[must_use]
1811-
fn with_cache(self, #[cfg(feature = "cache")] cache: Arc<Cache>) -> ProjectContext<WithCache> {
1811+
fn with_cache(self, #[cfg(feature = "cache")] cache: Cache) -> ProjectContext<WithCache> {
18121812
ProjectContext {
18131813
config: self.config,
18141814
apps: self.apps,
@@ -1908,7 +1908,7 @@ impl<S: BootstrapPhase<AuthBackend = Arc<dyn AuthBackend>>> ProjectContext<S> {
19081908
}
19091909

19101910
#[cfg(feature = "cache")]
1911-
impl<S: BootstrapPhase<Cache = Arc<Cache>>> ProjectContext<S> {
1911+
impl<S: BootstrapPhase<Cache = Cache>> ProjectContext<S> {
19121912
/// Returns the cache for the project.
19131913
///
19141914
/// # Examples
@@ -1925,7 +1925,7 @@ impl<S: BootstrapPhase<Cache = Arc<Cache>>> ProjectContext<S> {
19251925
/// ```
19261926
#[must_use]
19271927
#[cfg(feature = "cache")]
1928-
pub fn cache(&self) -> &Arc<Cache> {
1928+
pub fn cache(&self) -> &Cache {
19291929
&self.cache
19301930
}
19311931
}
@@ -2521,11 +2521,11 @@ mod tests {
25212521

25222522
#[cot::test]
25232523
async fn default_auth_backend() {
2524-
let cache_memory = Arc::new(Cache::new(
2524+
let cache_memory = Cache::new(
25252525
cache::store::memory::Memory::new(),
25262526
None,
25272527
Timeout::default(),
2528-
));
2528+
);
25292529

25302530
let context = ProjectContext::new()
25312531
.with_config(

cot/src/test.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub struct TestRequestBuilder {
233233
json_data: Option<String>,
234234
static_files: Vec<StaticFile>,
235235
#[cfg(feature = "cache")]
236-
cache: Option<Arc<Cache>>,
236+
cache: Option<Cache>,
237237
}
238238

239239
/// A wrapper over an auth backend that is cloneable.
@@ -774,7 +774,7 @@ impl TestRequestBuilder {
774774
#[cfg(feature = "cache")]
775775
self.cache
776776
.clone()
777-
.unwrap_or_else(|| Arc::new(Cache::new(Memory::new(), None, Timeout::default()))),
777+
.unwrap_or_else(|| Cache::new(Memory::new(), None, Timeout::default())),
778778
);
779779
prepare_request(&mut request, Arc::new(context));
780780

@@ -1776,17 +1776,14 @@ enum CacheKind {
17761776
#[cfg(feature = "cache")]
17771777
#[derive(Debug, Clone)]
17781778
pub struct TestCache {
1779-
cache: Arc<Cache>,
1779+
cache: Cache,
17801780
kind: CacheKind,
17811781
}
17821782

17831783
#[cfg(feature = "cache")]
17841784
impl TestCache {
17851785
fn new(cache: Cache, kind: CacheKind) -> Self {
1786-
Self {
1787-
cache: Arc::new(cache),
1788-
kind,
1789-
}
1786+
Self { cache, kind }
17901787
}
17911788

17921789
/// Create a new in-memory test cache.
@@ -1905,7 +1902,7 @@ impl TestCache {
19051902
/// # }
19061903
/// ```
19071904
#[must_use]
1908-
pub fn cache(&self) -> Arc<Cache> {
1905+
pub fn cache(&self) -> Cache {
19091906
self.cache.clone()
19101907
}
19111908

0 commit comments

Comments
 (0)