pub struct QueryCache<K, V> { /* private fields */ }Expand description
A thread-safe LRU cache with per-entry TTL expiry.
§Type Parameters
K– Key type. Must beHash + Eq + Clone + Send + Sync.V– Value type. Must beClone + Send + Sync.
§Example
use std::num::NonZeroUsize;
use std::time::Duration;
use oxirs_graphrag::cache::query_cache::{QueryCache, QueryCacheConfig};
let config = QueryCacheConfig {
capacity: NonZeroUsize::new(100).unwrap(),
default_ttl: Duration::from_secs(60),
..Default::default()
};
let cache: QueryCache<String, String> = QueryCache::new(config);
cache.put("hello".to_string(), "world".to_string()).unwrap();
assert_eq!(cache.get(&"hello".to_string()).unwrap(), Some("world".to_string()));Implementations§
Source§impl<K, V> QueryCache<K, V>
impl<K, V> QueryCache<K, V>
Sourcepub fn new(config: QueryCacheConfig) -> Self
pub fn new(config: QueryCacheConfig) -> Self
Create a new QueryCache from the given configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a QueryCache with default configuration.
Sourcepub fn get(&self, key: &K) -> GraphRAGResult<Option<V>>
pub fn get(&self, key: &K) -> GraphRAGResult<Option<V>>
Look up key and return the value if present and fresh.
Stale entries are evicted; their keys count as misses.
Sourcepub fn put(&self, key: K, value: V) -> GraphRAGResult<()>
pub fn put(&self, key: K, value: V) -> GraphRAGResult<()>
Insert value under key using the cache’s default TTL.
Sourcepub fn put_with_ttl(
&self,
key: K,
value: V,
ttl: Duration,
) -> GraphRAGResult<()>
pub fn put_with_ttl( &self, key: K, value: V, ttl: Duration, ) -> GraphRAGResult<()>
Insert value under key with an explicit TTL.
The TTL is clamped to [min_ttl, max_ttl] from the config.
Sourcepub fn remove(&self, key: &K) -> GraphRAGResult<Option<V>>
pub fn remove(&self, key: &K) -> GraphRAGResult<Option<V>>
Remove an entry. Returns the value if it existed and was fresh.
Sourcepub fn evict_expired(&self) -> GraphRAGResult<usize>
pub fn evict_expired(&self) -> GraphRAGResult<usize>
Evict all stale entries and return the count of evictions.
This is O(n) in the number of entries. Call it from a periodic maintenance task to keep memory usage bounded.
Sourcepub fn stats(&self) -> GraphRAGResult<CacheStats>
pub fn stats(&self) -> GraphRAGResult<CacheStats>
Return a snapshot of cache performance statistics.
Sourcepub fn peek_entry<F, R>(&self, key: &K, f: F) -> GraphRAGResult<Option<R>>where
F: FnOnce(&CacheEntry<V>) -> R,
pub fn peek_entry<F, R>(&self, key: &K, f: F) -> GraphRAGResult<Option<R>>where
F: FnOnce(&CacheEntry<V>) -> R,
Peek at an entry’s metadata without affecting LRU order.
Returns None if the entry is absent or stale.
Sourcepub fn clear(&self) -> GraphRAGResult<()>
pub fn clear(&self) -> GraphRAGResult<()>
Clear all entries from the cache.
Sourcepub fn len(&self) -> GraphRAGResult<usize>
pub fn len(&self) -> GraphRAGResult<usize>
Return the current number of live (non-stale) entries.
Note: this includes any entries that might have expired between insert time and this call but have not yet been lazily evicted.
Sourcepub fn is_empty(&self) -> GraphRAGResult<bool>
pub fn is_empty(&self) -> GraphRAGResult<bool>
Return true if the cache contains no entries.
Sourcepub fn capacity(&self) -> GraphRAGResult<usize>
pub fn capacity(&self) -> GraphRAGResult<usize>
Return the maximum capacity of the cache.
Trait Implementations§
Source§impl<K: Clone, V: Clone> Clone for QueryCache<K, V>
impl<K: Clone, V: Clone> Clone for QueryCache<K, V>
Source§fn clone(&self) -> QueryCache<K, V>
fn clone(&self) -> QueryCache<K, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<K, V> Freeze for QueryCache<K, V>
impl<K, V> RefUnwindSafe for QueryCache<K, V>
impl<K, V> Send for QueryCache<K, V>
impl<K, V> Sync for QueryCache<K, V>
impl<K, V> Unpin for QueryCache<K, V>
impl<K, V> UnsafeUnpin for QueryCache<K, V>
impl<K, V> UnwindSafe for QueryCache<K, V>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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>
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