pub struct DbCache<C = PostcardCodec>where
C: CacheCodec,{ /* private fields */ }Expand description
A database-oriented view over a HydraCache instance.
DbCache groups query result keys under a namespace while keeping all
cache storage, single-flight, tags, TTL, and stats in the shared local cache.
§Example
use std::time::Duration;
use hydracache::HydraCache;
use hydracache_db::DbCache;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct User {
id: i64,
name: String,
}
let local = HydraCache::local().build();
let queries = DbCache::new(local, "db");
let user = queries
.cached::<User>()
// Physical cache key: "db:user:42".
.key("user:42")
// Later, invalidate_tag("user:42") removes this result.
.tag("user:42")
.ttl(Duration::from_secs(60))
.fetch_with(|| async {
// Replace this block with code from sqlx, diesel, sea-orm, or any
// other database client. It is called only when the cache does not
// already contain "db:user:42" or when the cached value has expired.
Ok::<_, std::io::Error>(User {
id: 42,
name: "Ada".to_owned(),
})
})
.await?;
assert_eq!(user.id, 42);Implementations§
Source§impl<C> DbCache<C>where
C: CacheCodec,
impl<C> DbCache<C>where
C: CacheCodec,
Sourcepub fn new(cache: HydraCache<C>, namespace: impl Into<String>) -> DbCache<C>
pub fn new(cache: HydraCache<C>, namespace: impl Into<String>) -> DbCache<C>
Create a database query cache adapter over an existing local cache.
Sourcepub fn cache(&self) -> &HydraCache<C>
pub fn cache(&self) -> &HydraCache<C>
Return the underlying local cache.
Sourcepub fn cached<T>(&self) -> DbQuery<T, C>
pub fn cached<T>(&self) -> DbQuery<T, C>
Start describing a cacheable database-loaded value.
This is the preferred entry point when the query is already visible
inside the fetch_with loader through a database client, ORM, or
repository method.
Sourcepub fn named<T>(&self, name: impl Into<String>) -> DbQuery<T, C>
pub fn named<T>(&self, name: impl Into<String>) -> DbQuery<T, C>
Start describing a cacheable database-loaded value with a diagnostic name.
Sourcepub fn query_as<T>(&self, sql: impl Into<String>) -> DbQuery<T, C>
pub fn query_as<T>(&self, sql: impl Into<String>) -> DbQuery<T, C>
Start describing a cacheable SQL query result.
Prefer DbCache::cached or DbCache::named when writing new code.
This method remains useful if you want the SQL text itself to be the
diagnostic label for errors and logs.
Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for DbCache<C>
impl<C = PostcardCodec> !RefUnwindSafe for DbCache<C>
impl<C> Send for DbCache<C>
impl<C> Sync for DbCache<C>
impl<C> Unpin for DbCache<C>
impl<C> UnsafeUnpin for DbCache<C>
impl<C = PostcardCodec> !UnwindSafe for DbCache<C>
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<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