Struct RedisCacheMiddlewareBuilder

Source
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

Source

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
Source

pub fn ttl(self, seconds: u64) -> Self

Sets the TTL (time-to-live) for cached responses in seconds.

§Arguments
  • seconds - The number of seconds a response should remain in the cache
§Returns

Self for method chaining

Source

pub fn max_cacheable_size(self, bytes: usize) -> Self

Sets the maximum size of responses that can be cached, in bytes.

Responses larger than this size will not be cached.

§Arguments
  • bytes - The maximum cacheable response size in bytes
§Returns

Self for method chaining

Source

pub fn cache_prefix(self, prefix: impl Into<String>) -> Self

Sets the prefix used for Redis cache keys.

§Arguments
  • prefix - The string prefix to use for all cache keys
§Returns

Self for method chaining

Source

pub fn cache_if<F>(self, predicate: F) -> Self
where F: Fn(&CacheContext<'_>) -> bool + Send + Sync + 'static,

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" {
        return ctx.body.get("role").and_then(|r| r.as_str()) != Some("admin");
    }
    true
})
Source

pub fn with_cache_key<F>(self, key_fn: F) -> Self
where F: Fn(&CacheContext<'_>) -> String + Send + Sync + 'static,

Set a custom function to determine the cache key.

By default, cache keys are based on HTTP method, path, query string, and (if present) a hash of the request body. This method lets you specify a custom function to generate the base key before hashing.

Example:

builder.with_cache_key(|ctx| {
    // Only use method and path for the cache key (ignore query params and body)
    format!("{}:{}", ctx.method, ctx.path)
     
    // Or include specific query parameters
    // let user_id = ctx.query_string.split('&')
    //     .find(|p| p.starts_with("user_id="))
    //     .unwrap_or("");
    // format!("{}:{}:{}", ctx.method, ctx.path, user_id)
})
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,