Add atomic reservation support in Redis for goal alert deduplication with expiration. Update Cron logic and add unit tests for validation.

This commit is contained in:
Jeppe Bundgaard
2026-03-17 15:23:02 +01:00
parent bd4a90cdaa
commit 979b0f8fac
3 changed files with 134 additions and 41 deletions
+16 -1
View File
@@ -450,6 +450,21 @@ class redis implements redis_i
return 'temporary_cache_' . uniqid();
}
/**
* Atomically set a key with TTL only when it does not already exist.
*/
public function set_if_absent_with_expiration(string $key, string $value, int $seconds): bool
{
if (!self::is_connected()) {
self::connect();
}
$seconds = max(1, $seconds);
$result = $this->redis->set($key, $value, 'EX', $seconds, 'NX');
return $result === true || strtoupper((string)$result) === 'OK';
}
public function mget(array $array_map): array
{
// Get multiple keys from Redis
@@ -485,4 +500,4 @@ class redis implements redis_i
$this->delete('perm:' . $cache_key);
return $this;
}
}
}