Skip to main content

RedisCache

Struct RedisCache 

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

Redis cache store.

Implementations§

Source§

impl RedisCache

Source

pub async fn new(config: CacheConfig) -> CacheResult<Self>

Create a new Redis cache instance.

§Arguments
  • config - Cache configuration
§Examples
use armature_cache::*;

#[tokio::main]
async fn main() -> Result<(), CacheError> {
    let config = CacheConfig::redis("redis://localhost:6379")?;
    let cache = RedisCache::new(config).await?;
    Ok(())
}
Source

pub fn connection(&self) -> &ConnectionManager

Get the underlying connection manager.

Trait Implementations§

Source§

impl CacheStore for RedisCache

Source§

fn set_json_forever<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: String, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Plain SET with no expiry, skipping the default_ttl fallback that set_json applies to a None TTL. See CacheStore::set_json_forever.

Source§

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clear this cache’s keys.

When key_prefix is configured, this is scoped to that prefix: it SCANs for every key matching {key_prefix}:* and removes them with batched UNLINK calls, so it only wipes keys this cache actually wrote — not the whole Redis database/instance. SCAN is cursor-based and non-blocking (unlike KEYS, which is O(N) and stalls the single-threaded server for the entire keyspace); UNLINK reclaims memory off the main thread instead of blocking on DEL.

When no key_prefix is configured, this cache has no distinct slice of the keyspace to scope to, so it falls back to the previous unscoped FLUSHDB behavior — this remains destructive to the entire Redis database/instance, so an unprefixed RedisCache sharing a Redis instance with other services/tenants should not call clear().

Source§

fn mget<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<String>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Native multi-get: a single MGET round-trip instead of N GETs.

MGET preserves argument order, so the returned vector matches keys element-for-element, with None for missing keys.

Source§

fn mset<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, String)], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Native multi-set in a single round-trip.

Without a TTL this is a plain MSET. MSET cannot express per-key expiry, so when a TTL applies we pipeline SET ... EX commands (still one round-trip), preserving the exact per-key TTL semantics of set_json (including the default_ttl fallback).

Source§

fn mdel<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Native multi-delete: a single variadic DEL round-trip instead of N.

Source§

fn supports_atomic_sets(&self) -> bool

RedisCache backs set_add/set_remove/set_members with native SADD/SREM/SMEMBERS, which are atomic — see CacheStore::supports_atomic_sets.

Source§

fn set_add<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, set_key: &'life1 str, member: &'life2 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Native SADD: atomically adds member to a Redis Set, unlike the trait default’s non-atomic get/modify/set. This is what makes crate::invalidation::TaggedCache’s tag index safe to update concurrently from multiple instances sharing this backend.

Source§

fn set_remove<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, set_key: &'life1 str, member: &'life2 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Native SREM: atomically removes member from a Redis Set.

Source§

fn set_add_many<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, set_key: &'life1 str, members: &'life2 [&'life3 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Variadic SADD key m1 m2 ...: one round-trip for the whole batch rather than one per member.

Source§

fn set_remove_many<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, set_key: &'life1 str, members: &'life2 [&'life3 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Variadic SREM key m1 m2 ...: one round-trip for the whole batch.

Source§

fn set_members<'life0, 'life1, 'async_trait>( &'life0 self, set_key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Native SMEMBERS.

Source§

fn get_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get a JSON value from the cache. Read more
Source§

fn set_json<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, value: String, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set a JSON value in the cache. Read more
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a key from the cache. Read more
Source§

fn exists<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Check if a key exists in the cache. Read more
Source§

fn ttl<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ) -> Pin<Box<dyn Future<Output = CacheResult<Option<Duration>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get the TTL (time-to-live) of a key. Read more
Source§

fn expire<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, ttl: Duration, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Set or update the expiration time for a key. Read more
Source§

fn increment<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Increment a numeric value. Read more
Source§

fn decrement<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 str, delta: i64, ) -> Pin<Box<dyn Future<Output = CacheResult<i64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Decrement a numeric value. Read more
Source§

fn get_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<String>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get multiple keys in parallel. Read more
Source§

fn set_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, items: &'life1 [(&'life2 str, String)], ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Set multiple key-value pairs in parallel. Read more
Source§

fn delete_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Delete multiple keys in parallel. Read more
Source§

fn exists_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<bool>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Check existence of multiple keys in parallel. Read more
Source§

fn ttl_many<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, keys: &'life1 [&'life2 str], ) -> Pin<Box<dyn Future<Output = CacheResult<Vec<Option<Duration>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Get TTL for multiple keys in parallel. Read more
Source§

impl Clone for RedisCache

Source§

fn clone(&self) -> RedisCache

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.