pub struct PreparedQueryPolicy { /* private fields */ }Expand description
Prepared database query cache metadata.
PreparedQueryPolicy stores stable query-cache metadata once and binds only
the dynamic part, such as an entity id, on the hot path. It remains
database-neutral: SQLx, Diesel, SeaORM, or a hand-written repository can all
turn the prepared policy into the ordinary QueryCachePolicy consumed by
DbCache.
§Example
use std::time::Duration;
use hydracache_db::{CacheEntity, PreparedQueryPolicy};
struct User;
impl CacheEntity for User {
type Id = i64;
const ENTITY: &'static str = "user";
const COLLECTION: Option<&'static str> = Some("users");
}
let prepared = PreparedQueryPolicy::for_cache_entity::<User>()
.with_name("load-user")
.ttl(Duration::from_secs(60));
let policy = prepared.bind_id(42);
assert_eq!(policy.name(), Some("load-user"));
assert_eq!(policy.key_value(), Some("user:42"));
assert_eq!(policy.tags_value(), &["users".to_owned(), "user:42".to_owned()]);Implementations§
Source§impl PreparedQueryPolicy
impl PreparedQueryPolicy
Sourcepub fn new() -> PreparedQueryPolicy
pub fn new() -> PreparedQueryPolicy
Create an empty prepared policy.
Sourcepub fn short_lived() -> PreparedQueryPolicy
pub fn short_lived() -> PreparedQueryPolicy
Create a short-lived prepared policy for burst smoothing.
The preset uses a 30 second TTL and leaves key/tags to the caller.
Sourcepub fn read_mostly() -> PreparedQueryPolicy
pub fn read_mostly() -> PreparedQueryPolicy
Create a read-mostly prepared 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() -> PreparedQueryPolicy
pub fn per_entity() -> PreparedQueryPolicy
Create a prepared policy intended for one entity-shaped result.
The preset uses a 5 minute TTL and expects the caller to add an entity
prefix with PreparedQueryPolicy::entity or to start from
PreparedQueryPolicy::for_cache_entity.
Sourcepub fn no_ttl_explicit_invalidation() -> PreparedQueryPolicy
pub fn no_ttl_explicit_invalidation() -> PreparedQueryPolicy
Create a prepared policy for explicit-invalidation-only values.
No TTL is configured. The value remains cached until invalidated, removed, flushed, or evicted due to capacity pressure.
Sourcepub fn negative_cache() -> PreparedQueryPolicy
pub fn negative_cache() -> PreparedQueryPolicy
Create a prepared 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>) -> PreparedQueryPolicy
pub fn named(name: impl Into<String>) -> PreparedQueryPolicy
Create a prepared policy with a diagnostic operation name.
Sourcepub fn for_entity(kind: impl ToString) -> PreparedQueryPolicy
pub fn for_entity(kind: impl ToString) -> PreparedQueryPolicy
Create a prepared entity-id policy from one escaped entity segment.
The entity segment is escaped once. Each PreparedQueryPolicy::bind_id
call only escapes and appends the id segment.
Sourcepub fn for_cache_entity<T>() -> PreparedQueryPolicywhere
T: CacheEntity,
pub fn for_cache_entity<T>() -> PreparedQueryPolicywhere
T: CacheEntity,
Create a prepared entity-id policy from CacheEntity metadata.
The entity prefix and optional collection tag are precomputed once.
Sourcepub fn requires_id(&self) -> bool
pub fn requires_id(&self) -> bool
Return true when this policy needs an id binding before it has a key.
Sourcepub fn static_key_value(&self) -> Option<&str>
pub fn static_key_value(&self) -> Option<&str>
Return the static logical key, if this prepared policy has one.
Sourcepub fn entity_key_prefix(&self) -> Option<&str>
pub fn entity_key_prefix(&self) -> Option<&str>
Return the precomputed entity key prefix, if this is an entity policy.
Return precomputed 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 with_name(self, name: impl Into<String>) -> PreparedQueryPolicy
pub fn with_name(self, name: impl Into<String>) -> PreparedQueryPolicy
Set or replace the diagnostic operation name.
Sourcepub fn key(self, key: impl Into<String>) -> PreparedQueryPolicy
pub fn key(self, key: impl Into<String>) -> PreparedQueryPolicy
Set a static logical key.
Sourcepub fn key_builder(self, key: CacheKeyBuilder) -> PreparedQueryPolicy
pub fn key_builder(self, key: CacheKeyBuilder) -> PreparedQueryPolicy
Set a static logical key from a segmented key builder.
Sourcepub fn entity(self, kind: impl ToString) -> PreparedQueryPolicy
pub fn entity(self, kind: impl ToString) -> PreparedQueryPolicy
Set an entity-id key prefix from one escaped entity segment.
Sourcepub fn cache_entity<T>(self) -> PreparedQueryPolicywhere
T: CacheEntity,
pub fn cache_entity<T>(self) -> PreparedQueryPolicywhere
T: CacheEntity,
Set an entity-id key prefix and optional collection tag from
CacheEntity metadata while preserving preset TTL/name settings.
§Example
use std::time::Duration;
use hydracache_db::{CacheEntity, PreparedQueryPolicy};
struct User;
impl CacheEntity for User {
type Id = i64;
const ENTITY: &'static str = "user";
const COLLECTION: Option<&'static str> = Some("users");
}
let policy = PreparedQueryPolicy::per_entity().cache_entity::<User>();
assert_eq!(policy.entity_key_prefix(), Some("user"));
assert_eq!(policy.tags_value(), &["users".to_owned()]);
assert_eq!(policy.ttl_value(), Some(Duration::from_secs(300)));Sourcepub fn collection(self, name: impl ToString) -> PreparedQueryPolicy
pub fn collection(self, name: impl ToString) -> PreparedQueryPolicy
Set a static collection key and add the same collection invalidation tag.
Sourcepub fn tag(self, tag: impl Into<String>) -> PreparedQueryPolicy
pub fn tag(self, tag: impl Into<String>) -> PreparedQueryPolicy
Add one precomputed invalidation tag.
Sourcepub fn collection_tag(self, name: impl ToString) -> PreparedQueryPolicy
pub fn collection_tag(self, name: impl ToString) -> PreparedQueryPolicy
Add a precomputed collection invalidation tag from one escaped segment.
Add several precomputed invalidation tags.
Sourcepub fn tag_set(self, tags: TagSet) -> PreparedQueryPolicy
pub fn tag_set(self, tags: TagSet) -> PreparedQueryPolicy
Replace precomputed invalidation tags from a reusable TagSet.
Sourcepub fn ttl(self, ttl: Duration) -> PreparedQueryPolicy
pub fn ttl(self, ttl: Duration) -> PreparedQueryPolicy
Set a precomputed per-entry TTL.
Sourcepub fn refresh_policy(self, refresh: RefreshOptions) -> PreparedQueryPolicy
pub fn refresh_policy(self, refresh: RefreshOptions) -> PreparedQueryPolicy
Set refresh/stale behavior for this prepared query result.
Sourcepub fn to_policy(&self) -> QueryCachePolicy
pub fn to_policy(&self) -> QueryCachePolicy
Convert this prepared policy into a runtime QueryCachePolicy.
Entity-id policies still need PreparedQueryPolicy::bind_id to set a
key. Static-key and collection policies can use this method directly.
Sourcepub fn bind_id(&self, id: impl ToString) -> QueryCachePolicy
pub fn bind_id(&self, id: impl ToString) -> QueryCachePolicy
Bind an id to this prepared policy and produce a runtime policy.
For entity policies, this creates the final logical key and adds the
entity tag. For static-key policies, the id is ignored and
PreparedQueryPolicy::to_policy behavior is used.
Trait Implementations§
Source§impl Clone for PreparedQueryPolicy
impl Clone for PreparedQueryPolicy
Source§fn clone(&self) -> PreparedQueryPolicy
fn clone(&self) -> PreparedQueryPolicy
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 PreparedQueryPolicy
impl Debug for PreparedQueryPolicy
Source§impl Default for PreparedQueryPolicy
impl Default for PreparedQueryPolicy
Source§fn default() -> PreparedQueryPolicy
fn default() -> PreparedQueryPolicy
impl Eq for PreparedQueryPolicy
Source§impl PartialEq for PreparedQueryPolicy
impl PartialEq for PreparedQueryPolicy
Source§fn eq(&self, other: &PreparedQueryPolicy) -> bool
fn eq(&self, other: &PreparedQueryPolicy) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for PreparedQueryPolicy
Auto Trait Implementations§
impl Freeze for PreparedQueryPolicy
impl RefUnwindSafe for PreparedQueryPolicy
impl Send for PreparedQueryPolicy
impl Sync for PreparedQueryPolicy
impl Unpin for PreparedQueryPolicy
impl UnsafeUnpin for PreparedQueryPolicy
impl UnwindSafe for PreparedQueryPolicy
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.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