pub struct CacheLayer { /* private fields */ }Expand description
A two-backend response cache: in-memory (default) or Redis (feature caching).
Use CacheLayer::new_memory for single-process deployments or testing.
Use CacheLayer::new_redis (feature caching) when multiple pipeline instances need a shared
cache.
§Eviction
The in-memory backend evicts the entry with the nearest expiry when the
max_entries limit is reached (O(n) scan). For large caches consider a
dedicated LRU crate.
Entries with ttl_secs == 0 are silently ignored by CacheLayer::set.
§Thread safety
CacheLayer is Clone + Send + Sync. All clones share the same
Arc<MemoryCache>.
§Examples
use tokio_prompt_orchestrator::enhanced::CacheLayer;
let cache = CacheLayer::new_memory(1000);
cache.set("prompt:abc", "response text", 3600).await;
if let Some(hit) = cache.get("prompt:abc").await {
println!("cache hit: {hit}");
}Implementations§
Source§impl CacheLayer
impl CacheLayer
Sourcepub fn new_memory(max_entries: usize) -> Self
pub fn new_memory(max_entries: usize) -> Self
Create an in-memory CacheLayer with a maximum entry count.
When max_entries is reached, the entry nearest to expiry is evicted
before inserting the new value.
§Arguments
max_entries— Soft cap on the number of cached entries. Use0to disable the cap (entries accumulate until evicted by TTL checks onget).
§Examples
use tokio_prompt_orchestrator::enhanced::CacheLayer;
let cache = CacheLayer::new_memory(500);Sourcepub async fn get_checked(&self, key: &str) -> Result<Option<String>, String>
pub async fn get_checked(&self, key: &str) -> Result<Option<String>, String>
Get a cached value, distinguishing cache misses from backend errors.
Returns:
Ok(Some(value))— cache hitOk(None)— cache missErr(msg)— backend error (treat as miss but log it)
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Trait Implementations§
Source§impl Clone for CacheLayer
impl Clone for CacheLayer
Source§fn clone(&self) -> CacheLayer
fn clone(&self) -> CacheLayer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for CacheLayer
impl !UnwindSafe for CacheLayer
impl Freeze for CacheLayer
impl Send for CacheLayer
impl Sync for CacheLayer
impl Unpin for CacheLayer
impl UnsafeUnpin for CacheLayer
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request