From 94cae8d271b6ca58486c4f31b359c298852f8385 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Mon, 20 Mar 2023 09:26:45 +1300 Subject: [PATCH] 2.1.1 (2023-03-20) - Update setex to set, add TTL to stats --- CHANGELOG.md | 4 ++++ src/Resque/Job/Status.php | 16 ++++++++++------ src/Resque/Stat.php | 31 +++++++++++++++++++++++-------- src/Resque/Worker.php | 13 +++++++++++-- tests/Resque/Tests/RedisTest.php | 7 ++++++- 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 650ec73..b75f7d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.1.1 (2023-03-20) +- Changed setex to set with EX values +- Added TTLs to missing keys + ## 2.1.0 (2023-02-07) - Add PHP 8.1 / 8.2 unit tests - Updated code to be PHP 8.2 compliant diff --git a/src/Resque/Job/Status.php b/src/Resque/Job/Status.php index 2092820..0486394 100644 --- a/src/Resque/Job/Status.php +++ b/src/Resque/Job/Status.php @@ -59,7 +59,11 @@ class Status 'updated' => time(), 'started' => time(), ]; - \Resque\Resque::redis()->setex('job:' . $id . ':status', 86400, json_encode($statusPacket)); + \Resque\Resque::redis()->set( + 'job:' . $id . ':status', + json_encode($statusPacket), + ['ex' => time() + 86400], + ); } /** @@ -98,12 +102,12 @@ class Status 'status' => $status, 'updated' => time(), ]; - \Resque\Resque::redis()->setex((string)$this, 86400, json_encode($statusPacket)); - // Expire the status for completed jobs after 24 hours - if (in_array($status, self::$completeStatuses)) { - \Resque\Resque::redis()->expire((string)$this, 86400); - } + \Resque\Resque::redis()->set( + (string)$this, + json_encode($statusPacket), + ['ex' => time() + 86400], + ); } /** diff --git a/src/Resque/Stat.php b/src/Resque/Stat.php index 66cdb55..76bd069 100644 --- a/src/Resque/Stat.php +++ b/src/Resque/Stat.php @@ -29,11 +29,24 @@ class Stat * * @param string $stat The name of the statistic to increment. * @param int $by The amount to increment the statistic by. - * @return boolean True if successful, false if not. + * + * @return bool True if successful, false if not. */ - public static function incr($stat, $by = 1): bool + public static function incr(string $stat, int $by = 1): bool { - return (bool)Resque::redis()->incrby('stat:' . $stat, $by); + // Make sure we set a TTL by default + $set = Resque::redis()->set( + 'stat:' . $stat, + $by, + ['ex' => time() + 86400, 'nx'], + ); + + // If it already exists, return the incrby value + if (!$set) { + return (bool)Resque::redis()->incrby('stat:' . $stat, $by); + } + + return true; } /** @@ -41,9 +54,10 @@ class Stat * * @param string $stat The name of the statistic to decrement. * @param int $by The amount to decrement the statistic by. - * @return boolean True if successful, false if not. + * + * @return bool True if successful, false if not. */ - public static function decr($stat, $by = 1): bool + public static function decr(string $stat, int $by = 1): bool { return (bool)Resque::redis()->decrby('stat:' . $stat, $by); } @@ -52,10 +66,11 @@ class Stat * Delete a statistic with the given name. * * @param string $stat The name of the statistic to delete. - * @return boolean True if successful, false if not. + * + * @return bool True if successful, false if not. */ - public static function clear($stat): bool + public static function clear(string $stat): bool { - return (bool)Resque::redis()->del('stat:' . $stat); + return (bool)Resque::redis()->unlink('stat:' . $stat); } } diff --git a/src/Resque/Worker.php b/src/Resque/Worker.php index 9164437..1a03f92 100644 --- a/src/Resque/Worker.php +++ b/src/Resque/Worker.php @@ -491,7 +491,11 @@ class Worker public function registerWorker() { Resque::redis()->sadd('workers', (string)$this); - Resque::redis()->setex('worker:' . (string)$this . ':started', 86400, date('D M d H:i:s T Y')); + Resque::redis()->set( + 'worker:' . (string)$this . ':started', + date('D M d H:i:s T Y'), + ['ex' => time() + 86400], + ); } /** @@ -527,7 +531,12 @@ class Worker 'run_at' => date('D M d H:i:s T Y'), 'payload' => $job->payload ]); - Resque::redis()->setex('worker:' . $job->worker, 86400, $data); + + Resque::redis()->set( + 'worker:' . $job->worker, + $data, + ['ex' => time() + 86400], + ); } /** diff --git a/tests/Resque/Tests/RedisTest.php b/tests/Resque/Tests/RedisTest.php index a1a3814..200f208 100644 --- a/tests/Resque/Tests/RedisTest.php +++ b/tests/Resque/Tests/RedisTest.php @@ -14,7 +14,12 @@ class RedisTest extends TestCase { public function testRedisGetSet() { - $this->redis->setex("testKey", 3600, 24); + $this->redis->set( + 'testKey', + 24, + ['ex' => time() + 3600], + ); + $val = $this->redis->get("testKey"); $this->assertEquals(24, $val); }