pub struct PartitionedCache<K1: Eq + Hash, K2: Eq + Hash, V> {
partitions: HashMap<K1, Partition<K2, V>>,
access_priority: PriorityQueue<K1, Reverse<Instant>>,
expiry_priority: PriorityQueue<K1, Reverse<Instant>>,
current_size: usize,
desired_size: usize,
}
Fields§
§partitions: HashMap<K1, Partition<K2, V>>
Cached entries, indexed by partition key.
access_priority: PriorityQueue<K1, Reverse<Instant>>
Priority queue of partition keys ordered by access times.
When the cache is full and there are no expired records to prune, partitions will instead be pruned in LRU order.
INVARIANT: the keys in here are exactly the keys in partitions
.
expiry_priority: PriorityQueue<K1, Reverse<Instant>>
Priority queue of partition keys ordered by expiry time.
When the cache is pruned, expired records are removed first.
INVARIANT: the keys in here are exactly the keys in partitions
.
current_size: usize
The number of records in the cache, across all partitions.
INVARIANT: this is the sum of the size
fields of the partitions
.
desired_size: usize
The desired maximum number of records in the cache.
Implementations§
Source§impl<K1: Clone + Eq + Hash, K2: Copy + Eq + Hash, V: PartialEq> PartitionedCache<K1, K2, V>
impl<K1: Clone + Eq + Hash, K2: Copy + Eq + Hash, V: PartialEq> PartitionedCache<K1, K2, V>
Sourcepub fn with_desired_size(desired_size: usize) -> Self
pub fn with_desired_size(desired_size: usize) -> Self
Create a new cache with the given desired size.
The prune
method will remove expired records, and also enough records
(in least-recently-used order) to get down to this size.
Sourcepub fn get_partition_without_checking_expiration(
&mut self,
partition_key: &K1,
) -> Option<&HashMap<K2, Vec<(V, Instant)>>>
pub fn get_partition_without_checking_expiration( &mut self, partition_key: &K1, ) -> Option<&HashMap<K2, Vec<(V, Instant)>>>
Get all records for the given partition key from the cache, along with their expiration times.
These records may have expired if prune
has not been called recently.
Sourcepub fn get_without_checking_expiration(
&mut self,
partition_key: &K1,
record_key: &K2,
) -> Option<&[(V, Instant)]>
pub fn get_without_checking_expiration( &mut self, partition_key: &K1, record_key: &K2, ) -> Option<&[(V, Instant)]>
Get all records for the given partition and record key from the cache, along with their expiration times.
These records may have expired if prune
has not been called recently.
Sourcepub fn upsert(
&mut self,
partition_key: K1,
record_key: K2,
value: V,
ttl: Duration,
)
pub fn upsert( &mut self, partition_key: K1, record_key: K2, value: V, ttl: Duration, )
Insert a record into the cache, or reset the expiry time if already present.
Sourcepub fn remove_expired(&mut self) -> usize
pub fn remove_expired(&mut self) -> usize
Delete all expired records.
Returns the number of records deleted.
Sourcepub fn prune(&mut self) -> (bool, usize, usize, usize)
pub fn prune(&mut self) -> (bool, usize, usize, usize)
Delete all expired records, and then enough least-recently-used records to reduce the cache to the desired size.
Returns (has overflowed?, current size, num expired, num pruned)
.
Sourcefn remove_expired_step(&mut self) -> usize
fn remove_expired_step(&mut self) -> usize
Helper for remove_expired
: looks at the next-to-expire
domain and cleans up expired records from it. This may delete
more than one record, and may even delete the whole domain.
Returns the number of records removed.
Sourcefn remove_least_recently_used(&mut self) -> usize
fn remove_least_recently_used(&mut self) -> usize
Helper for prune
: deletes all records associated with the
least recently used domain.
Returns the number of records removed.
Trait Implementations§
Source§impl<K1: Clone + Eq + Hash, K2: Clone + Eq + Hash, V: Clone> Clone for PartitionedCache<K1, K2, V>
impl<K1: Clone + Eq + Hash, K2: Clone + Eq + Hash, V: Clone> Clone for PartitionedCache<K1, K2, V>
Source§fn clone(&self) -> PartitionedCache<K1, K2, V>
fn clone(&self) -> PartitionedCache<K1, K2, V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more