pub struct CacheService<S> { /* private fields */ }Expand description
Tower Service that caches Frame responses for cacheable read commands.
The local response cache supports GET, HGET, HGETALL, LRANGE,
SMEMBERS, ZRANGE, and TYPE. Other commands pass through to the inner
service; known mutations synchronously invalidate affected entries.
Clones share cache state and the owned invalidation lifecycle. Cacheable misses use per-key epochs so a response racing an invalidation is never inserted. Non-cacheable commands are classified and locally invalidated both before and after dispatch.
The inner service must implement ReleaseReadiness because a cache hit
returns without calling it after Tower readiness has already been acquired.
Implementations§
Source§impl<S> CacheService<S>
impl<S> CacheService<S>
Sourcepub fn new(inner: S, config: CacheConfig) -> Self
pub fn new(inner: S, config: CacheConfig) -> Self
Wrap inner without server invalidations.
§Staleness warning
This compatibility constructor cannot guarantee cache coherence.
Entries may remain stale until their TTL. Prefer a tracked cached
client, CacheLayer::with_invalidation_stream, or
with_invalidation_stream.
Sourcepub fn without_invalidation(inner: S, config: CacheConfig) -> Self
pub fn without_invalidation(inner: S, config: CacheConfig) -> Self
Explicitly wrap inner without an invalidation source.
Sourcepub fn with_cache(inner: S, cache: Arc<RwLock<CacheState>>) -> Self
pub fn with_cache(inner: S, cache: Arc<RwLock<CacheState>>) -> Self
Create with existing shared cache state and no invalidation source.
The caller is responsible for driving invalidations and failing the
cache closed if that source ends. Prefer
with_invalidation_stream, or pass
this cache to spawn_invalidation_task and retain its task handle.
Sourcepub fn with_invalidation_stream<T>(
inner: S,
config: CacheConfig,
push_stream: T,
) -> Self
pub fn with_invalidation_stream<T>( inner: S, config: CacheConfig, push_stream: T, ) -> Self
Wrap an inner service and own its invalidation stream across clones.
§Panics
Panics when called outside an active Tokio runtime because ownership of the stream is maintained by a spawned task.
Sourcepub fn with_invalidation_stream_and_recorder<T>(
inner: S,
config: CacheConfig,
push_stream: T,
recorder: Arc<dyn MetricsRecorder>,
) -> Self
pub fn with_invalidation_stream_and_recorder<T>( inner: S, config: CacheConfig, push_stream: T, recorder: Arc<dyn MetricsRecorder>, ) -> Self
Wrap an inner service with an owned invalidation stream and cache-event recorder.
§Panics
Panics when called outside an active Tokio runtime because ownership of the stream is maintained by a spawned task.
Source§impl<S> CacheService<S>
impl<S> CacheService<S>
Sourcepub fn into_inner(self) -> S
pub fn into_inner(self) -> S
Consume this middleware and return the wrapped service.
Dropping the service also drops this clone’s invalidation lease.
Sourcepub async fn cache_size(&self) -> usize
pub async fn cache_size(&self) -> usize
Get the current number of entries.
Sourcepub async fn clear_cache(&self)
pub async fn clear_cache(&self)
Clear all local entries.
Sourcepub async fn cache_statistics(&self) -> CacheStatistics
pub async fn cache_statistics(&self) -> CacheStatistics
Return aggregate cache statistics.
Sourcepub async fn is_caching_healthy(&self) -> bool
pub async fn is_caching_healthy(&self) -> bool
Whether cache use is enabled, no mutation is in flight, and any managed data worker remains healthy.
Source§impl CacheService<AutoPipelineService>
impl CacheService<AutoPipelineService>
Sourcepub fn queue_depth(&self) -> usize
pub fn queue_depth(&self) -> usize
Return the number of requests currently queued in the worker.
Sourcepub async fn call_pipeline(
&mut self,
frames: Vec<Frame>,
) -> Result<Vec<Frame>, RedisError>
pub async fn call_pipeline( &mut self, frames: Vec<Frame>, ) -> Result<Vec<Frame>, RedisError>
Submit an explicit batch atomically and conservatively clear the cache before and after dispatch.
Trait Implementations§
Source§impl<S: Clone> Clone for CacheService<S>
impl<S: Clone> Clone for CacheService<S>
Source§impl<S> Service<Frame> for CacheService<S>where
S: Service<Frame, Response = Frame, Error = RedisError> + ReleaseReadiness,
S::Future: Send + 'static,
impl<S> Service<Frame> for CacheService<S>where
S: Service<Frame, Response = Frame, Error = RedisError> + ReleaseReadiness,
S::Future: Send + 'static,
Source§type Error = RedisError
type Error = RedisError
Source§type Future = Pin<Box<dyn Future<Output = Result<Frame, RedisError>> + Send>>
type Future = Pin<Box<dyn Future<Output = Result<Frame, RedisError>> + Send>>
Auto Trait Implementations§
impl<S> !RefUnwindSafe for CacheService<S>
impl<S> !UnwindSafe for CacheService<S>
impl<S> Freeze for CacheService<S>
impl<S> Send for CacheService<S>
impl<S> Sync for CacheService<S>
impl<S> Unpin for CacheService<S>
impl<S> UnsafeUnpin for CacheService<S>where
S: UnsafeUnpin,
Option<fn(&mut S, Frame) -> Pin<Box<dyn Future<Output = Result<Frame, RedisError>> + Send>>>: UnsafeUnpin,
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,
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> ⓘ
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request> ⓘwhere
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request> ⓘwhere
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request> ⓘwhere
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request> ⓘwhere
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request> ⓘwhere
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request> ⓘwhere
Self: Sized,
Service, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read more