RedisCache

Struct RedisCache 

Source
pub struct RedisCache { /* private fields */ }
Expand description

Redis cache implementation that provides various caching operations

Implementations§

Source§

impl RedisCache

Source

pub async fn conn(&self) -> Result<RedisClientConn, Error>

Source

pub fn new(client: &'static RedisClient) -> Self

Creates a new RedisCacheBuilder with default settings:

  • TTL: 10 minutes
  • Empty prefix
  • Given Redis pool
Source

pub fn with_ttl(self, ttl: Duration) -> Self

Sets the time-to-live duration for cache entries Returns self for method chaining

Source

pub fn with_prefix(self, prefix: String) -> Self

Sets the prefix for all cache keys Returns self for method chaining

Source

pub async fn ping(&self) -> Result<(), Error>

Pings the Redis server to check connection

§Returns
  • Ok(()) - Connection is successful
  • Err(Error) - Redis operation failed
Source

pub async fn lock( &self, key: &str, ttl: Option<Duration>, ) -> Result<bool, Error>

Attempts to acquire a distributed lock using Redis SET NX command

§Arguments
  • key - The lock key
  • ttl - Optional lock duration (uses instance default if None)
§Returns
  • Ok(true) - Lock was successfully acquired
  • Ok(false) - Lock already exists
  • Err(Error) - Redis operation failed
Source

pub async fn del(&self, key: &str) -> Result<(), Error>

Removes a key and its value from Redis

§Arguments
  • key - The key to delete
§Returns
  • Ok(()) - Key was successfully deleted (or didn’t exist)
  • Err(Error) - Redis operation failed
Source

pub async fn incr( &self, key: &str, delta: i64, ttl: Option<Duration>, ) -> Result<i64, Error>

Atomically increments a counter by delta

§Arguments
  • key - The counter key
  • delta - Amount to increment by (can be negative)
  • ttl - Optional time-to-live for the counter
§Returns
  • Ok(i64) - The new value after incrementing
  • Err(Error) - Redis operation failed
§Notes

If the key doesn’t exist, it’s initialized to 0 with ttl before incrementing

Source

pub async fn set<T: ToRedisArgs + Send + Sync>( &self, key: &str, value: T, ttl: Option<Duration>, ) -> Result<(), Error>

Sets a value in Redis with an optional TTL

  • If TTL is None, uses the default TTL configured for this cache
  • Value type must implement ToRedisArgs trait
  • Key will be automatically prefixed if a prefix is configured
Source

pub async fn get<T: FromRedisValue>(&self, key: &str) -> Result<T, Error>

Retrieves a value from Redis

  • Value type must implement FromRedisValue trait
  • Key will be automatically prefixed if a prefix is configured
  • Returns Error if key doesn’t exist or value can’t be converted to T
Source

pub async fn set_struct<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
where T: ?Sized + Serialize,

Serializes and stores a struct in Redis as JSON

  • Value must implement Serialize trait
  • Optional TTL (uses default if None)
  • Key will be automatically prefixed
Source

pub async fn get_struct<T>(&self, key: &str) -> Result<Option<T>, Error>

Retrieves and deserializes a struct from Redis

  • Type must implement DeserializeOwned trait
  • Returns None if key doesn’t exist
  • Returns Error if deserialization fails
  • Key will be automatically prefixed
Source

pub async fn ttl(&self, key: &str) -> Result<i32, Error>

Gets the remaining time-to-live for a key

§Arguments
  • key - The key to check
§Returns
  • Ok(seconds) where:
    • seconds > 0 - Remaining time in seconds
    • seconds = -2 - Key does not exist
    • seconds = -1 - Key exists but has no expiry
  • Err(Error) - Redis operation failed
Source

pub async fn get_del<T: FromRedisValue>(&self, key: &str) -> Result<T, Error>

Atomically retrieves a value and deletes it from Redis(>=6.2.0)

§Type Parameters
  • T - The type to deserialize the Redis value into
§Arguments
  • key - The key to get and delete
§Returns
  • Ok(T) - The value before deletion
  • Err(Error) - Redis operation failed or value conversion error
Source

pub async fn set_struct_lz4<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
where T: ?Sized + Serialize,

Serializes a struct to JSON, compresses it with LZ4, and stores in Redis

§Type Parameters
  • T - The struct type to serialize
§Arguments
  • key - The key under which to store the compressed data
  • value - The struct to serialize and compress
  • ttl - Optional time-to-live duration
§Notes

Uses LZ4 compression which favors speed over compression ratio

Source

pub async fn get_struct_lz4<T>(&self, key: &str) -> Result<Option<T>, Error>

Retrieves, decompresses (LZ4), and deserializes a struct from Redis

§Type Parameters
  • T - The struct type to deserialize into
§Arguments
  • key - The key to retrieve
§Returns
  • Ok(Some(T)) - Successfully retrieved and deserialized value
  • Ok(None) - Key doesn’t exist
  • Err(Error) - Redis, decompression, or deserialization error
Source

pub async fn set_struct_zstd<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
where T: ?Sized + Serialize,

Serializes a struct to JSON, compresses it with Zstd, and stores in Redis

§Type Parameters
  • T - The struct type to serialize
§Arguments
  • key - The key under which to store the compressed data
  • value - The struct to serialize and compress
  • ttl - Optional time-to-live duration
§Notes

Uses Zstd compression which provides better compression ratios than LZ4

Source

pub async fn get_struct_zstd<T>(&self, key: &str) -> Result<Option<T>, Error>

Retrieves, decompresses (Zstd), and deserializes a struct from Redis

§Type Parameters
  • T - The struct type to deserialize into
§Arguments
  • key - The key to retrieve
§Returns
  • Ok(Some(T)) - Successfully retrieved and deserialized value
  • Ok(None) - Key doesn’t exist
  • Err(Error) - Redis, decompression, or deserialization error

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,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,