CacheableKey

Trait CacheableKey 

Source
pub trait CacheableKey {
    // Required method
    fn to_cache_key(&self) -> String;
}
Expand description

Trait defining how to generate a cache key for a given type.

This trait must be implemented for any type that will be used as a function parameter in a cached function. The cache key is used to uniquely identify cached results.

§Examples

use cachelito_core::CacheableKey;

#[derive(Debug)]
struct UserId(u64);

impl CacheableKey for UserId {
    fn to_cache_key(&self) -> String {
        format!("user_{}", self.0)
    }
}

Required Methods§

Source

fn to_cache_key(&self) -> String

Converts this value into a string that can be used as a cache key.

The returned string should uniquely identify the value to ensure correct cache behavior.

Implementors§

Source§

impl<T> CacheableKey for T

Blanket implementation for any type that explicitly opts in via DefaultCacheableKey.

This automatically implements CacheableKey::to_cache_key() for any type that implements DefaultCacheableKey, using the type’s Debug representation as the key.

§Performance Note

The cache key is generated by formatting the value with {:?}. For complex types, consider implementing CacheableKey directly for better performance.