Expand description
SQLx-facing integration crate for HydraCache database result caching.
The database-neutral query cache API lives in hydracache-db. This crate
keeps SQLx users on a convenient import path while avoiding a hard conceptual
dependency between the generic adapter and SQLx itself.
§Example
use hydracache::HydraCache;
use hydracache_sqlx::{DbCache, HydraCacheEntity, PreparedQueryPolicy, SqlxQueryExt};
#[derive(serde::Serialize, serde::Deserialize, HydraCacheEntity)]
#[hydracache(entity = "user", collection = "users")]
struct User {
#[hydracache(id)]
id: i64,
name: String,
}
let local = HydraCache::local().build();
// SQLx users may import DbCache from this crate, but the type itself is
// database-neutral and comes from hydracache-db.
let queries = DbCache::new(local, "db");
let user: User = queries
.for_entity::<User>(42)
.fetch_with(move || async move {
let (id, name): (i64, String) =
sqlx::query_as("select id, name from users where id = $1")
.bind(42_i64)
.fetch_one(&pool)
.await?;
Ok::<_, sqlx::Error>(User { id, name })
})
.await?;
assert_eq!(user.id, 42);
assert!(!user.name.is_empty());Prepared policies keep repeated repository methods cheap while still using ordinary SQLx query execution on cache misses:
use hydracache::HydraCache;
use hydracache_sqlx::{DbCache, HydraCacheEntity, PreparedQueryPolicy, SqlxQueryExt};
#[derive(serde::Serialize, serde::Deserialize, HydraCacheEntity)]
#[hydracache(entity = "user", collection = "users")]
struct User {
#[hydracache(id)]
id: i64,
name: String,
}
let queries = DbCache::new(HydraCache::local().build(), "db");
let load_user = queries.prepare::<(i64, String)>(
PreparedQueryPolicy::for_cache_entity::<User>().with_name("load-user"),
);
let (id, name) = load_user
.for_id(42)
.sqlx_one(
pool.clone(),
sqlx::query_as("select id, name from users where id = $1").bind(42_i64),
)
.await?;
assert_eq!(id, 42);
assert!(!name.is_empty());Use DbQuery::fetch_with when you need SQLx macros, transactions, or a
repository function instead of a pool-like executor.
QueryCachePolicy and PreparedQueryPolicy are also re-exported for
SQLx users, but the policy types are database-neutral and live in
hydracache-db.
query_cache_policy! is re-exported for the same convenience.
Re-exports§
pub use sqlx;
Macros§
- prepared_
query_ policy - Build a reusable
PreparedQueryPolicywith less boilerplate. - query_
cache_ policy - Build a
QueryCachePolicywith less boilerplate.
Structs§
- Cache
KeyBuilder - Builder for cache keys made of escaped
:-separated segments. - DbCache
- A database-oriented view over a
HydraCacheinstance. - DbOperation
Context - Diagnostic context for one database cache operation.
- DbQuery
- A cacheable database query descriptor.
- Invalidation
Collector - Mutable invalidation collector passed to transaction companion closures.
- Invalidation
Plan - A database-neutral list of cache invalidations staged by repository code.
- Invalidation
Report - Result of executing a staged
InvalidationPlan. - PgNotify
Intent - One Postgres LISTEN/NOTIFY wake-up payload.
- PgNotify
Intent Source - Thin sqlx
PgListenerwrapper for invalidation wake-ups. - Prepared
DbQuery - A prepared database query descriptor.
- Prepared
Query Policy - Prepared database query cache metadata.
- Query
Cache Policy - Reusable cache metadata for one database query result.
- Sqlx
Invalidation Outbox - SQLx-backed invalidation outbox.
- Sqlx
Transaction Companion - SQLx transaction companion.
- Sqlx
Transaction Diagnostics - Diagnostics for SQLx transaction companion runs.
- Sqlx
Transaction Report - Result of a SQLx transaction companion run.
Enums§
- DbAdapter
Kind - Database adapter kind attached to operation diagnostics.
- DbCache
Error - Error type returned by database cache adapter helpers.
- DbResult
Shape - Cached database result shape attached to operation diagnostics.
- Sqlx
Cache Error - Error type returned by SQLx-facing cache helpers.
- Sqlx
Transaction Error - Error returned by SQLx transaction companion helpers.
Constants§
- OUTBOX_
SCHEMA_ VERSION - Current durable schema version for
hydracache_invalidation_outbox.
Traits§
- Cache
Entity - Static cache metadata for a domain entity.
- Sqlx
Query Ext - Convenience SQLx execution methods for
DbQuery. - Sqlx
Transaction Ext - Extension trait that creates SQLx transaction companions from
DbCache.
Type Aliases§
- DbResult
- Database cache adapter result type.
- Refresh
Policy - Database-facing alias for local cache refresh/stale behavior.
- Result
- SQLx adapter result type.
- Sqlx
Cache - SQLx-specific compatibility name for
DbCache. - Sqlx
Query - SQLx-specific compatibility name for
DbQuery. - Sqlx
Transaction Future - Boxed future returned by SQLx transaction companion closures.
- Transaction
Result - SQLx transaction companion result type.
Derive Macros§
- Hydra
Cache Entity - Derive
CacheEntitymetadata for database result-cache helpers.