pub struct CacheLayer { /* private fields */ }Expand description
A Tower layer that shares one local cache and invalidation lifecycle across every service it creates.
The wrapped service must implement ReleaseReadiness. Put this cache
directly above that backend, then put tracing, metrics, timeout, and other
middleware outside the cache. If custom middleware sits inside the cache,
it must propagate the readiness-release contract to its own inner service.
Implementations§
Source§impl CacheLayer
impl CacheLayer
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Create a layer without server invalidations.
§Staleness warning
This constructor cannot guarantee cache coherence. Entries may remain
stale until ttl expires. Prefer a tracked cached client or
with_invalidation_stream.
Sourcepub fn without_invalidation(config: CacheConfig) -> Self
pub fn without_invalidation(config: CacheConfig) -> Self
Explicitly create a layer without an invalidation source.
Sourcepub fn without_invalidation_with_recorder(
config: CacheConfig,
recorder: Arc<dyn MetricsRecorder>,
) -> Self
pub fn without_invalidation_with_recorder( config: CacheConfig, recorder: Arc<dyn MetricsRecorder>, ) -> Self
Create an invalidation-free layer with a cache metrics recorder.
Sourcepub fn with_invalidation_stream<T>(config: CacheConfig, push_stream: T) -> Self
pub fn with_invalidation_stream<T>(config: CacheConfig, push_stream: T) -> Self
Create a layer that owns push_stream until the final layer/service
clone is dropped. Stream termination clears and disables the cache.
§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>(
config: CacheConfig,
push_stream: T,
recorder: Arc<dyn MetricsRecorder>,
) -> Self
pub fn with_invalidation_stream_and_recorder<T>( config: CacheConfig, push_stream: T, recorder: Arc<dyn MetricsRecorder>, ) -> Self
Create a recorder-enabled layer that owns its invalidation stream.
This is the production counterpart to
without_invalidation_with_recorder:
cache events are forwarded to recorder, and stream termination fails
the cache closed.
§Panics
Panics when called outside an active Tokio runtime because ownership of the stream is maintained by a spawned task.