Skip to content

Commit 94cae8d

Browse files
committed
2.1.1 (2023-03-20) - Update setex to set, add TTL to stats
1 parent 679394e commit 94cae8d

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.1 (2023-03-20)
2+
- Changed setex to set with EX values
3+
- Added TTLs to missing keys
4+
15
## 2.1.0 (2023-02-07)
26
- Add PHP 8.1 / 8.2 unit tests
37
- Updated code to be PHP 8.2 compliant

src/Resque/Job/Status.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public static function create($id)
5959
'updated' => time(),
6060
'started' => time(),
6161
];
62-
\Resque\Resque::redis()->setex('job:' . $id . ':status', 86400, json_encode($statusPacket));
62+
\Resque\Resque::redis()->set(
63+
'job:' . $id . ':status',
64+
json_encode($statusPacket),
65+
['ex' => time() + 86400],
66+
);
6367
}
6468

6569
/**
@@ -98,12 +102,12 @@ public function update($status)
98102
'status' => $status,
99103
'updated' => time(),
100104
];
101-
\Resque\Resque::redis()->setex((string)$this, 86400, json_encode($statusPacket));
102105

103-
// Expire the status for completed jobs after 24 hours
104-
if (in_array($status, self::$completeStatuses)) {
105-
\Resque\Resque::redis()->expire((string)$this, 86400);
106-
}
106+
\Resque\Resque::redis()->set(
107+
(string)$this,
108+
json_encode($statusPacket),
109+
['ex' => time() + 86400],
110+
);
107111
}
108112

109113
/**

src/Resque/Stat.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,35 @@ public static function get(string $stat): int
2929
*
3030
* @param string $stat The name of the statistic to increment.
3131
* @param int $by The amount to increment the statistic by.
32-
* @return boolean True if successful, false if not.
32+
*
33+
* @return bool True if successful, false if not.
3334
*/
34-
public static function incr($stat, $by = 1): bool
35+
public static function incr(string $stat, int $by = 1): bool
3536
{
36-
return (bool)Resque::redis()->incrby('stat:' . $stat, $by);
37+
// Make sure we set a TTL by default
38+
$set = Resque::redis()->set(
39+
'stat:' . $stat,
40+
$by,
41+
['ex' => time() + 86400, 'nx'],
42+
);
43+
44+
// If it already exists, return the incrby value
45+
if (!$set) {
46+
return (bool)Resque::redis()->incrby('stat:' . $stat, $by);
47+
}
48+
49+
return true;
3750
}
3851

3952
/**
4053
* Decrement the value of the specified statistic by a certain amount (default is 1)
4154
*
4255
* @param string $stat The name of the statistic to decrement.
4356
* @param int $by The amount to decrement the statistic by.
44-
* @return boolean True if successful, false if not.
57+
*
58+
* @return bool True if successful, false if not.
4559
*/
46-
public static function decr($stat, $by = 1): bool
60+
public static function decr(string $stat, int $by = 1): bool
4761
{
4862
return (bool)Resque::redis()->decrby('stat:' . $stat, $by);
4963
}
@@ -52,10 +66,11 @@ public static function decr($stat, $by = 1): bool
5266
* Delete a statistic with the given name.
5367
*
5468
* @param string $stat The name of the statistic to delete.
55-
* @return boolean True if successful, false if not.
69+
*
70+
* @return bool True if successful, false if not.
5671
*/
57-
public static function clear($stat): bool
72+
public static function clear(string $stat): bool
5873
{
59-
return (bool)Resque::redis()->del('stat:' . $stat);
74+
return (bool)Resque::redis()->unlink('stat:' . $stat);
6075
}
6176
}

src/Resque/Worker.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,11 @@ public function workerPids()
491491
public function registerWorker()
492492
{
493493
Resque::redis()->sadd('workers', (string)$this);
494-
Resque::redis()->setex('worker:' . (string)$this . ':started', 86400, date('D M d H:i:s T Y'));
494+
Resque::redis()->set(
495+
'worker:' . (string)$this . ':started',
496+
date('D M d H:i:s T Y'),
497+
['ex' => time() + 86400],
498+
);
495499
}
496500

497501
/**
@@ -527,7 +531,12 @@ public function workingOn(\Resque\Job\Job $job)
527531
'run_at' => date('D M d H:i:s T Y'),
528532
'payload' => $job->payload
529533
]);
530-
Resque::redis()->setex('worker:' . $job->worker, 86400, $data);
534+
535+
Resque::redis()->set(
536+
'worker:' . $job->worker,
537+
$data,
538+
['ex' => time() + 86400],
539+
);
531540
}
532541

533542
/**

tests/Resque/Tests/RedisTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ class RedisTest extends TestCase
1414
{
1515
public function testRedisGetSet()
1616
{
17-
$this->redis->setex("testKey", 3600, 24);
17+
$this->redis->set(
18+
'testKey',
19+
24,
20+
['ex' => time() + 3600],
21+
);
22+
1823
$val = $this->redis->get("testKey");
1924
$this->assertEquals(24, $val);
2025
}

0 commit comments

Comments
 (0)