pub struct Cache<K, V, H = DefaultHasher> { /* private fields */ }
Expand description
A general cache with size and item count limits.
When size limit is reached, a pseudo-random element is removed and
the new one inserted. See Cache::discard_one
.
The insert method, Cache::cache
, has type-specific implementations.
This enables clever inserting of data, independently from this struct.
Therefore, the Cache::insert
function should only be used in
those implementations of this struct.
Implementations§
source§impl<K, V> Cache<K, V, DefaultHasher>
impl<K, V> Cache<K, V, DefaultHasher>
source§impl<K: Eq + Hash, V, H> Cache<K, V, H>
impl<K: Eq + Hash, V, H> Cache<K, V, H>
sourcepub fn get<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> CacheOut<&V>where
K: Borrow<Q>,
pub fn get<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> CacheOut<&V>where
K: Borrow<Q>,
Get value at key
from the cache.
See HashMap::get
for more info.
sourcepub fn get_with_lifetime<Q: ?Sized + Hash + Eq>(
&self,
key: &Q
) -> CacheOut<&CacheItem<V>>where
K: Borrow<Q>,
pub fn get_with_lifetime<Q: ?Sized + Hash + Eq>(
&self,
key: &Q
) -> CacheOut<&CacheItem<V>>where
K: Borrow<Q>,
sourcepub fn get_mut<Q: ?Sized + Hash + Eq>(&mut self, key: &Q) -> CacheOut<&V>where
K: Borrow<Q>,
pub fn get_mut<Q: ?Sized + Hash + Eq>(&mut self, key: &Q) -> CacheOut<&V>where
K: Borrow<Q>,
Get a mutable reference to the value at key
from the cache.
See HashMap::get
for more info.
sourcepub fn get_mut_with_lifetime<Q: ?Sized + Hash + Eq>(
&mut self,
key: &Q
) -> CacheOut<&mut CacheItem<V>>where
K: Borrow<Q>,
pub fn get_mut_with_lifetime<Q: ?Sized + Hash + Eq>(
&mut self,
key: &Q
) -> CacheOut<&mut CacheItem<V>>where
K: Borrow<Q>,
sourcepub fn contains<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> boolwhere
K: Borrow<Q>,
pub fn contains<Q: ?Sized + Hash + Eq>(&self, key: &Q) -> boolwhere
K: Borrow<Q>,
Returns true
if the cache contains key
.
See HashMap::contains_key
for more info.
sourcepub fn remove<Q: ?Sized + Hash + Eq>(&mut self, key: &Q) -> CacheOut<V>where
K: Borrow<Q>,
pub fn remove<Q: ?Sized + Hash + Eq>(&mut self, key: &Q) -> CacheOut<V>where
K: Borrow<Q>,
Removes a key-value pair from the cache, returning the value, if present.
See HashMap::remove
and CacheOut::Present
.
source§impl<K, V, H: Hasher> Cache<K, V, H>
impl<K, V, H: Hasher> Cache<K, V, H>
sourcepub fn feed_hasher(&mut self, data: &[u8])
pub fn feed_hasher(&mut self, data: &[u8])
Writes to the internal hasher to increase quality of output.
Should be used by implementors of the cache
method to add
to the internal hasher with their data.
The hasher is used when selecting a item to remove from the cache.
source§impl<K: Eq + Hash, V, H: Hasher> Cache<K, V, H>
impl<K: Eq + Hash, V, H: Hasher> Cache<K, V, H>
sourcepub fn discard_one(&mut self)
pub fn discard_one(&mut self)
Discards one key-value pair pseudo-randomly.
source§impl<K: Eq + Hash, H: Hasher> Cache<K, VariedResponse, H>
impl<K: Eq + Hash, H: Hasher> Cache<K, VariedResponse, H>
sourcepub fn cache(
&mut self,
key: K,
response: VariedResponse
) -> CacheOut<VariedResponse>
pub fn cache(
&mut self,
key: K,
response: VariedResponse
) -> CacheOut<VariedResponse>
Caches a CompressedResponse
and returns the previous response, if any.