pub struct QueryCachePolicy { /* private fields */ }Expand description
Reusable cache metadata for one database query result.
QueryCachePolicy contains the database-neutral parts of query result
caching: diagnostic name, logical key, invalidation tags, and optional TTL.
It is intentionally independent of SQLx, Diesel, SeaORM, or any other
database client.
§Example
use std::time::Duration;
use hydracache_db::QueryCachePolicy;
let policy = QueryCachePolicy::named("load-user")
.key("user:42")
.tag("user:42")
.ttl(Duration::from_secs(60));
assert_eq!(policy.name(), Some("load-user"));
assert_eq!(policy.key_value(), Some("user:42"));
assert_eq!(policy.tags_value(), &["user:42".to_owned()]);
assert_eq!(policy.ttl_value(), Some(Duration::from_secs(60)));The query_cache_policy! macro provides a
shorter declarative form when the policy is known at the call site.
Implementations§
Source§impl QueryCachePolicy
impl QueryCachePolicy
Sourcepub fn new() -> QueryCachePolicy
pub fn new() -> QueryCachePolicy
Create an empty cache policy.
Sourcepub fn short_lived() -> QueryCachePolicy
pub fn short_lived() -> QueryCachePolicy
Create a short-lived policy for values that should smooth brief bursts.
The preset uses a 30 second TTL and leaves key/tags to the caller.
§Example
use std::time::Duration;
use hydracache_db::QueryCachePolicy;
let policy = QueryCachePolicy::short_lived().key("user:42");
assert_eq!(policy.ttl_value(), Some(Duration::from_secs(30)));
assert_eq!(policy.key_value(), Some("user:42"));Sourcepub fn read_mostly() -> QueryCachePolicy
pub fn read_mostly() -> QueryCachePolicy
Create a read-mostly policy for values that change rarely.
The preset uses a 5 minute TTL. Pair it with entity or collection tags so writes can still invalidate cached results explicitly.
Sourcepub fn per_entity() -> QueryCachePolicy
pub fn per_entity() -> QueryCachePolicy
Create a policy intended for one entity-shaped result.
The preset uses a 5 minute TTL and expects the caller to add an entity
key/tag with QueryCachePolicy::for_entity or
QueryCachePolicy::for_cache_entity.
Sourcepub fn no_ttl_explicit_invalidation() -> QueryCachePolicy
pub fn no_ttl_explicit_invalidation() -> QueryCachePolicy
Create a policy for explicit-invalidation-only values.
No TTL is configured. The value remains cached until the caller invalidates a key/tag, removes it, flushes the cache, or the backend evicts it due to capacity pressure.
Sourcepub fn negative_cache() -> QueryCachePolicy
pub fn negative_cache() -> QueryCachePolicy
Create a policy for caching negative lookups briefly.
Use this for Option<T> or domain-specific “not found” results where
repeated misses are expensive but long-lived absence would be unsafe.
The preset uses a 30 second TTL.
Sourcepub fn named(name: impl Into<String>) -> QueryCachePolicy
pub fn named(name: impl Into<String>) -> QueryCachePolicy
Create a cache policy with a diagnostic operation name.
Return configured invalidation tags.
Sourcepub fn refresh_policy_value(&self) -> Option<RefreshOptions>
pub fn refresh_policy_value(&self) -> Option<RefreshOptions>
Return the optional refresh/stale policy.
Sourcepub fn required_dimensions_value(&self) -> &[String]
pub fn required_dimensions_value(&self) -> &[String]
Return statically declared key dimensions required by this policy.
These labels are diagnostics only; values are intentionally not stored.
Sourcepub fn key_dimension_labels(&self) -> &[String]
pub fn key_dimension_labels(&self) -> &[String]
Return static key dimension labels recorded by macro/profile tooling.
Sourcepub fn tag_dimension_labels(&self) -> &[String]
pub fn tag_dimension_labels(&self) -> &[String]
Return static tag dimension labels recorded by macro/profile tooling.
Sourcepub fn dimension_profile(&self) -> Option<&DimensionProfile>
pub fn dimension_profile(&self) -> Option<&DimensionProfile>
Return the optional required-dimension profile.
Sourcepub fn dimension_validation_mode(&self) -> DimensionValidationMode
pub fn dimension_validation_mode(&self) -> DimensionValidationMode
Return the profile validation mode.
Sourcepub fn lint_metadata(&self) -> Option<&PolicyLintMetadata>
pub fn lint_metadata(&self) -> Option<&PolicyLintMetadata>
Return optional SQL dependency-lint metadata for CI/build-time tooling.
Sourcepub fn with_name(self, name: impl Into<String>) -> QueryCachePolicy
pub fn with_name(self, name: impl Into<String>) -> QueryCachePolicy
Set or replace the diagnostic operation name.
Sourcepub fn key(self, key: impl Into<String>) -> QueryCachePolicy
pub fn key(self, key: impl Into<String>) -> QueryCachePolicy
Set the logical cache key.
Sourcepub fn key_builder(self, key: CacheKeyBuilder) -> QueryCachePolicy
pub fn key_builder(self, key: CacheKeyBuilder) -> QueryCachePolicy
Set the logical cache key from a segmented key builder.
Sourcepub fn for_entity(
self,
kind: impl ToString,
id: impl ToString,
) -> QueryCachePolicy
pub fn for_entity( self, kind: impl ToString, id: impl ToString, ) -> QueryCachePolicy
Set the logical key and add the same entity invalidation tag.
Sourcepub fn for_cache_entity<T>(self, id: <T as CacheEntity>::Id) -> QueryCachePolicywhere
T: CacheEntity,
pub fn for_cache_entity<T>(self, id: <T as CacheEntity>::Id) -> QueryCachePolicywhere
T: CacheEntity,
Set the logical key and tags from CacheEntity metadata.
Sourcepub fn collection(self, name: impl ToString) -> QueryCachePolicy
pub fn collection(self, name: impl ToString) -> QueryCachePolicy
Set the logical key and invalidation tag for a collection result.
Sourcepub fn tag(self, tag: impl Into<String>) -> QueryCachePolicy
pub fn tag(self, tag: impl Into<String>) -> QueryCachePolicy
Add one invalidation tag.
Sourcepub fn collection_tag(self, name: impl ToString) -> QueryCachePolicy
pub fn collection_tag(self, name: impl ToString) -> QueryCachePolicy
Add a collection invalidation tag from one escaped key segment.
Add several invalidation tags.
Sourcepub fn tag_set(self, tags: TagSet) -> QueryCachePolicy
pub fn tag_set(self, tags: TagSet) -> QueryCachePolicy
Replace invalidation tags from a reusable TagSet.
Sourcepub fn ttl(self, ttl: Duration) -> QueryCachePolicy
pub fn ttl(self, ttl: Duration) -> QueryCachePolicy
Set a per-entry TTL.
Sourcepub fn refresh_policy(self, refresh: RefreshOptions) -> QueryCachePolicy
pub fn refresh_policy(self, refresh: RefreshOptions) -> QueryCachePolicy
Set refresh/stale behavior for this query result.
Sourcepub fn required_dimensions<I, S>(self, dimensions: I) -> QueryCachePolicy
pub fn required_dimensions<I, S>(self, dimensions: I) -> QueryCachePolicy
Store statically declared key dimensions for diagnostics and review.
Sourcepub fn required_dimension(
self,
dimension: impl Into<String>,
) -> QueryCachePolicy
pub fn required_dimension( self, dimension: impl Into<String>, ) -> QueryCachePolicy
Add one statically declared key dimension for diagnostics and review.
Sourcepub fn with_key_dimension_labels<I, S>(self, labels: I) -> QueryCachePolicy
pub fn with_key_dimension_labels<I, S>(self, labels: I) -> QueryCachePolicy
Store key dimension labels for profile validation.
Sourcepub fn with_tag_dimension_labels<I, S>(self, labels: I) -> QueryCachePolicy
pub fn with_tag_dimension_labels<I, S>(self, labels: I) -> QueryCachePolicy
Store tag dimension labels for profile validation.
Sourcepub fn with_dimension_profile(
self,
profile: DimensionProfile,
) -> QueryCachePolicy
pub fn with_dimension_profile( self, profile: DimensionProfile, ) -> QueryCachePolicy
Attach a reusable required-dimension profile.
Sourcepub fn with_dimension_validation_mode(
self,
mode: DimensionValidationMode,
) -> QueryCachePolicy
pub fn with_dimension_validation_mode( self, mode: DimensionValidationMode, ) -> QueryCachePolicy
Set whether profile violations should warn or fail a CI/release gate.
Sourcepub fn allow_dimension_violation(
self,
label: impl Into<String>,
reason: impl Into<String>,
) -> Result<QueryCachePolicy, DimensionAllowError>
pub fn allow_dimension_violation( self, label: impl Into<String>, reason: impl Into<String>, ) -> Result<QueryCachePolicy, DimensionAllowError>
Allow one profile dimension violation with an explicit review reason.
Sourcepub fn validate_dimension_profile(&self) -> ProfileValidation
pub fn validate_dimension_profile(&self) -> ProfileValidation
Validate the configured profile against recorded key/tag labels.
Sourcepub fn enforce_dimension_profile(&self) -> Result<(), DbCacheError>
pub fn enforce_dimension_profile(&self) -> Result<(), DbCacheError>
Return an error when profile validation is in deny mode and fails.
Sourcepub fn lint_sql(self, sql: impl Into<String>) -> QueryCachePolicy
pub fn lint_sql(self, sql: impl Into<String>) -> QueryCachePolicy
Attach SQL text for off-runtime dependency linting.
Sourcepub fn dependency_lint_mode(self, mode: DeclaredLintMode) -> QueryCachePolicy
pub fn dependency_lint_mode(self, mode: DeclaredLintMode) -> QueryCachePolicy
Set the lint mode used by CI/build-time dependency checking.
Sourcepub fn declared_dependency(self, relation: DeclaredRelation) -> QueryCachePolicy
pub fn declared_dependency(self, relation: DeclaredRelation) -> QueryCachePolicy
Declare one relation that the query is expected to read.
Sourcepub fn declared_dependencies<I>(self, relations: I) -> QueryCachePolicywhere
I: IntoIterator<Item = DeclaredRelation>,
pub fn declared_dependencies<I>(self, relations: I) -> QueryCachePolicywhere
I: IntoIterator<Item = DeclaredRelation>,
Declare several relations that the query is expected to read.
Sourcepub fn lint_allow(
self,
finding: LintFinding,
reason: impl Into<String>,
) -> QueryCachePolicy
pub fn lint_allow( self, finding: LintFinding, reason: impl Into<String>, ) -> QueryCachePolicy
Suppress one dependency-lint finding with an explicit reason.
Trait Implementations§
Source§impl Clone for QueryCachePolicy
impl Clone for QueryCachePolicy
Source§fn clone(&self) -> QueryCachePolicy
fn clone(&self) -> QueryCachePolicy
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for QueryCachePolicy
impl Debug for QueryCachePolicy
Source§impl Default for QueryCachePolicy
impl Default for QueryCachePolicy
Source§fn default() -> QueryCachePolicy
fn default() -> QueryCachePolicy
impl Eq for QueryCachePolicy
Source§impl PartialEq for QueryCachePolicy
impl PartialEq for QueryCachePolicy
Source§fn eq(&self, other: &QueryCachePolicy) -> bool
fn eq(&self, other: &QueryCachePolicy) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for QueryCachePolicy
Auto Trait Implementations§
impl Freeze for QueryCachePolicy
impl RefUnwindSafe for QueryCachePolicy
impl Send for QueryCachePolicy
impl Sync for QueryCachePolicy
impl Unpin for QueryCachePolicy
impl UnsafeUnpin for QueryCachePolicy
impl UnwindSafe for QueryCachePolicy
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more