Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
docs(cache): Populate empty doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Connelly committed Jun 14, 2021
1 parent 8241a22 commit e8ed35b
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::hash::Hash;
use std::time::Duration;
use tokio::sync::RwLock;

///
/// A [`BTreeMap`]-backed cache supporting capacity- and duration-based expiry.
#[derive(Debug)]
pub struct Cache<T, V>
where
Expand Down Expand Up @@ -58,25 +58,27 @@ where
}
}

///
/// Returns the number of items in the cache.
pub async fn len(&self) -> usize {
self.items.read().await.len()
}

///
/// Returns `true` if the cache contains no items.
pub async fn is_empty(&self) -> bool {
self.items.read().await.is_empty()
}

///
/// Returns the number of items in the cache that match the given predicate.
pub async fn count<P>(&self, predicate: P) -> usize
where
P: FnMut(&(&T, &Item<V>)) -> bool,
{
self.items.read().await.iter().filter(predicate).count()
}

/// Get a value from the cache if one is set and not expired.
///
/// A clone of the value is returned, so this is only implemented when `V: Clone`.
pub async fn get(&self, key: &T) -> Option<V>
where
T: Eq + Hash,
Expand All @@ -90,7 +92,11 @@ where
.map(|k| k.object.clone())
}

/// Set a value in the cache and return the previous value, if any.
///
/// This will override an existing value for the same key, if there is one. `custom_duration`
/// can be set to override `self.item_duration`. If the new item causes the cache to exceed its
/// capacity, the oldest entry in the cache will be removed.
pub async fn set(&self, key: T, value: V, custom_duration: Option<Duration>) -> Option<V>
where
T: Eq + Hash + Clone,
Expand All @@ -109,7 +115,7 @@ where
replaced
}

///
/// Remove expired items from the cache storage.
#[allow(unused_assignments)]
pub async fn remove_expired(&self) {
let mut expired_keys = Vec::new();
Expand All @@ -127,7 +133,7 @@ where
}
}

/// removes keys beyond capacity
/// Remove items that exceed capacity, oldest first.
#[allow(unused_assignments)]
async fn drop_excess(&self) {
let len = self.len().await;
Expand All @@ -150,15 +156,15 @@ where
}
}

///
/// Remove an item from the cache, returning the removed value.
pub async fn remove(&self, key: &T) -> Option<V>
where
T: Eq + Hash,
{
self.items.write().await.remove(key).map(|item| item.object)
}

///
/// Clear the cache, removing all items.
pub async fn clear(&self) {
self.items.write().await.clear()
}
Expand Down

0 comments on commit e8ed35b

Please sign in to comment.