Skip to content

Commit

Permalink
Merge pull request #137 from moka-rs/basic-stats
Browse files Browse the repository at this point in the history
Add `entry_count` and `weighted_size` methods to all cache implementations
  • Loading branch information
tatsuya6502 authored May 25, 2022
2 parents 915d9b0 + dd23c2b commit 3e57167
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 200 deletions.
32 changes: 15 additions & 17 deletions src/dash/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ impl<K, V, S> Drop for BaseCache<K, V, S> {
}
}

impl<K, V, S> BaseCache<K, V, S> {
pub(crate) fn policy(&self) -> Policy {
self.inner.policy()
}

pub(crate) fn entry_count(&self) -> u64 {
self.inner.entry_count()
}

pub(crate) fn weighted_size(&self) -> u64 {
self.inner.weighted_size()
}
}

impl<K, V, S> BaseCache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
Expand Down Expand Up @@ -203,20 +217,6 @@ where
let now = self.inner.current_time_from_expiration_clock();
self.inner.set_valid_after(now);
}

pub(crate) fn policy(&self) -> Policy {
self.inner.policy()
}

#[cfg(test)]
pub(crate) fn estimated_entry_count(&self) -> u64 {
self.inner.estimated_entry_count()
}

#[cfg(test)]
pub(crate) fn weighted_size(&self) -> u64 {
self.inner.weighted_size()
}
}

impl<'a, K, V, S> BaseCache<K, V, S>
Expand Down Expand Up @@ -555,13 +555,11 @@ impl<K, V, S> Inner<K, V, S> {
self.time_to_idle
}

#[cfg(test)]
#[inline]
fn estimated_entry_count(&self) -> u64 {
fn entry_count(&self) -> u64 {
self.entry_count.load()
}

#[cfg(test)]
#[inline]
pub(crate) fn weighted_size(&self) -> u64 {
self.weighted_size.load()
Expand Down
93 changes: 67 additions & 26 deletions src/dash/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,65 @@ where
}
}

impl<K, V, S> Cache<K, V, S> {
/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
/// A future version may support to modify it.
pub fn policy(&self) -> Policy {
self.base.policy()
}

/// Returns an approximate number of entries in this cache.
///
/// The value returned is _an estimate_; the actual count may differ if there are
/// concurrent insertions or removals, or if some entries are pending removal due
/// to expiration. This inaccuracy can be mitigated by performing a `sync()`
/// first.
///
/// # Example
///
/// ```rust
/// use moka::dash::Cache;
///
/// let cache = Cache::new(10);
/// cache.insert('n', "Netherland Dwarf");
/// cache.insert('l', "Lop Eared");
/// cache.insert('d', "Dutch");
///
/// // Ensure an entry exists.
/// assert!(cache.contains_key(&'n'));
///
/// // However, followings may print stale number zeros instead of threes.
/// println!("{}", cache.entry_count()); // -> 0
/// println!("{}", cache.weighted_size()); // -> 0
///
/// // To mitigate the inaccuracy, bring `ConcurrentCacheExt` trait to
/// // the scope so we can use `sync` method.
/// use moka::dash::ConcurrentCacheExt;
/// // Call `sync` to run pending internal tasks.
/// cache.sync();
///
/// // Followings will print the actual numbers.
/// println!("{}", cache.entry_count()); // -> 3
/// println!("{}", cache.weighted_size()); // -> 3
/// ```
///
pub fn entry_count(&self) -> u64 {
self.base.entry_count()
}

/// Returns an approximate total weighted size of entries in this cache.
///
/// The value returned is _an estimate_; the actual size may differ if there are
/// concurrent insertions or removals, or if some entries are pending removal due
/// to expiration. This inaccuracy can be mitigated by performing a `sync()`
/// first. See [`entry_count`](#method.entry_count) for a sample code.
pub fn weighted_size(&self) -> u64 {
self.base.weighted_size()
}
}

impl<K, V, S> Cache<K, V, S>
where
K: Hash + Eq + Send + Sync + 'static,
Expand Down Expand Up @@ -419,24 +478,6 @@ where
pub fn invalidate_all(&self) {
self.base.invalidate_all();
}

/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
/// A future version may support to modify it.
pub fn policy(&self) -> Policy {
self.base.policy()
}

#[cfg(test)]
pub(crate) fn estimated_entry_count(&self) -> u64 {
self.base.estimated_entry_count()
}

#[cfg(test)]
pub(crate) fn weighted_size(&self) -> u64 {
self.base.weighted_size()
}
}

impl<'a, K, V, S> Cache<K, V, S>
Expand Down Expand Up @@ -552,7 +593,7 @@ where
S: BuildHasher + Clone + Send + Sync + 'static,
{
pub(crate) fn is_table_empty(&self) -> bool {
self.estimated_entry_count() == 0
self.entry_count() == 0
}

pub(crate) fn reconfigure_for_testing(&mut self) {
Expand Down Expand Up @@ -725,7 +766,7 @@ mod tests {
assert!(!cache.contains_key(&"d"));

// Verify the sizes.
assert_eq!(cache.estimated_entry_count(), 2);
assert_eq!(cache.entry_count(), 2);
assert_eq!(cache.weighted_size(), 25);
}

Expand Down Expand Up @@ -827,14 +868,14 @@ mod tests {
cache.insert("b", "bob");
cache.sync();

assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

mock.increment(Duration::from_secs(5)); // 15 secs.
cache.sync();

assert_eq!(cache.get(&"b"), Some("bob"));
assert!(cache.contains_key(&"b"));
assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

cache.insert("b", "bill");
cache.sync();
Expand All @@ -844,7 +885,7 @@ mod tests {

assert_eq!(cache.get(&"b"), Some("bill"));
assert!(cache.contains_key(&"b"));
assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

mock.increment(Duration::from_secs(5)); // 25 secs
assert_eq!(cache.get(&"a"), None);
Expand Down Expand Up @@ -887,7 +928,7 @@ mod tests {
cache.insert("b", "bob");
cache.sync();

assert_eq!(cache.estimated_entry_count(), 2);
assert_eq!(cache.entry_count(), 2);

mock.increment(Duration::from_secs(2)); // 12 secs.
cache.sync();
Expand All @@ -897,7 +938,7 @@ mod tests {
assert!(cache.contains_key(&"b"));
cache.sync();

assert_eq!(cache.estimated_entry_count(), 2);
assert_eq!(cache.entry_count(), 2);

mock.increment(Duration::from_secs(3)); // 15 secs.
assert_eq!(cache.get(&"a"), None);
Expand All @@ -908,7 +949,7 @@ mod tests {
assert_eq!(cache.iter().count(), 1);

cache.sync();
assert_eq!(cache.estimated_entry_count(), 1);
assert_eq!(cache.entry_count(), 1);

mock.increment(Duration::from_secs(10)); // 25 secs
assert_eq!(cache.get(&"a"), None);
Expand Down
Loading

0 comments on commit 3e57167

Please sign in to comment.