pub struct OperationCache { /* private fields */ }Expand description
True LRU cache for immutable MCP operations.
Thread-safe cache using DashMap for concurrent access with true LRU eviction.
Both get() and insert() update the access timestamp, ensuring frequently
accessed entries are retained.
§Example
use hedl_mcp::cache::OperationCache;
use serde_json::json;
let cache = OperationCache::new(1000);
// Cache a validation result
let result = json!({"valid": true});
cache.insert("validate", "my hedl content", result.clone());
// Retrieve from cache (updates LRU position)
if let Some(cached) = cache.get("validate", "my hedl content") {
assert_eq!(cached, result);
}Implementations§
Source§impl OperationCache
impl OperationCache
Sourcepub fn get(&self, operation: &str, input: &str) -> Option<JsonValue>
pub fn get(&self, operation: &str, input: &str) -> Option<JsonValue>
Get a cached result if available, updating its LRU position.
§Arguments
operation- Operation name (e.g., “validate”, “lint”)input- Input content (used for cache key hash)
§Returns
Cached result if available, None otherwise.
§Examples
use hedl_mcp::cache::OperationCache;
let cache = OperationCache::new(1000);
let result = cache.get("validate", "%VERSION 1.0\n---");
assert!(result.is_none()); // Cache miss on first accessSourcepub fn insert(&self, operation: &str, input: &str, result: JsonValue)
pub fn insert(&self, operation: &str, input: &str, result: JsonValue)
Insert a result into the cache.
If the cache is full, evicts the least recently used entry.
If max_size is 0, this is a no-op (cache disabled).
§Arguments
operation- Operation name (e.g., “validate”, “lint”)input- Input content (used for cache key hash)result- Result to cache
§Examples
use hedl_mcp::cache::OperationCache;
use serde_json::json;
let cache = OperationCache::new(1000);
cache.insert("validate", "%VERSION 1.0\n---", json!({"valid": true}));Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Get cache statistics.
§Returns
Current cache statistics including hit/miss counts and hit rate.
§Examples
use hedl_mcp::cache::OperationCache;
use serde_json::json;
let cache = OperationCache::new(1000);
cache.insert("validate", "input", json!({"valid": true}));
cache.get("validate", "input"); // Hit
cache.get("validate", "other"); // Miss
let stats = cache.stats();
assert_eq!(stats.hits, 1);
assert_eq!(stats.misses, 1);
assert_eq!(stats.size, 1);Sourcepub fn clear(&self)
pub fn clear(&self)
Clear all cache entries.
§Examples
use hedl_mcp::cache::OperationCache;
use serde_json::json;
let cache = OperationCache::new(1000);
cache.insert("validate", "input", json!({"valid": true}));
assert_eq!(cache.stats().size, 1);
cache.clear();
assert_eq!(cache.stats().size, 0);Sourcepub fn reset_stats(&self)
pub fn reset_stats(&self)
Reset cache statistics (hit/miss/eviction counters).
Does not clear the cache itself, only resets the counters.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for OperationCache
impl !RefUnwindSafe for OperationCache
impl Send for OperationCache
impl Sync for OperationCache
impl Unpin for OperationCache
impl UnwindSafe for OperationCache
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more