pub struct CacheHandle { /* private fields */ }Expand description
A Cache backend plus the active CacheMode and TTLs. Cheap to clone
(clones share the backend); clone it out of the
Context to call get_or_fetch
with a closure that borrows the context.
Implementations§
Source§impl CacheHandle
impl CacheHandle
Sourcepub fn new(
backend: Arc<dyn Cache>,
config: CacheConfig,
mode: CacheMode,
) -> Self
pub fn new( backend: Arc<dyn Cache>, config: CacheConfig, mode: CacheMode, ) -> Self
config.enabled == false forces CacheMode::Off.
Sourcepub fn with_mode(&self, mode: CacheMode) -> Self
pub fn with_mode(&self, mode: CacheMode) -> Self
The same backend with another mode; still off if disabled in config.
pub fn mode(&self) -> CacheMode
pub fn config(&self) -> &CacheConfig
pub fn ttl(&self, kind: CacheKind) -> u64
Sourcepub fn backend(&self) -> &Arc<dyn Cache> ⓘ
pub fn backend(&self) -> &Arc<dyn Cache> ⓘ
The backend, for housekeeping outside the mode rules.
Sourcepub async fn get<T: DeserializeOwned>(&self, key: &CacheKey) -> Option<T>
pub async fn get<T: DeserializeOwned>(&self, key: &CacheKey) -> Option<T>
A fresh cached value, if reads are enabled and one exists. Any problem reading or decoding the entry is a miss.
Sourcepub async fn lookup<T: DeserializeOwned>(
&self,
key: &CacheKey,
) -> Option<Fetched<T>>
pub async fn lookup<T: DeserializeOwned>( &self, key: &CacheKey, ) -> Option<Fetched<T>>
Like get, with the time the entry was fetched.
Sourcepub async fn lookup_stale<T: DeserializeOwned>(
&self,
key: &CacheKey,
) -> Option<Fetched<T>>
pub async fn lookup_stale<T: DeserializeOwned>( &self, key: &CacheKey, ) -> Option<Fetched<T>>
The stored value for key even if it has expired (but not yet been
pruned), for falling back when the live call fails. None when the
mode is off.
Sourcepub async fn put<T: Serialize>(&self, key: &CacheKey, value: &T) -> Result<()>
pub async fn put<T: Serialize>(&self, key: &CacheKey, value: &T) -> Result<()>
Store a value (no-op when writes are off or the kind’s TTL is 0).
Sourcepub async fn get_or_fetch<T, F, Fut>(
&self,
key: &CacheKey,
fetch: F,
) -> Result<T>
pub async fn get_or_fetch<T, F, Fut>( &self, key: &CacheKey, fetch: F, ) -> Result<T>
Return the cached value for key, or run fetch, store its result
(best-effort) and return it. Errors from fetch are returned and never
cached. Prefer T = serde_json::Value holding the raw upstream response.
Sourcepub async fn get_or_fetch_meta<T, F, Fut>(
&self,
key: &CacheKey,
fetch: F,
) -> Result<Fetched<T>>
pub async fn get_or_fetch_meta<T, F, Fut>( &self, key: &CacheKey, fetch: F, ) -> Result<Fetched<T>>
get_or_fetch that also says whether the value
came from the cache and how old it is.
Sourcepub async fn index_restaurants(
&self,
rows: &[RestaurantRow],
seen_from: Option<(f64, f64)>,
) -> Result<()>
pub async fn index_restaurants( &self, rows: &[RestaurantRow], seen_from: Option<(f64, f64)>, ) -> Result<()>
Upsert restaurants seen in a list response (no-op when writes are off).
Sourcepub async fn index_items(&self, items: &[ItemRow]) -> Result<()>
pub async fn index_items(&self, items: &[ItemRow]) -> Result<()>
Upsert individual items (no-op when writes are off).
Store a full menu (no-op when writes are off).
Sourcepub async fn search_restaurants(
&self,
q: &RestaurantQuery,
) -> Result<Vec<RestaurantHit>>
pub async fn search_restaurants( &self, q: &RestaurantQuery, ) -> Result<Vec<RestaurantHit>>
Query restaurants in the catalog (works in every mode: it only reads what earlier fetches stored).
Sourcepub async fn search_items(&self, q: &ItemQuery) -> Result<Vec<ItemHit>>
pub async fn search_items(&self, q: &ItemQuery) -> Result<Vec<ItemHit>>
Query menu items in the catalog.
Sourcepub async fn restaurant(&self, id: &str) -> Result<Option<RestaurantHit>>
pub async fn restaurant(&self, id: &str) -> Result<Option<RestaurantHit>>
One restaurant by id.
Sourcepub async fn cached_image(&self, url: &str) -> Option<CachedImage>
pub async fn cached_image(&self, url: &str) -> Option<CachedImage>
The cached image for url, if present. None when the mode is off.
Sourcepub async fn store_image(
&self,
url: &str,
bytes: &[u8],
content_type: &str,
) -> Result<CachedImage>
pub async fn store_image( &self, url: &str, bytes: &[u8], content_type: &str, ) -> Result<CachedImage>
Store downloaded image bytes (refused when writes are off).
Sourcepub async fn fetch_image(&self, url: &str) -> Result<CachedImage>
pub async fn fetch_image(&self, url: &str) -> Result<CachedImage>
The cached image for url, downloading it first if needed. Only Grab
CDN URLs are fetched (image_url_allowed); no cookies are sent.