pub struct Cache { /* private fields */ }Expand description
A handle on the cache database plus the active mode and TTLs. Cheap to
clone (clones share one connection); clone it out of the
Context to use get_or_fetch
with a closure that borrows the context. The database is opened on first
use.
Implementations§
Source§impl Cache
impl Cache
Sourcepub fn index_restaurants(
&self,
rows: &[RestaurantRow],
seen_from: Option<(f64, f64)>,
) -> Result<()>
pub fn index_restaurants( &self, rows: &[RestaurantRow], seen_from: Option<(f64, f64)>, ) -> Result<()>
Upsert restaurants seen in a list response. seen_from is the
delivery location of the request; when given, each row’s distance /
ETA / fee are also recorded for that location. No-op when writes are
off.
pub fn index_restaurants_at( &self, rows: &[RestaurantRow], seen_from: Option<(f64, f64)>, now: i64, ) -> Result<()>
Sourcepub fn index_items(&self, items: &[ItemRow]) -> Result<()>
pub fn index_items(&self, items: &[ItemRow]) -> Result<()>
Upsert individual items (e.g. dish hits in search results), keeping any category already known. No-op when writes are off.
pub fn index_items_at(&self, items: &[ItemRow], now: i64) -> Result<()>
Store a full menu: the restaurant, its categories (replacing the old ones) and items; items no longer on the menu are removed. No-op when writes are off.
Sourcepub fn search_restaurants(
&self,
q: &RestaurantQuery,
) -> Result<Vec<RestaurantHit>>
pub 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 fn search_items(&self, q: &ItemQuery) -> Result<Vec<ItemHit>>
pub fn search_items(&self, q: &ItemQuery) -> Result<Vec<ItemHit>>
Query menu items in the catalog.
Sourcepub fn restaurant(&self, id: &str) -> Result<Option<RestaurantHit>>
pub fn restaurant(&self, id: &str) -> Result<Option<RestaurantHit>>
One restaurant by id.
Source§impl Cache
impl Cache
Sourcepub fn cached_image(&self, url: &str) -> Option<CachedImage>
pub fn cached_image(&self, url: &str) -> Option<CachedImage>
The cached file for url, if present (and still on disk). Marks it
recently used.
Sourcepub fn store_image(
&self,
url: &str,
bytes: &[u8],
content_type: &str,
) -> Result<CachedImage>
pub fn store_image( &self, url: &str, bytes: &[u8], content_type: &str, ) -> Result<CachedImage>
Store downloaded image bytes for url (dedupes by content hash), then
evict over the size cap.
pub fn store_image_at( &self, url: &str, bytes: &[u8], content_type: &str, now: i64, ) -> Result<CachedImage>
Sourcepub async fn fetch_image(&self, url: &str) -> Result<CachedImage>
pub async fn fetch_image(&self, url: &str) -> Result<CachedImage>
The cached file for url, downloading it first if needed. Only Grab
CDN URLs are fetched (image_url_allowed); no cookies are sent.
Source§impl Cache
impl Cache
Sourcepub fn new(
root: impl Into<PathBuf>,
config: CacheConfig,
mode: CacheMode,
) -> Self
pub fn new( root: impl Into<PathBuf>, config: CacheConfig, mode: CacheMode, ) -> Self
A cache rooted at root (the database is root/cache.sqlite).
config.enabled == false forces CacheMode::Off.
Sourcepub fn from_config(config: &CacheConfig, mode: CacheMode) -> Self
pub fn from_config(config: &CacheConfig, mode: CacheMode) -> Self
The production cache: paths::cache_dir, the [cache] config, and
GRAB_NO_CACHE=1 forcing it off. If the cache dir can’t be resolved the
cache is simply off.
Sourcepub fn with_mode(&self, mode: CacheMode) -> Self
pub fn with_mode(&self, mode: CacheMode) -> Self
The same cache (sharing its connection) with another mode; still off if disabled in config.
pub fn mode(&self) -> CacheMode
pub fn db_path(&self) -> PathBuf
pub fn config(&self) -> &CacheConfig
pub fn ttl(&self, kind: CacheKind) -> u64
Sourcepub fn get<T: DeserializeOwned>(&self, key: &CacheKey) -> Option<T>
pub 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 fn lookup<T: DeserializeOwned>(&self, key: &CacheKey) -> Option<Fetched<T>>
pub fn lookup<T: DeserializeOwned>(&self, key: &CacheKey) -> Option<Fetched<T>>
Like get, with the time the entry was fetched.
Sourcepub fn lookup_stale<T: DeserializeOwned>(
&self,
key: &CacheKey,
) -> Option<Fetched<T>>
pub 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 fn put<T: Serialize>(&self, key: &CacheKey, value: &T) -> Result<()>
pub 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.
pub fn stats(&self) -> Result<CacheStats>
Sourcepub fn clear_all(&self) -> Result<ClearReport>
pub fn clear_all(&self) -> Result<ClearReport>
Remove everything: responses, catalog rows and image files.
Sourcepub fn prune(&self) -> Result<PruneReport>
pub fn prune(&self) -> Result<PruneReport>
Housekeeping: drop expired responses, catalog rows older than
index_max_age_days, and evict images over image_max_mb (LRU), then
compact the database.