pub struct RedisCacheMiddlewareBuilder { /* private fields */ }Expand description
Builder for configuring and creating the RedisCacheMiddleware.
Provides a fluent interface for configuring cache parameters such as TTL, maximum cacheable size, cache key prefix, and cache decision predicates.
Implementations§
Source§impl RedisCacheMiddlewareBuilder
impl RedisCacheMiddlewareBuilder
Sourcepub fn new(redis_url: impl Into<String>) -> Self
pub fn new(redis_url: impl Into<String>) -> Self
Creates a new builder with the given Redis URL.
§Arguments
redis_url- The Redis connection URL (e.g., “redis://127.0.0.1:6379”)
§Returns
A new RedisCacheMiddlewareBuilder with default settings:
- TTL: 3600 seconds (1 hour)
- Max cacheable size: 1MB
- Cache prefix: “cache:”
- Cache predicate: cache all responses
Sourcepub fn max_cacheable_size(self, bytes: usize) -> Self
pub fn max_cacheable_size(self, bytes: usize) -> Self
Sourcepub fn cache_prefix(self, prefix: impl Into<String>) -> Self
pub fn cache_prefix(self, prefix: impl Into<String>) -> Self
Sourcepub fn cache_if<F>(self, predicate: F) -> Self
pub fn cache_if<F>(self, predicate: F) -> Self
Set a predicate function to determine if a response should be cached
Example:
builder.cache_if(|ctx| {
// Only cache GET requests
if ctx.method != "GET" {
return false;
}
// Don't cache if Authorization header is present
if ctx.headers.contains_key("Authorization") {
return false;
}
// Don't cache responses to paths that start with /admin
if ctx.path.starts_with("/admin") {
return false;
}
// Don't cache for a specific route if its body contains some field
if ctx.path.starts_with("/api/users") && ctx.method == "POST" {
if let Ok(user_json) = serde_json::from_slice::<serde_json::Value>(ctx.body) {
// Check properties in the JSON to make caching decisions
return user_json.get("role").and_then(|r| r.as_str()) != Some("admin");
}
}
true
})Sourcepub fn build(self) -> RedisCacheMiddleware
pub fn build(self) -> RedisCacheMiddleware
Builds and returns the configured RedisCacheMiddleware.
§Returns
A new RedisCacheMiddleware instance configured with the settings from this builder.
Auto Trait Implementations§
impl Freeze for RedisCacheMiddlewareBuilder
impl !RefUnwindSafe for RedisCacheMiddlewareBuilder
impl Send for RedisCacheMiddlewareBuilder
impl Sync for RedisCacheMiddlewareBuilder
impl Unpin for RedisCacheMiddlewareBuilder
impl !UnwindSafe for RedisCacheMiddlewareBuilder
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
Mutably borrows from an owned value. Read more