pub struct CachedCatalog<C: MetricCatalog + ?Sized> { /* private fields */ }Expand description
Cache layer for any MetricCatalog implementation.
Catalog data is small and slow-changing (per-session: it can only grow, never shrink). The cache absorbs the repeated queries that completion fires on every keystroke and amortises the backend round-trip across them. For sqlite-backed sessions the speedup is modest (the queries are already cheap); for remote backends it’s the difference between “completion feels instant” and “every tab spawns an HTTP roundtrip.”
§Invalidation
Three layers, evaluated in order:
- Time-based TTL — every cached entry expires after
CachedCatalog::ttl(default: 1 second). Aggressive by design: completion users tap repeatedly, so the cache absorbs bursts but yields to fresh data quickly. - Generation counter —
CachedCatalog::invalidatebumps a process-local generation to force the next read. Used by integration tests + by callers that know the underlying state changed (e.g. a writer just landed a new metric family). - Backend-supplied mtime — when the
CachedCatalogholds amtime_fn, every read consults it; if the timestamp moved past the cached snapshot’s read time, the entry expires immediately. This is the path the sqlite adapter uses to detect on-disk db changes (writer flushed).
The cache is a soft hint — if any layer says “stale,” the next call refetches and rebuilds. There’s no invariant that two concurrent readers of the same just-invalidated key see the same fresh result; both may run the underlying query. That’s acceptable because catalog reads are idempotent and side-effect-free.
Implementations§
Source§impl<C: MetricCatalog + ?Sized + 'static> CachedCatalog<C>
impl<C: MetricCatalog + ?Sized + 'static> CachedCatalog<C>
Sourcepub fn new(inner: Arc<C>) -> Self
pub fn new(inner: Arc<C>) -> Self
Wrap an inner catalog with a default 1-second TTL and no backend-mtime hook.
Sourcepub fn with_ttl(self, ttl: Duration) -> Self
pub fn with_ttl(self, ttl: Duration) -> Self
Builder: set the TTL window. Setting Duration::ZERO
disables the TTL layer (entries still expire on
generation bump or mtime change).
Sourcepub fn with_mtime_fn<F>(self, mtime_fn: F) -> Self
pub fn with_mtime_fn<F>(self, mtime_fn: F) -> Self
Builder: install a backend-mtime hook. The closure
returns the latest backend-side mtime as a monotonic
Instant; entries cached before the latest mtime
are considered stale on the next read.
None from the closure (e.g. the underlying file
disappeared) keeps the existing entries — the
behaviour matches “mtime unknown ⇒ trust the TTL.”
Sourcepub fn invalidate(&self)
pub fn invalidate(&self)
Force the next read of every key to refetch. Internally bumps the generation counter; existing entries become stale at next access.
Trait Implementations§
Source§impl<C: MetricCatalog + ?Sized + 'static> MetricCatalog for CachedCatalog<C>
impl<C: MetricCatalog + ?Sized + 'static> MetricCatalog for CachedCatalog<C>
Source§fn metric_families(&self) -> Result<Vec<MetricFamilyMeta>, DataSourceError>
fn metric_families(&self) -> Result<Vec<MetricFamilyMeta>, DataSourceError>
Source§fn label_keys(
&self,
family_filter: Option<&str>,
) -> Result<Vec<String>, DataSourceError>
fn label_keys( &self, family_filter: Option<&str>, ) -> Result<Vec<String>, DataSourceError>
family_filter = None returns every key the
backend has seen anywhere. Read moreSource§fn label_values(
&self,
key: &str,
family_filter: Option<&str>,
) -> Result<Vec<String>, DataSourceError>
fn label_values( &self, key: &str, family_filter: Option<&str>, ) -> Result<Vec<String>, DataSourceError>
key, optionally
restricted to one metric family. Returns an empty list
if the key has no values observed (or doesn’t exist) —
no error, since “no values yet” is a normal state for
a fresh session. Read moreSource§fn series(&self, matchers: &[Matcher]) -> Result<Vec<LabelSet>, DataSourceError>
fn series(&self, matchers: &[Matcher]) -> Result<Vec<LabelSet>, DataSourceError>
Matcher. The result is the series identity
surface — one entry per (family + label set) tuple
that satisfies the selector. Used by callers that need
to know “which concrete series exist that satisfy this
shape” without fetching their samples. Read moreSource§fn exemplars(
&self,
matchers: &[Matcher],
time_range: Option<(i64, i64)>,
) -> Result<Vec<ExemplarPoint>, DataSourceError>
fn exemplars( &self, matchers: &[Matcher], time_range: Option<(i64, i64)>, ) -> Result<Vec<ExemplarPoint>, DataSourceError>
matchers,
optionally restricted to a [start_ms, end_ms]
window on sample_timestamp_ms. Returns one
ExemplarPoint per stored exemplar; series with
no exemplars contribute nothing. Read more