Struct arc_swap::cache::Cache

source ·
pub struct Cache<A, T> { /* private fields */ }
Expand description

Caching handle for ArcSwapAny.

Instead of loading the Arc on every request from the shared storage, this keeps another copy inside itself. Upon request it only cheaply revalidates it is up to date. If it is, access is significantly faster. If it is stale, the load_full is done and the cache value is replaced. Under a read-heavy loads, the measured speedup are 10-25 times, depending on the architecture.

There are, however, downsides:

  • The handle needs to be kept around by the caller (usually, one per thread). This is fine if there’s one global ArcSwapAny, but starts being tricky with eg. data structures build from them.
  • As it keeps a copy of the Arc inside the cache, the old value may be kept alive for longer period of time ‒ it is replaced by the new value on load. You may not want to use this if dropping the old value in timely manner is important (possibly because of releasing large amount of RAM or because of closing file handles).

Examples

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use arc_swap::{ArcSwap, Cache};

let shared = Arc::new(ArcSwap::from_pointee(42));
let terminate = Arc::new(AtomicBool::new(false));
// Start 10 worker threads...
for _ in 0..10 {
    let mut cache = Cache::new(Arc::clone(&shared));
    let terminate = Arc::clone(&terminate);
    std::thread::spawn(move || {
        // Keep loading it like mad..
        while !terminate.load(Ordering::Relaxed) {
            let value = cache.load();
            do_something(value);
        }
    });
}
shared.store(Arc::new(12));

Another one with using a thread local storage and explicit types:

static CURRENT_CONFIG: Lazy<ArcSwap<Config>> = Lazy::new(|| ArcSwap::from_pointee(Config::default()));

thread_local! {
    static CACHE: RefCell<Cache<&'static ArcSwap<Config>, Arc<Config>>> = RefCell::new(Cache::from(CURRENT_CONFIG.deref()));
}

CACHE.with(|c| {
    // * RefCell needed, because load on cache is `&mut`.
    // * You want to operate inside the `with` ‒ cloning the Arc is comparably expensive as
    //   ArcSwap::load itself and whatever you'd save by the cache would be lost on that.
    println!("{:?}", c.borrow_mut().load());
});

Implementations§

source§

impl<A, T, S> Cache<A, T>where A: Deref<Target = ArcSwapAny<T, S>>, T: RefCnt, S: Strategy<T>,

source

pub fn new(arc_swap: A) -> Self

Creates a new caching handle.

The parameter is something dereferencing into an ArcSwapAny (eg. either to ArcSwap or ArcSwapOption). That can be ArcSwapAny itself, but that’s not very useful. But it also can be a reference to it or Arc, which makes it possible to share the ArcSwapAny with multiple caches or access it in non-cached way too.

source

pub fn arc_swap(&self) -> &A::Target

Gives access to the (possibly shared) cached ArcSwapAny.

source

pub fn load(&mut self) -> &T

Loads the currently held value.

This first checks if the cached value is up to date. This check is very cheap.

If it is up to date, the cached value is simply returned without additional costs. If it is outdated, a load is done on the underlying shared storage. The newly loaded value is then stored in the cache and returned.

source

pub fn map<F, U>(self, f: F) -> MapCache<A, T, F>where F: FnMut(&T) -> &U,

Turns this cache into a cache with a projection inside the cached value.

You’d use this in case when some part of code needs access to fresh values of U, however a bigger structure containing U is provided by this cache. The possibility of giving the whole structure to the part of the code falls short in terms of reusability (the part of the code could be used within multiple contexts, each with a bigger different structure containing U) and code separation (the code shouldn’t needs to know about the big structure).

Warning

As the provided f is called inside every load, this one should be cheap. Most often it is expected to be just a closure taking reference of some inner field.

For the same reasons, it should not have side effects and should never panic (these will not break Rust’s safety rules, but might produce behaviour you don’t expect).

Examples
use arc_swap::ArcSwap;
use arc_swap::cache::{Access, Cache};

struct InnerCfg {
    answer: usize,
}

struct FullCfg {
    inner: InnerCfg,
}

fn use_inner<A: Access<InnerCfg>>(cache: &mut A) {
    let value = cache.load();
    println!("The answer is: {}", value.answer);
}

let full_cfg = ArcSwap::from_pointee(FullCfg {
    inner: InnerCfg {
        answer: 42,
    }
});
let cache = Cache::new(&full_cfg);
use_inner(&mut cache.map(|full| &full.inner));

let inner_cfg = ArcSwap::from_pointee(InnerCfg { answer: 24 });
let mut inner_cache = Cache::new(&inner_cfg);
use_inner(&mut inner_cache);

Trait Implementations§

source§

impl<A, T, S> Access<<T as Deref>::Target> for Cache<A, T>where A: Deref<Target = ArcSwapAny<T, S>>, T: Deref<Target = <T as RefCnt>::Base> + RefCnt, S: Strategy<T>,

source§

fn load(&mut self) -> &T::Target

Loads the value from cache. Read more
source§

impl<A: Clone, T: Clone> Clone for Cache<A, T>

source§

fn clone(&self) -> Cache<A, T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<A: Debug, T: Debug> Debug for Cache<A, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<A, T, S> From<A> for Cache<A, T>where A: Deref<Target = ArcSwapAny<T, S>>, T: RefCnt, S: Strategy<T>,

source§

fn from(arc_swap: A) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<A, T> RefUnwindSafe for Cache<A, T>where A: RefUnwindSafe, T: RefUnwindSafe,

§

impl<A, T> Send for Cache<A, T>where A: Send, T: Send,

§

impl<A, T> Sync for Cache<A, T>where A: Sync, T: Sync,

§

impl<A, T> Unpin for Cache<A, T>where A: Unpin, T: Unpin,

§

impl<A, T> UnwindSafe for Cache<A, T>where A: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.