pub struct RedisCache { /* private fields */ }Expand description
Redis cache implementation that provides various caching operations
Implementations§
Source§impl RedisCache
impl RedisCache
pub async fn conn(&self) -> Result<RedisClientConn, Error>
Sourcepub fn new(client: &'static RedisClient) -> Self
pub fn new(client: &'static RedisClient) -> Self
Creates a new RedisCacheBuilder with default settings:
- TTL: 10 minutes
- Empty prefix
- Given Redis pool
Sourcepub fn with_ttl(self, ttl: Duration) -> Self
pub fn with_ttl(self, ttl: Duration) -> Self
Sets the time-to-live duration for cache entries Returns self for method chaining
Sourcepub fn with_prefix(self, prefix: String) -> Self
pub fn with_prefix(self, prefix: String) -> Self
Sets the prefix for all cache keys Returns self for method chaining
Sourcepub async fn ping(&self) -> Result<(), Error>
pub async fn ping(&self) -> Result<(), Error>
Pings the Redis server to check connection
§Returns
Ok(())- Connection is successfulErr(Error)- Redis operation failed
Sourcepub async fn incr(
&self,
key: &str,
delta: i64,
ttl: Option<Duration>,
) -> Result<i64, Error>
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 keydelta- Amount to increment by (can be negative)ttl- Optional time-to-live for the counter
§Returns
Ok(i64)- The new value after incrementingErr(Error)- Redis operation failed
§Notes
If the key doesn’t exist, it’s initialized to 0 with ttl before incrementing
Sourcepub async fn set<T: ToRedisArgs + Send + Sync>(
&self,
key: &str,
value: T,
ttl: Option<Duration>,
) -> Result<(), Error>
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
Sourcepub async fn get<T: FromRedisValue>(&self, key: &str) -> Result<T, Error>
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
Sourcepub async fn set_struct<T>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> Result<(), Error>
pub async fn set_struct<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
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
Sourcepub async fn get_struct<T>(&self, key: &str) -> Result<Option<T>, Error>where
T: DeserializeOwned,
pub async fn get_struct<T>(&self, key: &str) -> Result<Option<T>, Error>where
T: DeserializeOwned,
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
Sourcepub async fn set_struct_lz4<T>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> Result<(), Error>
pub async fn set_struct_lz4<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
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 datavalue- The struct to serialize and compressttl- Optional time-to-live duration
§Notes
Uses LZ4 compression which favors speed over compression ratio
Sourcepub async fn get_struct_lz4<T>(&self, key: &str) -> Result<Option<T>, Error>where
T: DeserializeOwned,
pub async fn get_struct_lz4<T>(&self, key: &str) -> Result<Option<T>, Error>where
T: DeserializeOwned,
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 valueOk(None)- Key doesn’t existErr(Error)- Redis, decompression, or deserialization error
Sourcepub async fn set_struct_zstd<T>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> Result<(), Error>
pub async fn set_struct_zstd<T>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Result<(), Error>
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 datavalue- The struct to serialize and compressttl- Optional time-to-live duration
§Notes
Uses Zstd compression which provides better compression ratios than LZ4
Sourcepub async fn get_struct_zstd<T>(&self, key: &str) -> Result<Option<T>, Error>where
T: DeserializeOwned,
pub async fn get_struct_zstd<T>(&self, key: &str) -> Result<Option<T>, Error>where
T: DeserializeOwned,
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 valueOk(None)- Key doesn’t existErr(Error)- Redis, decompression, or deserialization error