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 short_lived() -> Self
pub fn short_lived() -> Self
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() -> Self
pub fn read_mostly() -> Self
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() -> Self
pub fn per_entity() -> Self
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() -> Self
pub fn no_ttl_explicit_invalidation() -> Self
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() -> Self
pub fn negative_cache() -> Self
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>) -> Self
pub fn named(name: impl Into<String>) -> Self
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>) -> Self
pub fn with_name(self, name: impl Into<String>) -> Self
Set or replace the diagnostic operation name.
Sourcepub fn key_builder(self, key: CacheKeyBuilder) -> Self
pub fn key_builder(self, key: CacheKeyBuilder) -> Self
Set the logical cache key from a segmented key builder.
Sourcepub fn for_entity(self, kind: impl ToString, id: impl ToString) -> Self
pub fn for_entity(self, kind: impl ToString, id: impl ToString) -> Self
Set the logical key and add the same entity invalidation tag.
Sourcepub fn for_cache_entity<T>(self, id: T::Id) -> Selfwhere
T: CacheEntity,
pub fn for_cache_entity<T>(self, id: T::Id) -> Selfwhere
T: CacheEntity,
Set the logical key and tags from CacheEntity metadata.
Sourcepub fn collection(self, name: impl ToString) -> Self
pub fn collection(self, name: impl ToString) -> Self
Set the logical key and invalidation tag for a collection result.
Sourcepub fn collection_tag(self, name: impl ToString) -> Self
pub fn collection_tag(self, name: impl ToString) -> Self
Add a collection invalidation tag from one escaped key segment.
Add several invalidation tags.
Sourcepub fn refresh_policy(self, refresh: RefreshOptions) -> Self
pub fn refresh_policy(self, refresh: RefreshOptions) -> Self
Set refresh/stale behavior for this query result.
Sourcepub fn required_dimensions<I, S>(self, dimensions: I) -> Self
pub fn required_dimensions<I, S>(self, dimensions: I) -> Self
Store statically declared key dimensions for diagnostics and review.
Sourcepub fn required_dimension(self, dimension: impl Into<String>) -> Self
pub fn required_dimension(self, dimension: impl Into<String>) -> Self
Add one statically declared key dimension for diagnostics and review.
Sourcepub fn with_key_dimension_labels<I, S>(self, labels: I) -> Self
pub fn with_key_dimension_labels<I, S>(self, labels: I) -> Self
Store key dimension labels for profile validation.
Sourcepub fn with_tag_dimension_labels<I, S>(self, labels: I) -> Self
pub fn with_tag_dimension_labels<I, S>(self, labels: I) -> Self
Store tag dimension labels for profile validation.
Sourcepub fn with_dimension_profile(self, profile: DimensionProfile) -> Self
pub fn with_dimension_profile(self, profile: DimensionProfile) -> Self
Attach a reusable required-dimension profile.
Sourcepub fn with_dimension_validation_mode(
self,
mode: DimensionValidationMode,
) -> Self
pub fn with_dimension_validation_mode( self, mode: DimensionValidationMode, ) -> Self
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<Self, DimensionAllowError>
pub fn allow_dimension_violation( self, label: impl Into<String>, reason: impl Into<String>, ) -> Result<Self, 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<()>
pub fn enforce_dimension_profile(&self) -> Result<()>
Return an error when profile validation is in deny mode and fails.
Sourcepub fn lint_sql(self, sql: impl Into<String>) -> Self
pub fn lint_sql(self, sql: impl Into<String>) -> Self
Attach SQL text for off-runtime dependency linting.
Sourcepub fn dependency_lint_mode(self, mode: DeclaredLintMode) -> Self
pub fn dependency_lint_mode(self, mode: DeclaredLintMode) -> Self
Set the lint mode used by CI/build-time dependency checking.
Sourcepub fn declared_dependency(self, relation: DeclaredRelation) -> Self
pub fn declared_dependency(self, relation: DeclaredRelation) -> Self
Declare one relation that the query is expected to read.
Sourcepub fn declared_dependencies<I>(self, relations: I) -> Selfwhere
I: IntoIterator<Item = DeclaredRelation>,
pub fn declared_dependencies<I>(self, relations: I) -> Selfwhere
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>) -> Self
pub fn lint_allow(self, finding: LintFinding, reason: impl Into<String>) -> Self
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
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
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.