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§
Sourcefn to_key_string(&self) -> String
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".