kutil_http/cache/implementation/moka/
cache.rs

1use super::super::super::{cache::*, key::*, response::*};
2
3use std::{ops::*, sync::*};
4
5//
6// MokaCacheImplementation
7//
8
9/// Moka cache implementation.
10///
11/// Note that it is based on the `sync` version of Moka cache rather than its `future` version.
12///
13/// The reason is that our `get` is not `async`, and unfortunately the `future` version of Moka
14/// does not have a non-`async` version of its `get`.
15pub type MokaCacheImplementation<CacheKeyT = CommonCacheKey> = Arc<moka::future::Cache<CacheKeyT, CachedResponseRef>>;
16
17impl<CacheKeyT> Cache<CacheKeyT> for MokaCacheImplementation<CacheKeyT>
18where
19    CacheKeyT: CacheKey,
20{
21    async fn get(&self, key: &CacheKeyT) -> Option<CachedResponseRef> {
22        self.deref().get(key).await
23    }
24
25    async fn put(&self, key: CacheKeyT, cached_response: CachedResponseRef) {
26        self.deref().insert(key, cached_response).await
27    }
28
29    async fn invalidate(&self, key: &CacheKeyT) {
30        self.deref().invalidate(key).await
31    }
32
33    async fn invalidate_all(&self) {
34        self.deref().invalidate_all()
35    }
36}