Skip to content

v0.10.0

Choose a tag to compare

@javaquasar javaquasar released this 02 Jun 10:19
· 206 commits to main since this release

HydraCache 0.10.0 Release Notes

HydraCache 0.10.0 adds domain entity cache metadata for database result
caching.

Highlights

  • Added CacheEntity in hydracache-db.
  • Re-exported CacheEntity from hydracache-sqlx.
  • Added DbCache::for_entity<T: CacheEntity>(id).
  • Added DbQuery::for_cache_entity(id) for existing descriptor chains.
  • Automatically adds the entity tag and optional collection tag from metadata.
  • Kept entity(kind, id), collection(name), and the explicit
    .cached().key().tag() API unchanged.
  • Extended unit tests and real Postgres SQLx integration tests for the new
    metadata-driven path.

Example

use hydracache::HydraCache;
use hydracache_sqlx::{CacheEntity, DbCache};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
    id: i64,
    name: String,
}

impl CacheEntity for User {
    type Id = i64;

    const ENTITY: &'static str = "user";
    const COLLECTION: Option<&'static str> = Some("users");
}

# async fn example() -> hydracache_sqlx::Result<()> {
let queries = DbCache::new(HydraCache::local().build(), "db");

let user = queries
    .for_entity::<User>(42)
    .fetch_with(|| async {
        Ok::<_, std::io::Error>(User {
            id: 42,
            name: "Ada".to_owned(),
        })
    })
    .await?;

assert_eq!(user.id, 42);
# Ok(())
# }

Semantics

  • CacheEntity::ENTITY provides the entity key/tag prefix.
  • CacheEntity::COLLECTION optionally provides a broader invalidation tag.
  • DbCache::for_entity::<User>(42) creates key user:42, tag user:42, and
    tag users for the example above.
  • DbQuery::for_cache_entity(42) applies the same metadata to an existing
    descriptor and preserves existing tags.
  • All generated segments use CacheKeyBuilder escaping.

Backward Compatibility

The 0.9.0 helpers remain available:

queries.entity::<User>("user", 42).collection_tag("users")

The full-control API also remains available:

queries.cached::<User>().key("user:42").tag("user:42")

CacheEntity is an additive metadata layer and the intended future expansion
target for a derive macro.

Validation

  • cargo fmt --all -- --check
  • cargo check --workspace --all-targets --locked
  • cargo test --workspace --all-targets --locked
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo test --doc --workspace --locked
  • cargo doc --workspace --no-deps --locked
  • cargo llvm-cov --workspace --all-targets --locked --summary-only
  • cargo package -p hydracache-core --locked --allow-dirty

The SQLx Postgres integration test uses testcontainers. It runs against a real
database when Docker is available and skips successfully when Docker is not
available.

Dependent package verification must be run in publish order after
hydracache-core 0.10.0 and then hydracache 0.10.0 are available on
crates.io.