ParallelCacheOps

Struct ParallelCacheOps 

Source
pub struct ParallelCacheOps;
Expand description

Parallel batch operations for cache stores.

This module provides high-performance batch operations that execute multiple cache operations concurrently, significantly reducing total latency.

§Performance

  • get_many: 10-100x faster than sequential gets (depending on network latency)
  • set_many: 10-100x faster than sequential sets
  • delete_many: Similar performance gains

§Examples

use armature_cache::*;
use armature_cache::parallel::*;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

// Get multiple keys in parallel
let keys = vec!["user:1", "user:2", "user:3"];
let values = get_many_json(&cache, &keys).await?;

// Set multiple keys in parallel
let items = vec![
    ("key1", "value1".to_string()),
    ("key2", "value2".to_string()),
];
set_many_json(&cache, &items, None).await?;

Implementations§

Source§

impl ParallelCacheOps

Source

pub async fn get_many_json<S: CacheStore>( store: &S, keys: &[&str], ) -> CacheResult<Vec<Option<String>>>

Get multiple JSON values in parallel.

§Arguments
  • store - The cache store
  • keys - Slice of keys to fetch
§Returns

A vector of optional values in the same order as keys.

§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let keys = vec!["key1", "key2", "key3"];
let values = ParallelCacheOps::get_many_json(&cache, &keys).await?;

for (key, value) in keys.iter().zip(values.iter()) {
    println!("{}: {:?}", key, value);
}
Source

pub async fn get_many<S: CacheStore, T: DeserializeOwned>( store: &S, keys: &[&str], ) -> CacheResult<Vec<Option<T>>>

Get multiple typed values in parallel.

§Type Parameters
  • T - The type to deserialize into
§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct User {
    id: u64,
    name: String,
}

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let keys = vec!["user:1", "user:2", "user:3"];
let users: Vec<Option<User>> = ParallelCacheOps::get_many(&cache, &keys).await?;
Source

pub async fn set_many_json<S: CacheStore>( store: &S, items: &[(&str, String)], ttl: Option<Duration>, ) -> CacheResult<()>

Set multiple JSON values in parallel.

§Arguments
  • store - The cache store
  • items - Slice of (key, value) tuples
  • ttl - Optional time-to-live for all items
§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;
use std::time::Duration;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let items = vec![
    ("key1", r#"{"value": 1}"#.to_string()),
    ("key2", r#"{"value": 2}"#.to_string()),
];

ParallelCacheOps::set_many_json(&cache, &items, Some(Duration::from_secs(3600))).await?;
Source

pub async fn set_many<S: CacheStore, T: Serialize>( store: &S, items: &[(&str, T)], ttl: Option<Duration>, ) -> CacheResult<()>

Set multiple typed values in parallel.

§Type Parameters
  • T - The type to serialize from
§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Counter {
    count: u64,
}

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let items = vec![
    ("counter:1", Counter { count: 10 }),
    ("counter:2", Counter { count: 20 }),
];

ParallelCacheOps::set_many(&cache, &items, None).await?;
Source

pub async fn delete_many<S: CacheStore>( store: &S, keys: &[&str], ) -> CacheResult<()>

Delete multiple keys in parallel.

§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let keys = vec!["key1", "key2", "key3"];
ParallelCacheOps::delete_many(&cache, &keys).await?;
Source

pub async fn exists_many<S: CacheStore>( store: &S, keys: &[&str], ) -> CacheResult<Vec<bool>>

Check if multiple keys exist in parallel.

§Returns

A vector of booleans indicating existence, in the same order as keys.

§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let keys = vec!["key1", "key2", "key3"];
let exists = ParallelCacheOps::exists_many(&cache, &keys).await?;

for (key, exists) in keys.iter().zip(exists.iter()) {
    println!("{}: {}", key, exists);
}
Source

pub async fn ttl_many<S: CacheStore>( store: &S, keys: &[&str], ) -> CacheResult<Vec<Option<Duration>>>

Get TTL for multiple keys in parallel.

§Returns

A vector of optional durations, in the same order as keys.

§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let keys = vec!["key1", "key2", "key3"];
let ttls = ParallelCacheOps::ttl_many(&cache, &keys).await?;

for (key, ttl) in keys.iter().zip(ttls.iter()) {
    println!("{}: {:?}", key, ttl);
}
Source

pub async fn warm_cache<S, T, F, Fut>( store: &S, keys: &[&str], ttl: Option<Duration>, factory: F, ) -> CacheResult<()>
where S: CacheStore, T: Serialize, F: Fn(&str) -> Fut, Fut: Future<Output = CacheResult<T>>,

Cache warming: preload multiple keys into cache.

§Type Parameters
  • T - The type to serialize
  • F - Factory function that returns data for a given key
§Examples
use armature_cache::*;
use armature_cache::parallel::ParallelCacheOps;
use std::time::Duration;

let cache = RedisCache::new(CacheConfig::redis("redis://localhost:6379")?).await?;

let keys = vec!["user:1", "user:2", "user:3"];

ParallelCacheOps::warm_cache(
    &cache,
    &keys,
    Some(Duration::from_secs(3600)),
    |key: &str| async move {
        // Fetch from database
        let data = format!("Data for {}", key);
        Ok::<String, CacheError>(data)
    },
).await?;

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, 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, 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