Skip to content
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
10 changes: 9 additions & 1 deletion src/phpFastCache/Drivers/Predis/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ protected function driverWrite(CacheItemInterface $item)
if ($item instanceof Item) {
$ttl = $item->getExpirationDate()->getTimestamp() - time();

return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
/**
* @see https://redis.io/commands/setex
* @see https://redis.io/commands/expire
*/
if($ttl <= 0){
return $this->instance->expire($item->getKey(), 0);
}else{
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
}
} else {
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
}
Expand Down
10 changes: 9 additions & 1 deletion src/phpFastCache/Drivers/Redis/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ protected function driverWrite(CacheItemInterface $item)
if ($item instanceof Item) {
$ttl = $item->getExpirationDate()->getTimestamp() - time();

return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
/**
* @see https://redis.io/commands/setex
* @see https://redis.io/commands/expire
*/
if($ttl <= 0){
return $this->instance->expire($item->getKey(), 0);
}else{
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
}
} else {
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
}
Expand Down
62 changes: 62 additions & 0 deletions tests/RedisExpireTtl0.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*/

use phpFastCache\CacheManager;
use phpFastCache\Helper\TestHelper;

chdir(__DIR__);
require_once __DIR__ . '/../vendor/autoload.php';
$testHelper = new TestHelper('(P)Redis Expire TTL to 0');
$cacheInstance = CacheManager::getInstance('Redis', []);
$cacheKey = 'cacheKey';
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
$loops = 10;

$testHelper->printText('See https://redis.io/commands/setex');
$testHelper->printText('See https://redis.io/commands/expire');
$testHelper->printNewLine();

for ($i = 0; $i <= $loops; $i++)
{
$cacheItem = $cacheInstance->getItem("{$cacheKey}-{$i}");
$cacheItem->set($RandomCacheValue)
->expiresAt(new DateTime());

$cacheInstance->saveDeferred($cacheItem);
}

try{
$cacheInstance->commit();
$testHelper->printPassText('The COMMIT operation has finished successfully');
}catch (Predis\Response\ServerException $e){
if(strpos($e->getMessage(), 'setex')){
$testHelper->printFailText('The COMMIT operation has failed due to to an invalid time detection.');
}else{
$testHelper->printFailText('The COMMIT operation has failed due to to an unexpected error: ' . $e->getMessage());
}
}
$cacheInstance->detachAllItems();

$testHelper->printText('Sleeping a second...');


sleep(1);

for ($i = 0; $i <= $loops; $i++)
{
$cacheItem = $cacheInstance->getItem("{$cacheKey}-{$i}");

if($cacheItem->isHit()){
$testHelper->printFailText(sprintf('The cache item "%s" is considered as HIT with the following value: %s', $cacheItem->getKey(), $cacheItem->get()));
}else{
$testHelper->printPassText(sprintf('The cache item "%s" is not considered as HIT.', $cacheItem->getKey()));
}
}

$cacheInstance->clear();

$testHelper->terminateTest();