Skip to main content

Crate hydracache_sqlx

Crate hydracache_sqlx 

Source
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;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct User {
    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 = queries
    .cached::<User>()
    .key("user:42")
    .tag("user:42")
    .fetch_with(|| async {
        // This loader runs only on a cache miss. On a cache hit, HydraCache
        // returns the cached User and this SQLx code is not executed.
        Ok::<_, std::io::Error>(User {
            id: 42,
            name: "Ada".to_owned(),
        })
    })
    .await?;

assert_eq!(user.id, 42);

Re-exports§

pub use sqlx;

Structs§

DbCache
A database-oriented view over a HydraCache instance.
DbQuery
A cacheable database query descriptor.

Enums§

DbCacheError
Error type returned by database cache adapter helpers.

Type Aliases§

Result
Database cache adapter result type.
SqlxCache
SQLx-specific compatibility name for DbCache.
SqlxCacheError
SQLx-specific compatibility name for DbCacheError.
SqlxQuery
SQLx-specific compatibility name for DbQuery.