pub struct SharedLocalCache { /* private fields */ }
Expand description
Provides shared, synchronized access to a LocalCache
and a function-memoization
API in SharedLocalCache::cache_with
.
For convenience wrappers around SharedLocalCache::cache_with
see
SharedLocalCache::cache
for returned types that implement
Clone
and SharedLocalCache::hold
for values that just need to be stored
without returning a reference.
§Example
let storage = dyn_cache::local::SharedLocalCache::default();
let call_count = std::cell::Cell::new(0);
let increment_count = |&to_add: &i32| {
let new_count = call_count.get() + to_add;
call_count.set(new_count);
new_count
};
assert_eq!(call_count.get(), 0, "not called yet");
let with_one = storage.cache_with(&'a', &1, &increment_count, Clone::clone);
assert_eq!(call_count.get(), 1, "called only once");
assert_eq!(call_count.get(), with_one);
let with_one_again = storage.cache_with(&'a', &1, &increment_count, Clone::clone);
assert_eq!(call_count.get(), 1, "still called only once, previous value cached");
assert_eq!(call_count.get(), with_one_again);
let with_two = storage.cache_with(&'a', &2, &increment_count, Clone::clone);
assert_eq!(call_count.get(), 3, "called again with a new, larger increment");
assert_eq!(call_count.get(), with_two);
let with_other_query = storage.cache_with(&'b', &1, &increment_count, Clone::clone);
assert_eq!(call_count.get(), 4, "called again with the same increment, different scope");
assert_eq!(call_count.get(), with_other_query);
let with_two_again = storage.cache_with(&'a', &2, &increment_count, Clone::clone);
assert_eq!(call_count.get(), 4, "cell still has last mutation's value");
assert_eq!(with_two_again, with_two, "cache should still have previous value");
storage.gc(); // won't drop any values, but sets all of the cached values to be dropped
call_count.set(0);
// re-run 'b', marking it live
let reran_other_query = storage.cache_with(&'b', &1, &increment_count, Clone::clone);
assert_eq!(reran_other_query , 4, "returns the cached value");
assert_eq!(call_count.get(), 0, "without running increment_count");
storage.gc(); // query 'a' will be dropped
// re-run 'b', observing cached value
let reran_other_query = storage.cache_with(&'b', &1, &increment_count, Clone::clone);
assert_eq!(reran_other_query , 4, "still returns the cached value");
assert_eq!(call_count.get(), 0, "still without running increment_count");
// run 'a' again, observe no cached value
let with_one_again = storage.cache_with(&'a', &1, &increment_count, Clone::clone);
assert_eq!(call_count.get(), 1, "called without caching");
assert_eq!(call_count.get(), with_one_again);
Implementations§
Sourcepub fn cache_with<Key, Scope, Arg, Input, Output, Ret>(
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output,
with: impl FnOnce(&Output) -> Ret,
) -> Ret
pub fn cache_with<Key, Scope, Arg, Input, Output, Ret>( &self, key: &Key, arg: &Arg, init: impl FnOnce(&Input) -> Output, with: impl FnOnce(&Output) -> Ret, ) -> Ret
Caches the result of init(arg)
once per key
, re-running it when arg
changes. Always
runs with
on the stored Output
before returning the result.
See SharedLocalCache::cache
for an ergonomic wrapper that requires Output: Clone
.
Sourcepub fn cache<Key, Scope, Arg, Input, Output>(
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output,
) -> Output
pub fn cache<Key, Scope, Arg, Input, Output>( &self, key: &Key, arg: &Arg, init: impl FnOnce(&Input) -> Output, ) -> Output
Caches the result of init(arg)
once per key
, re-running it when arg
changes. Clones
the cached output before returning the result.
See SharedLocalCache::cache_with
for a lower-level version which does not require
Output: Clone
.
Sourcepub fn hold<Key, Scope, Arg, Input, Output>(
&self,
key: &Key,
arg: &Arg,
init: impl FnOnce(&Input) -> Output,
)
pub fn hold<Key, Scope, Arg, Input, Output>( &self, key: &Key, arg: &Arg, init: impl FnOnce(&Input) -> Output, )
Caches the result of init(arg)
once per key
, re-running it when arg
changes.
Does not return any reference to the cached value. See SharedLocalCache::cache
for similar functionality that returns a copy of Output
or
SharedLocalCache::cache_with
which allows specifying other pre-return functions.
Sourcepub fn gc(&self)
pub fn gc(&self)
Forwards to LocalCache::gc
.
Trait Implementations§
Source§fn clone(&self) -> SharedLocalCache
fn clone(&self) -> SharedLocalCache
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§fn default() -> SharedLocalCache
fn default() -> SharedLocalCache
Source§fn from(inner: LocalCache) -> Self
fn from(inner: LocalCache) -> Self
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.