diff --git a/src/phpFastCache/Drivers/Predis/Driver.php b/src/phpFastCache/Drivers/Predis/Driver.php index 3225d1f47..b13c61583 100644 --- a/src/phpFastCache/Drivers/Predis/Driver.php +++ b/src/phpFastCache/Drivers/Predis/Driver.php @@ -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'); } diff --git a/src/phpFastCache/Drivers/Redis/Driver.php b/src/phpFastCache/Drivers/Redis/Driver.php index be00037b9..9ba1fa4cf 100644 --- a/src/phpFastCache/Drivers/Redis/Driver.php +++ b/src/phpFastCache/Drivers/Redis/Driver.php @@ -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'); } diff --git a/tests/RedisExpireTtl0.test.php b/tests/RedisExpireTtl0.test.php new file mode 100644 index 000000000..f1b5df5b8 --- /dev/null +++ b/tests/RedisExpireTtl0.test.php @@ -0,0 +1,62 @@ + http://www.phpfastcache.com + * @author Georges.L (Geolim4) + */ + +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(); \ No newline at end of file