Skip to content

Commit

Permalink
Add cache_unset_lifespan
Browse files Browse the repository at this point in the history
This makes it possible to temporarily change a cache lifespan, or to
remove a cache lifespan once it's set. See jaemk#197 for context.
  • Loading branch information
9999years committed Apr 8, 2024
1 parent 6a42aea commit 05c9510
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ pub trait Cached<K, V> {
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
None
}

/// Remove the lifespan for cached values, returns the old value.
///
/// For cache implementations that don't support retaining values indefinitely, this method is
/// a no-op.
fn cache_unset_lifespan(&mut self) -> Option<u64> {
None
}
}

/// Extra cache operations for types that implement `Clone`
Expand Down Expand Up @@ -418,10 +426,18 @@ pub trait IOCached<K, V> {
None
}

/// Set the lifespan of cached values, returns the old value
/// Set the lifespan of cached values, returns the old value.
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
None
}

/// Remove the lifespan for cached values, returns the old value.
///
/// For cache implementations that don't support retaining values indefinitely, this method is
/// a no-op.
fn cache_unset_lifespan(&mut self) -> Option<u64> {
None
}
}

#[cfg(feature = "async")]
Expand All @@ -448,4 +464,12 @@ pub trait IOCachedAsync<K, V> {
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
None
}

/// Remove the lifespan for cached values, returns the old value.
///
/// For cache implementations that don't support retaining values indefinitely, this method is
/// a no-op.
fn cache_unset_lifespan(&mut self) -> Option<u64> {
None
}
}
4 changes: 4 additions & 0 deletions src/stores/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ where
self.refresh = refresh;
old
}

fn cache_unset_lifespan(&mut self) -> Option<u64> {
self.seconds.take()
}
}

#[cfg(test)]
Expand Down

0 comments on commit 05c9510

Please sign in to comment.