2.1.1 (2023-03-20) - Update setex to set, add TTL to stats

This commit is contained in:
Daniel Mason 2023-03-20 09:26:45 +13:00
parent 679394eb5f
commit 94cae8d271
5 changed files with 54 additions and 17 deletions

View File

@ -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) ## 2.1.0 (2023-02-07)
- Add PHP 8.1 / 8.2 unit tests - Add PHP 8.1 / 8.2 unit tests
- Updated code to be PHP 8.2 compliant - Updated code to be PHP 8.2 compliant

View File

@ -59,7 +59,11 @@ class Status
'updated' => time(), 'updated' => time(),
'started' => 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, 'status' => $status,
'updated' => time(), 'updated' => time(),
]; ];
\Resque\Resque::redis()->setex((string)$this, 86400, json_encode($statusPacket));
// Expire the status for completed jobs after 24 hours \Resque\Resque::redis()->set(
if (in_array($status, self::$completeStatuses)) { (string)$this,
\Resque\Resque::redis()->expire((string)$this, 86400); json_encode($statusPacket),
} ['ex' => time() + 86400],
);
} }
/** /**

View File

@ -29,11 +29,24 @@ class Stat
* *
* @param string $stat The name of the statistic to increment. * @param string $stat The name of the statistic to increment.
* @param int $by The amount to increment the statistic by. * @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 string $stat The name of the statistic to decrement.
* @param int $by The amount to decrement the statistic by. * @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); return (bool)Resque::redis()->decrby('stat:' . $stat, $by);
} }
@ -52,10 +66,11 @@ class Stat
* Delete a statistic with the given name. * Delete a statistic with the given name.
* *
* @param string $stat The name of the statistic to delete. * @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);
} }
} }

View File

@ -491,7 +491,11 @@ class Worker
public function registerWorker() public function registerWorker()
{ {
Resque::redis()->sadd('workers', (string)$this); 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'), 'run_at' => date('D M d H:i:s T Y'),
'payload' => $job->payload 'payload' => $job->payload
]); ]);
Resque::redis()->setex('worker:' . $job->worker, 86400, $data);
Resque::redis()->set(
'worker:' . $job->worker,
$data,
['ex' => time() + 86400],
);
} }
/** /**

View File

@ -14,7 +14,12 @@ class RedisTest extends TestCase
{ {
public function testRedisGetSet() public function testRedisGetSet()
{ {
$this->redis->setex("testKey", 3600, 24); $this->redis->set(
'testKey',
24,
['ex' => time() + 3600],
);
$val = $this->redis->get("testKey"); $val = $this->redis->get("testKey");
$this->assertEquals(24, $val); $this->assertEquals(24, $val);
} }