pub struct KVCacheManager { /* private fields */ }Expand description
Main KV cache management engine
Implementations§
Source§impl KVCacheManager
impl KVCacheManager
Sourcepub fn new(
config: KVCacheConfig,
database: Arc<MemoryDatabase>,
llm_worker: Option<Arc<LLMWorker>>,
) -> Result<Self>
pub fn new( config: KVCacheConfig, database: Arc<MemoryDatabase>, llm_worker: Option<Arc<LLMWorker>>, ) -> Result<Self>
Create a new KV cache manager.
Pass llm_worker to enable pre-clear summarization; pass None to disable it.
Sourcepub async fn process_conversation(
&mut self,
session_id: &str,
messages: &[Message],
current_kv_entries: &[KVEntry],
_current_cache_size_bytes: usize,
max_cache_size_bytes: usize,
) -> Result<CacheProcessingResult>
pub async fn process_conversation( &mut self, session_id: &str, messages: &[Message], current_kv_entries: &[KVEntry], _current_cache_size_bytes: usize, max_cache_size_bytes: usize, ) -> Result<CacheProcessingResult>
Process a conversation and manage cache
Sourcepub fn should_clear_by_conversation(&self, conversation_count: usize) -> bool
pub fn should_clear_by_conversation(&self, conversation_count: usize) -> bool
Check if cache needs to be cleared based on conversation count
Sourcepub fn should_clear_by_memory(
&self,
current_usage_bytes: usize,
max_memory_bytes: usize,
) -> bool
pub fn should_clear_by_memory( &self, current_usage_bytes: usize, max_memory_bytes: usize, ) -> bool
Check if cache needs to be cleared based on memory usage
Sourcepub fn calculate_cache_memory_usage(&self, entries: &[KVEntry]) -> usize
pub fn calculate_cache_memory_usage(&self, entries: &[KVEntry]) -> usize
Calculate the actual memory usage of current cache entries
Sourcepub async fn clear_cache(
&mut self,
session_id: &str,
current_entries: &[KVEntry],
messages: &[Message],
reason: ClearReason,
) -> Result<CacheClearResult>
pub async fn clear_cache( &mut self, session_id: &str, current_entries: &[KVEntry], messages: &[Message], reason: ClearReason, ) -> Result<CacheClearResult>
Clear KV cache intelligently.
messages is the full conversation up to this point — used to generate a compact
pre-clear summary so the LLM can be re-fed cheaply when the session continues.
Sourcepub async fn retrieve_context(
&mut self,
session_id: &str,
query: &str,
current_cache_entries: &[KVEntry],
) -> Result<RetrievalResult>
pub async fn retrieve_context( &mut self, session_id: &str, query: &str, current_cache_entries: &[KVEntry], ) -> Result<RetrievalResult>
Retrieve relevant context from all tiers
Sourcepub fn set_llm_worker(&mut self, llm_worker: Arc<LLMWorker>)
pub fn set_llm_worker(&mut self, llm_worker: Arc<LLMWorker>)
Set the LLM worker reference
Sourcepub fn get_statistics(&self) -> &CacheStatistics
pub fn get_statistics(&self) -> &CacheStatistics
Get cache statistics
Sourcepub fn get_session_state(&self, session_id: &str) -> Option<&SessionCacheState>
pub fn get_session_state(&self, session_id: &str) -> Option<&SessionCacheState>
Get session state
Sourcepub fn get_all_session_states(&self) -> &HashMap<String, SessionCacheState>
pub fn get_all_session_states(&self) -> &HashMap<String, SessionCacheState>
Get all session states
Sourcepub async fn restore_from_snapshot(
&mut self,
session_id: &str,
snapshot_id: i64,
) -> Result<Vec<KVEntry>>
pub async fn restore_from_snapshot( &mut self, session_id: &str, snapshot_id: i64, ) -> Result<Vec<KVEntry>>
Restore cache from snapshot
Sourcepub async fn manual_clear_cache(
&mut self,
session_id: &str,
current_entries: &[KVEntry],
) -> Result<CacheClearResult>
pub async fn manual_clear_cache( &mut self, session_id: &str, current_entries: &[KVEntry], ) -> Result<CacheClearResult>
Manual cache clear (for testing or admin purposes). No messages are available in this path, so no pre-clear summary is generated.
Sourcepub async fn perform_maintenance(&mut self) -> Result<MaintenanceResult>
pub async fn perform_maintenance(&mut self) -> Result<MaintenanceResult>
Check cache health and perform maintenance if needed
Sourcepub fn export_statistics(&self) -> CacheStatisticsExport
pub fn export_statistics(&self) -> CacheStatisticsExport
Export cache statistics
Sourcepub fn get_config(&self) -> &KVCacheConfig
pub fn get_config(&self) -> &KVCacheConfig
Get configuration
Sourcepub fn update_config(&mut self, config: KVCacheConfig)
pub fn update_config(&mut self, config: KVCacheConfig)
Update configuration
Sourcepub async fn flush_to_database(
&self,
session_id: &str,
current_entries: &[KVEntry],
) -> Result<Option<i64>>
pub async fn flush_to_database( &self, session_id: &str, current_entries: &[KVEntry], ) -> Result<Option<i64>>
Flush current cache state to database for persistence
Sourcepub fn cache_scorer(&self) -> &CacheEntryScorer
pub fn cache_scorer(&self) -> &CacheEntryScorer
Get cache scorer reference
Sourcepub fn cache_scorer_mut(&mut self) -> &mut CacheEntryScorer
pub fn cache_scorer_mut(&mut self) -> &mut CacheEntryScorer
Get mutable cache scorer reference
Sourcepub fn reset_statistics(&mut self)
pub fn reset_statistics(&mut self)
Reset statistics
Sourcepub async fn shutdown_flush(&self) -> Result<()>
pub async fn shutdown_flush(&self) -> Result<()>
Shutdown method to flush all pending cache data to database
Auto Trait Implementations§
impl Freeze for KVCacheManager
impl !RefUnwindSafe for KVCacheManager
impl Send for KVCacheManager
impl Sync for KVCacheManager
impl Unpin for KVCacheManager
impl UnsafeUnpin for KVCacheManager
impl !UnwindSafe for KVCacheManager
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> 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