pub struct CacheKey { /* private fields */ }Expand description
A cache key identifying a cached entry.
Cache keys are composed of:
- A prefix for namespacing (e.g., “api”, “users”)
- A version number for cache invalidation
- A list of parts (key-value pairs) extracted from requests
§Cheap Cloning
CacheKey wraps its data in Arc, making clone() an O(1) operation
that only increments a reference count. This is important because keys
are frequently passed around during cache operations.
§Example
use hitbox_core::{CacheKey, KeyPart};
let key = CacheKey::new(
"api",
1,
vec![
KeyPart::new("method", Some("GET")),
KeyPart::new("path", Some("/users/123")),
],
);
assert_eq!(key.prefix(), "api");
assert_eq!(key.version(), 1);
assert_eq!(format!("{}", key), "api:v1:method=GET&path=/users/123");Implementations§
Source§impl CacheKey
impl CacheKey
Sourcepub fn memory_size(&self) -> usize
pub fn memory_size(&self) -> usize
Returns the estimated memory usage of this cache key in bytes.
This includes:
- Arc heap allocation (control block + CacheKeyInner)
- Vec heap allocation (KeyPart elements)
- SmolStr heap allocations (strings >23 bytes)
Sourcepub fn new(
prefix: impl Into<SmolStr>,
version: u32,
parts: Vec<KeyPart>,
) -> Self
pub fn new( prefix: impl Into<SmolStr>, version: u32, parts: Vec<KeyPart>, ) -> Self
Creates a new cache key with the given components.
§Arguments
prefix- Namespace prefix for the keyversion- Version number for cache invalidationparts- List of key-value parts
Sourcepub fn from_str(key: &str, value: &str) -> Self
pub fn from_str(key: &str, value: &str) -> Self
Creates a simple cache key with a single key-value part.
The prefix is empty and version is 0.
Sourcepub fn from_slice(parts: &[(&str, Option<&str>)]) -> Self
pub fn from_slice(parts: &[(&str, Option<&str>)]) -> Self
Creates a cache key from a slice of key-value pairs.
The prefix is empty and version is 0.
Trait Implementations§
impl<'de> Decode<'de> for CacheKey
impl Encode for CacheKey
impl Eq for CacheKey
Auto Trait Implementations§
impl Freeze for CacheKey
impl RefUnwindSafe for CacheKey
impl Send for CacheKey
impl Sync for CacheKey
impl Unpin for CacheKey
impl UnwindSafe for CacheKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more