Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with respecting TTL in Predis and File cache #4875

Merged
merged 1 commit into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public function getMetaData(string $key)
}

return [
'expire' => $data['time'] + $data['ttl'],
'expire' => $data['ttl'] > 0 ? $data['time'] + $data['ttl'] : null,
'mtime' => filemtime($this->path . $key),
'data' => $data['data'],
];
Expand Down
4 changes: 3 additions & 1 deletion system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public function save(string $key, $value, int $ttl = 60)
return false;
}

$this->redis->expireat($key, time() + $ttl);
if ($ttl) {
$this->redis->expireat($key, time() + $ttl);
}

return true;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ public function testSaveExcessiveKeyLength()
unlink($file);
}

public function testSavePermanent()
{
$this->assertTrue($this->fileHandler->save(self::$key1, 'value', 0));
$metaData = $this->fileHandler->getMetaData(self::$key1);

$this->assertSame(null, $metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->fileHandler->delete(self::$key1));
}

public function testDelete()
{
$this->fileHandler->save(self::$key1, 'value');
Expand Down
12 changes: 12 additions & 0 deletions tests/system/Cache/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ public function testSave()
$this->assertTrue($this->memcachedHandler->save(self::$key1, 'value'));
}

public function testSavePermanent()
{
$this->assertTrue($this->memcachedHandler->save(self::$key1, 'value', 0));
$metaData = $this->memcachedHandler->getMetaData(self::$key1);

$this->assertSame(null, $metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->memcachedHandler->delete(self::$key1));
}

public function testDelete()
{
$this->memcachedHandler->save(self::$key1, 'value');
Expand Down
16 changes: 14 additions & 2 deletions tests/system/Cache/Handlers/PredisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function testNew()

public function testDestruct()
{
$this->PredisHandler = new PRedisHandler($this->config);
$this->PredisHandler = new PredisHandler($this->config);
$this->PredisHandler->initialize();

$this->assertInstanceOf(PRedisHandler::class, $this->PredisHandler);
$this->assertInstanceOf(PredisHandler::class, $this->PredisHandler);
}

/**
Expand Down Expand Up @@ -97,6 +97,18 @@ public function testSave()
$this->assertTrue($this->PredisHandler->save(self::$key1, 'value'));
}

public function testSavePermanent()
{
$this->assertTrue($this->PredisHandler->save(self::$key1, 'value', 0));
$metaData = $this->PredisHandler->getMetaData(self::$key1);

$this->assertSame(null, $metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->PredisHandler->delete(self::$key1));
}

public function testDelete()
{
$this->PredisHandler->save(self::$key1, 'value');
Expand Down
12 changes: 12 additions & 0 deletions tests/system/Cache/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public function testSave()
$this->assertTrue($this->redisHandler->save(self::$key1, 'value'));
}

public function testSavePermanent()
{
$this->assertTrue($this->redisHandler->save(self::$key1, 'value', 0));
$metaData = $this->redisHandler->getMetaData(self::$key1);

$this->assertSame(null, $metaData['expire']);
$this->assertLessThanOrEqual(1, $metaData['mtime'] - time());
$this->assertSame('value', $metaData['data']);

$this->assertTrue($this->redisHandler->delete(self::$key1));
}

public function testDelete()
{
$this->redisHandler->save(self::$key1, 'value');
Expand Down