Skip to content

Commit

Permalink
Support notification on eviction
Browse files Browse the repository at this point in the history
Add the `name` method to `sync` and `future` caches.
  • Loading branch information
tatsuya6502 committed Jul 3, 2022
1 parent b206dbc commit e207c38
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
@@ -1,5 +1,5 @@
{
"rust-analyzer.cargo.features": ["future", "dash", "unstable-debug-counters"],
"rust-analyzer.cargo.features": ["future", "dash", "logging", "unstable-debug-counters"],
"rust-analyzer.server.extraEnv": {
"CARGO_TARGET_DIR": "target/ra"
},
Expand Down
13 changes: 13 additions & 0 deletions src/future/builder.rs
Expand Up @@ -54,6 +54,7 @@ use std::{
///
#[must_use]
pub struct CacheBuilder<K, V, C> {
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
weigher: Option<Weigher<K, V>>,
Expand All @@ -72,6 +73,7 @@ where
{
fn default() -> Self {
Self {
name: None,
max_capacity: None,
initial_capacity: None,
weigher: None,
Expand Down Expand Up @@ -110,6 +112,7 @@ where
let build_hasher = RandomState::default();
builder_utils::ensure_expirations_or_panic(self.time_to_live, self.time_to_idle);
Cache::with_everything(
self.name,
self.max_capacity,
self.initial_capacity,
build_hasher,
Expand All @@ -135,6 +138,7 @@ where
{
builder_utils::ensure_expirations_or_panic(self.time_to_live, self.time_to_idle);
Cache::with_everything(
self.name,
self.max_capacity,
self.initial_capacity,
hasher,
Expand All @@ -149,6 +153,15 @@ where
}

impl<K, V, C> CacheBuilder<K, V, C> {
/// Sets the name of the cache. Currently the name is used for identification
/// only in logging messages.
pub fn name(self, name: &str) -> Self {
Self {
name: Some(name.to_string()),
..self
}
}

/// Sets the max capacity of the cache.
pub fn max_capacity(self, max_capacity: u64) -> Self {
Self {
Expand Down
9 changes: 9 additions & 0 deletions src/future/cache.rs
Expand Up @@ -547,6 +547,11 @@ where
}

impl<K, V, S> Cache<K, V, S> {
/// Returns cache’s name.
pub fn name(&self) -> Option<&str> {
self.base.name()
}

/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
Expand Down Expand Up @@ -633,6 +638,7 @@ where
pub fn new(max_capacity: u64) -> Self {
let build_hasher = RandomState::default();
Self::with_everything(
None,
Some(max_capacity),
None,
build_hasher,
Expand Down Expand Up @@ -663,6 +669,7 @@ where
// https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
#[allow(clippy::too_many_arguments)]
pub(crate) fn with_everything(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
build_hasher: S,
Expand All @@ -675,6 +682,7 @@ where
) -> Self {
Self {
base: BaseCache::new(
name,
max_capacity,
initial_capacity,
build_hasher.clone(),
Expand Down Expand Up @@ -2687,6 +2695,7 @@ mod tests {

// Create a cache with the eviction listener.
let mut cache = Cache::builder()
.name("My Future Cache")
.eviction_listener_with_queued_delivery_mode(listener)
.build();
cache.reconfigure_for_testing();
Expand Down
16 changes: 16 additions & 0 deletions src/sync/builder.rs
Expand Up @@ -46,6 +46,7 @@ use std::{
///
#[must_use]
pub struct CacheBuilder<K, V, C> {
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
num_segments: Option<usize>,
Expand All @@ -65,6 +66,7 @@ where
{
fn default() -> Self {
Self {
name: None,
max_capacity: None,
initial_capacity: None,
num_segments: None,
Expand Down Expand Up @@ -105,6 +107,7 @@ where
assert!(num_segments != 0);

CacheBuilder {
name: self.name,
max_capacity: self.max_capacity,
initial_capacity: self.initial_capacity,
num_segments: Some(num_segments),
Expand Down Expand Up @@ -132,6 +135,7 @@ where
let build_hasher = RandomState::default();
builder_utils::ensure_expirations_or_panic(self.time_to_live, self.time_to_idle);
Cache::with_everything(
self.name,
self.max_capacity,
self.initial_capacity,
build_hasher,
Expand Down Expand Up @@ -160,6 +164,7 @@ where
{
builder_utils::ensure_expirations_or_panic(self.time_to_live, self.time_to_idle);
Cache::with_everything(
self.name,
self.max_capacity,
self.initial_capacity,
hasher,
Expand Down Expand Up @@ -192,6 +197,7 @@ where
let build_hasher = RandomState::default();
builder_utils::ensure_expirations_or_panic(self.time_to_live, self.time_to_idle);
SegmentedCache::with_everything(
self.name,
self.max_capacity,
self.initial_capacity,
self.num_segments.unwrap(),
Expand Down Expand Up @@ -221,6 +227,7 @@ where
{
builder_utils::ensure_expirations_or_panic(self.time_to_live, self.time_to_idle);
SegmentedCache::with_everything(
self.name,
self.max_capacity,
self.initial_capacity,
self.num_segments.unwrap(),
Expand All @@ -236,6 +243,15 @@ where
}

impl<K, V, C> CacheBuilder<K, V, C> {
/// Sets the name of the cache. Currently the name is used for identification
/// only in logging messages.
pub fn name(self, name: &str) -> Self {
Self {
name: Some(name.to_string()),
..self
}
}

/// Sets the max capacity of the cache.
pub fn max_capacity(self, max_capacity: u64) -> Self {
Self {
Expand Down
9 changes: 9 additions & 0 deletions src/sync/cache.rs
Expand Up @@ -730,6 +730,11 @@ where
}

impl<K, V, S> Cache<K, V, S> {
/// Returns cache’s name.
pub fn name(&self) -> Option<&str> {
self.base.name()
}

/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
Expand Down Expand Up @@ -802,6 +807,7 @@ where
pub fn new(max_capacity: u64) -> Self {
let build_hasher = RandomState::default();
Self::with_everything(
None,
Some(max_capacity),
None,
build_hasher,
Expand Down Expand Up @@ -832,6 +838,7 @@ where
// https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
#[allow(clippy::too_many_arguments)]
pub(crate) fn with_everything(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
build_hasher: S,
Expand All @@ -844,6 +851,7 @@ where
) -> Self {
Self {
base: BaseCache::new(
name,
max_capacity,
initial_capacity,
build_hasher.clone(),
Expand Down Expand Up @@ -2909,6 +2917,7 @@ mod tests {

// Create a cache with the eviction listener.
let mut cache = Cache::builder()
.name("My Sync Cache")
.eviction_listener_with_conf(listener, listener_conf)
.build();
cache.reconfigure_for_testing();
Expand Down
10 changes: 10 additions & 0 deletions src/sync/segment.rs
Expand Up @@ -96,6 +96,7 @@ where
pub fn new(max_capacity: u64, num_segments: usize) -> Self {
let build_hasher = RandomState::default();
Self::with_everything(
None,
Some(max_capacity),
None,
num_segments,
Expand All @@ -119,6 +120,11 @@ where
}

impl<K, V, S> SegmentedCache<K, V, S> {
/// Returns cache’s name.
pub fn name(&self) -> Option<&str> {
self.inner.segments[0].name()
}

/// Returns a read-only cache policy of this cache.
///
/// At this time, cache policy cannot be modified after cache creation.
Expand Down Expand Up @@ -199,6 +205,7 @@ where
/// Panics if `num_segments` is 0.
#[allow(clippy::too_many_arguments)]
pub(crate) fn with_everything(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
num_segments: usize,
Expand All @@ -212,6 +219,7 @@ where
) -> Self {
Self {
inner: Arc::new(Inner::new(
name,
max_capacity,
initial_capacity,
num_segments,
Expand Down Expand Up @@ -578,6 +586,7 @@ where
/// Panics if `num_segments` is 0.
#[allow(clippy::too_many_arguments)]
fn new(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
num_segments: usize,
Expand All @@ -601,6 +610,7 @@ where
let segments = (0..actual_num_segments)
.map(|_| {
Cache::with_everything(
name.clone(),
seg_max_capacity,
seg_init_capacity,
build_hasher.clone(),
Expand Down
20 changes: 19 additions & 1 deletion src/sync_base/base_cache.rs
Expand Up @@ -84,6 +84,10 @@ impl<K, V, S> Drop for BaseCache<K, V, S> {
}

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

pub(crate) fn policy(&self) -> Policy {
self.inner.policy()
}
Expand Down Expand Up @@ -140,6 +144,7 @@ where
// https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
build_hasher: S,
Expand All @@ -153,6 +158,7 @@ where
let (r_snd, r_rcv) = crossbeam_channel::bounded(READ_LOG_SIZE);
let (w_snd, w_rcv) = crossbeam_channel::bounded(WRITE_LOG_SIZE);
let inner = Arc::new(Inner::new(
name,
max_capacity,
initial_capacity,
build_hasher,
Expand Down Expand Up @@ -660,6 +666,7 @@ enum AdmissionResult<K> {
type CacheStore<K, V, S> = crate::cht::SegmentedHashMap<Arc<K>, TrioArc<ValueEntry<K, V>>, S>;

pub(crate) struct Inner<K, V, S> {
name: Option<String>,
max_capacity: Option<u64>,
entry_count: AtomicCell<u64>,
weighted_size: AtomicCell<u64>,
Expand All @@ -684,6 +691,10 @@ pub(crate) struct Inner<K, V, S> {

// functions/methods used by BaseCache
impl<K, V, S> Inner<K, V, S> {
fn name(&self) -> Option<&str> {
self.name.as_deref()
}

fn policy(&self) -> Policy {
Policy::new(self.max_capacity, 1, self.time_to_live, self.time_to_idle)
}
Expand Down Expand Up @@ -802,6 +813,7 @@ where
// https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
#[allow(clippy::too_many_arguments)]
fn new(
name: Option<String>,
max_capacity: Option<u64>,
initial_capacity: Option<usize>,
build_hasher: S,
Expand All @@ -824,7 +836,11 @@ where
build_hasher.clone(),
);
let (removal_notifier, key_locks) = if let Some(listener) = eviction_listener {
let rn = RemovalNotifier::new(listener, eviction_listener_conf.unwrap_or_default());
let rn = RemovalNotifier::new(
listener,
eviction_listener_conf.unwrap_or_default(),
name.clone(),
);
if rn.is_blocking() {
let kl = KeyLockMap::with_hasher(build_hasher.clone());
(Some(rn), Some(kl))
Expand All @@ -836,6 +852,7 @@ where
};

Self {
name,
max_capacity: max_capacity.map(|n| n as u64),
entry_count: Default::default(),
weighted_size: Default::default(),
Expand Down Expand Up @@ -2014,6 +2031,7 @@ mod tests {

let ensure_sketch_len = |max_capacity, len, name| {
let cache = BaseCache::<u8, u8>::new(
None,
Some(max_capacity),
None,
RandomState::default(),
Expand Down

0 comments on commit e207c38

Please sign in to comment.