Skip to content

Commit

Permalink
Fix redis tag cache ttls in past no getting flushed
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Gersten committed Jan 26, 2024
1 parent abc4c98 commit 59b8380
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Cache/RedisTagSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class RedisTagSet extends TagSet
* Add a reference entry to the tag set's underlying sorted set.
*
* @param string $key
* @param int $ttl
* @param int|null $ttl
* @param string $updateWhen
* @return void
*/
public function addEntry(string $key, int $ttl = 0, $updateWhen = null)
public function addEntry(string $key, int $ttl = null, $updateWhen = null)
{
$ttl = $ttl > 0 ? Carbon::now()->addSeconds($ttl)->getTimestamp() : -1;
$ttl = is_null($ttl) ? -1 : Carbon::now()->addSeconds($ttl)->getTimestamp();

foreach ($this->tagIds() as $tagKey) {
if ($updateWhen) {
Expand Down
20 changes: 18 additions & 2 deletions src/Illuminate/Cache/RedisTaggedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,19 @@ class RedisTaggedCache extends TaggedCache
*/
public function add($key, $value, $ttl = null)
{
$seconds = null;

if ($ttl !== null) {
$seconds = $this->getSeconds($ttl);

if ($seconds <= 0) {
return false;
}
}

$this->tags->addEntry(
$this->itemKey($key),
! is_null($ttl) ? $this->getSeconds($ttl) : 0
$seconds
);

return parent::add($key, $value, $ttl);
Expand All @@ -36,9 +46,15 @@ public function put($key, $value, $ttl = null)
return $this->forever($key, $value);
}

$seconds = $this->getSeconds($ttl);

if ($seconds <= 0) {
return false;
}

$this->tags->addEntry(
$this->itemKey($key),
$this->getSeconds($ttl)
$seconds
);

return parent::put($key, $value, $ttl);
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Cache/RedisStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Cache;

use DateTime;
use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
Expand Down Expand Up @@ -139,6 +140,27 @@ public function testIncrementedTagEntriesProperlyTurnStale()
$this->assertEquals(0, count($keyCount));
}

public function testPastTtlTagEntriesAreNotAdded()
{
Cache::store('redis')->clear();

Cache::store('redis')->tags(['votes'])->add('person-1', 0, new DateTime('yesterday'));

$keyCount = Cache::store('redis')->connection()->keys('*');
$this->assertEquals(0, count($keyCount));
}

public function testPutPastTtlTagEntriesProperlyTurnStale()
{
Cache::store('redis')->clear();

Cache::store('redis')->tags(['votes'])->put('person-1', 0, new DateTime('yesterday'));
Cache::store('redis')->tags(['votes'])->flushStale();

$keyCount = Cache::store('redis')->connection()->keys('*');
$this->assertEquals(0, count($keyCount));
}

public function testTagsCanBeFlushedBySingleKey()
{
Cache::store('redis')->clear();
Expand Down

0 comments on commit 59b8380

Please sign in to comment.