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 92a15a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 4 additions & 2 deletions 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 All @@ -445,7 +447,7 @@ pub trait IOCachedAsync<K, V> {
}

/// Set the lifespan of cached values, returns the old value
fn cache_set_lifespan(&mut self, _seconds: u64) -> Option<u64> {
fn cache_set_lifespan(&mut self, _seconds: Option<u64>) -> Option<u64> {
None
}
}
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
18 changes: 10 additions & 8 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 @@ -639,9 +641,9 @@ mod async_redis {
}

/// Set the lifespan of cached values, returns the old value
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;
self.seconds = seconds.unwrap_or(u64::MAX);
Some(old)
}
}
Expand Down Expand Up @@ -675,15 +677,15 @@ mod async_redis {
sleep(Duration::new(2, 500_000));
assert!(c.cache_get(&1).await.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).await.unwrap().is_none());
assert!(c.cache_get(&1).await.unwrap().is_some());

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

c.cache_set_lifespan(10).unwrap();
c.cache_set_lifespan(Some(10)).unwrap();
assert!(c.cache_set(1, 100).await.unwrap().is_none());
assert!(c.cache_set(2, 100).await.unwrap().is_none());
assert_eq!(c.cache_get(&1).await.unwrap().unwrap(), 100);
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 92a15a5

Please sign in to comment.