fix(cache): honor sub-second Redis TTL via PSETEX + document clear() safety
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -69,8 +69,18 @@ impl Cache for RedisCache {
|
|||||||
let k = self.key(key);
|
let k = self.key(key);
|
||||||
match ttl {
|
match ttl {
|
||||||
Some(ttl) => {
|
Some(ttl) => {
|
||||||
let secs = ttl.as_secs().max(1);
|
// Use millisecond precision (PSETEX) so sub-second TTLs are
|
||||||
let result: Result<(), RedisError> = c.set_ex(&k, value, secs).await;
|
// honored faithfully. Previously `set_ex(seconds.max(1))`
|
||||||
|
// rounded anything < 1s up to 1s, silently changing expiry
|
||||||
|
// semantics for short-lived cache entries.
|
||||||
|
let ms = ttl.as_millis();
|
||||||
|
if ms == 0 {
|
||||||
|
return Err(CacheError::Key(
|
||||||
|
"ttl of 0ms not allowed — pass None to store permanently".into(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let ms = ms as u64;
|
||||||
|
let result: Result<(), RedisError> = c.pset_ex(&k, value, ms).await;
|
||||||
result.map_err(map_err)
|
result.map_err(map_err)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@@ -88,9 +98,13 @@ impl Cache for RedisCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn clear(&self) -> Result<(), CacheError> {
|
async fn clear(&self) -> Result<(), CacheError> {
|
||||||
// Deliberately does nothing: `FLUSHALL`/`FLUSHDB` are dangerous on a
|
// Deliberately does nothing: a blind `FLUSHDB`/`FLUSHALL` on a shared
|
||||||
// shared instance. Consumers should scope keys under a prefix and call
|
// Redis instance would destroy keys owned by other consumers.
|
||||||
// `invalidate` for the keys they own.
|
// Consumers that need a true wipe must either (a) use a dedicated Redis
|
||||||
|
// DB / namespace prefix they own exclusively, or (b) call
|
||||||
|
// `invalidate` per-key for the keys they manage.
|
||||||
|
//
|
||||||
|
// See: https://redis.io/commands/flushdb/ (no key-scoping)
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user