[−][src]Struct arc_swap::Cache
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 arc_swap::{ArcSwap, Cache}; let shared = Arc::new(ArcSwap::from_pointee(42)); // Start 10 worker threads... for _ in 0..10 { let mut cache = Cache::new(Arc::clone(&shared)); std::thread::spawn(move || { // Keep loading it like mad.. loop { let value = cache.load(); do_something(value); } }); } shared.store(Arc::new(12));
Methods
impl<A, T, S> Cache<A, T> where
A: Deref<Target = ArcSwapAny<T, S>>,
T: RefCnt,
S: LockStorage,
[src]
A: Deref<Target = ArcSwapAny<T, S>>,
T: RefCnt,
S: LockStorage,
pub fn new(arc_swap: A) -> Self
[src]
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.
pub fn arc_swap(&self) -> &A::Target
[src]
Gives access to the (possibly shared) cached ArcSwapAny
.
pub fn load(&mut self) -> &T
[src]
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.
Trait Implementations
impl<A, T, S> From<A> for Cache<A, T> where
A: Deref<Target = ArcSwapAny<T, S>>,
T: RefCnt,
S: LockStorage,
[src]
A: Deref<Target = ArcSwapAny<T, S>>,
T: RefCnt,
S: LockStorage,
impl<A: Clone, T: Clone> Clone for Cache<A, T>
[src]
fn clone(&self) -> Cache<A, T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<A: Debug, T: Debug> Debug for Cache<A, T>
[src]
Auto Trait Implementations
impl<A, T> Unpin for Cache<A, T> where
A: Unpin,
T: Unpin,
A: Unpin,
T: Unpin,
impl<A, T> Sync for Cache<A, T> where
A: Sync,
T: Sync,
A: Sync,
T: Sync,
impl<A, T> Send for Cache<A, T> where
A: Send,
T: Send,
A: Send,
T: Send,
impl<A, T> RefUnwindSafe for Cache<A, T> where
A: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
T: RefUnwindSafe,
impl<A, T> UnwindSafe for Cache<A, T> where
A: UnwindSafe,
T: UnwindSafe,
A: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,