Skip to main content

CacheEntity

Trait CacheEntity 

Source
pub trait CacheEntity {
    type Id: ToString;

    const ENTITY: &'static str;
    const COLLECTION: Option<&'static str>;

    // Provided methods
    fn cache_key_for(id: &Self::Id) -> String { ... }
    fn entity_tag_for(id: &Self::Id) -> String { ... }
    fn collection_tag() -> Option<String> { ... }
}
Expand description

Static cache metadata for a domain entity.

Implement this trait manually when you want domain-shaped cache calls without repeating entity and collection names at every query site. A future derive macro can generate the same implementation.

§Example

use hydracache_db::CacheEntity;

struct User;

impl CacheEntity for User {
    type Id = i64;

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

assert_eq!(User::cache_key_for(&42), "user:42");
assert_eq!(User::entity_tag_for(&42), "user:42");
assert_eq!(User::collection_tag(), Some("users".to_owned()));

Required Associated Constants§

Source

const ENTITY: &'static str

Stable entity segment used in keys and entity tags.

Source

const COLLECTION: Option<&'static str>

Optional collection tag for broader invalidation groups.

Required Associated Types§

Source

type Id: ToString

Identifier type used to build entity keys and tags.

Provided Methods§

Source

fn cache_key_for(id: &Self::Id) -> String

Build the logical cache key for this entity id.

Source

fn entity_tag_for(id: &Self::Id) -> String

Build the entity invalidation tag for this id.

Source

fn collection_tag() -> Option<String>

Build the optional collection invalidation tag.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§