Skip to main content

CacheKey

Trait CacheKey 

Source
pub trait CacheKey: Send + Sync {
    // Required method
    fn to_key_string(&self) -> String;
}
Expand description

Trait for types that can be used as cache keys

This trait provides a way to convert any type into a string representation suitable for use as a cache key. Blanket implementations are provided for common types like String, &str, and numeric types.

§Example

use oxcache::traits::CacheKey;

// Using blanket implementation for String
let key: String = "user:123".to_string();
assert_eq!(key.to_key_string(), "user:123");

// Using blanket implementation for u64
let id: u64 = 12345;
assert_eq!(id.to_key_string(), "12345");

// Custom implementation
struct UserId(u64);

impl CacheKey for UserId {
    fn to_key_string(&self) -> String {
        format!("user:{}", self.0)
    }
}

let user_id = UserId(12345);
assert_eq!(user_id.to_key_string(), "user:12345");

Required Methods§

Source

fn to_key_string(&self) -> String

Convert this key to a string representation

This method should return a unique string representation that can be used as a cache key. The implementation should be deterministic and produce the same output for equal values.

§Returns

A string representation of this key

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl CacheKey for &str

Source§

impl CacheKey for String

Source§

impl CacheKey for i32

Source§

impl CacheKey for i64

Source§

impl CacheKey for isize

Source§

impl CacheKey for u32

Source§

impl CacheKey for u64

Source§

impl CacheKey for u128

Source§

impl CacheKey for usize

Implementors§