#[cfg(feature = "cache")]
use crate::cache::{CacheManager, CacheStats, CombinedCacheStats};
#[cfg(feature = "cache")]
use alloc::string::String;
#[cfg(feature = "cache")]
use crate::context::{CombinedContext, ContextProvider};
#[cfg(feature = "cache")]
use super::Router;
#[cfg(feature = "cache")]
impl<C: ContextProvider> Router<C> {
pub fn get_cached_result(&mut self, query_hash: u64) -> Option<&crate::cache::CacheEntry> {
if !self.config.cache_enabled || !self.cache.is_enabled() {
return None;
}
self.cache.query_cache.get(query_hash)
}
pub fn cache_result(&mut self, query_hash: u64, result: String, source_id: String) {
if !self.config.cache_enabled || !self.cache.is_enabled() {
return;
}
self.cache.query_cache.put(query_hash, result, source_id);
}
pub fn cache_result_with_ttl(
&mut self,
query_hash: u64,
result: String,
source_id: String,
ttl_ms: u64,
) {
if !self.config.cache_enabled || !self.cache.is_enabled() {
return;
}
self.cache
.query_cache
.put_with_ttl(query_hash, result, source_id, ttl_ms);
}
pub fn invalidate_cached_result(&mut self, query_hash: u64) {
self.cache.query_cache.invalidate(query_hash);
}
#[must_use]
pub fn is_result_cached(&self, query_hash: u64) -> bool {
if !self.config.cache_enabled || !self.cache.is_enabled() {
return false;
}
self.cache.query_cache.contains(query_hash)
}
pub fn get_cached_context(&mut self, provider_id: &str) -> Option<&CombinedContext> {
if !self.config.cache_enabled || !self.cache.is_enabled() {
return None;
}
self.cache.context_cache.get(provider_id)
}
pub fn cache_context(&mut self, provider_id: impl Into<String>, context: CombinedContext) {
if !self.config.cache_enabled || !self.cache.is_enabled() {
return;
}
self.cache.context_cache.put(provider_id, context);
}
#[must_use]
pub fn cache_stats(&self) -> CombinedCacheStats {
self.cache.combined_stats()
}
#[must_use]
pub fn query_cache_stats(&self) -> &CacheStats {
self.cache.query_cache.stats()
}
#[must_use]
pub fn cache_hit_rate(&self) -> f64 {
self.cache.query_cache.stats().hit_rate()
}
pub fn clear_cache(&mut self) {
self.cache.clear_all();
}
pub fn clear_query_cache(&mut self) {
self.cache.query_cache.clear();
}
pub fn cleanup_cache(&mut self) -> usize {
self.cache.cleanup_all()
}
pub fn enable_cache(&mut self) {
self.config.cache_enabled = true;
self.cache.enable();
}
pub fn disable_cache(&mut self) {
self.config.cache_enabled = false;
self.cache.disable();
}
#[must_use]
pub const fn is_cache_enabled(&self) -> bool {
self.config.cache_enabled
}
#[must_use]
pub fn cached_query_count(&self) -> usize {
self.cache.query_cache.len()
}
pub fn cache_mut(&mut self) -> &mut CacheManager {
&mut self.cache
}
#[must_use]
pub const fn cache(&self) -> &CacheManager {
&self.cache
}
}