Skip to content

Commit

Permalink
Make cache_set_lifespan the inverse of cache_get_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 6, 2024
1 parent 6a42aea commit 378507b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ pub trait IOCached<K, V> {
}

/// Set the lifespan of cached values, returns the old value
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
///
/// If the lifespan is set to `None`, cached values do not expire.
fn cache_set_lifespan(&mut self, _seconds: Option<u64>) -> Option<u64> {
None
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/stores/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ where
self.seconds
}

fn cache_set_lifespan(&mut self, seconds: u64) -> Option<u64> {
fn cache_set_lifespan(&mut self, seconds: Option<u64>) -> Option<u64> {
let old = self.seconds;
self.seconds = Some(seconds);
self.seconds = seconds;
old
}

Expand Down Expand Up @@ -346,15 +346,15 @@ mod tests {
sleep(Duration::new(2, 500000));
assert!(c.cache_get(&1).unwrap().is_none());

let old = c.cache_set_lifespan(1).unwrap();
let old = c.cache_set_lifespan(Some(1)).unwrap();
assert_eq!(2, old);
assert!(c.cache_set(1, 100).unwrap().is_none());
assert!(c.cache_get(&1).unwrap().is_some());

sleep(Duration::new(1, 600000));
assert!(c.cache_get(&1).unwrap().is_none());

c.cache_set_lifespan(10).unwrap();
c.cache_set_lifespan(Some(10)).unwrap();
assert!(c.cache_set(1, 100).unwrap().is_none());
assert!(c.cache_set(2, 100).unwrap().is_none());
assert_eq!(c.cache_get(&1).unwrap().unwrap(), 100);
Expand Down
10 changes: 6 additions & 4 deletions src/stores/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,11 @@ where
Some(self.seconds)
}

fn cache_set_lifespan(&mut self, seconds: u64) -> Option<u64> {
fn cache_set_lifespan(&mut self, seconds: Option<u64>) -> Option<u64> {
let old = self.seconds;
self.seconds = seconds;
// Redis must have a cache TTL, so get as close to an unbounded cache as we can if
// the user wants values to never expire.
self.seconds = seconds.unwrap_or(u64::MAX);
Some(old)
}

Expand Down Expand Up @@ -736,15 +738,15 @@ mod tests {
sleep(Duration::new(2, 500_000));
assert!(c.cache_get(&1).unwrap().is_none());

let old = c.cache_set_lifespan(1).unwrap();
let old = c.cache_set_lifespan(Some(1)).unwrap();
assert_eq!(2, old);
assert!(c.cache_set(1, 100).unwrap().is_none());
assert!(c.cache_get(&1).unwrap().is_some());

sleep(Duration::new(1, 600_000));
assert!(c.cache_get(&1).unwrap().is_none());

c.cache_set_lifespan(10).unwrap();
c.cache_set_lifespan(Some(10)).unwrap();
assert!(c.cache_set(1, 100).unwrap().is_none());
assert!(c.cache_set(2, 100).unwrap().is_none());
assert_eq!(c.cache_get(&1).unwrap().unwrap(), 100);
Expand Down

0 comments on commit 378507b

Please sign in to comment.