From 3e2f49d0410124d6ed4e4b1c11d8d6d260b048d6 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Mon, 10 Jul 2017 19:27:40 +0200 Subject: [PATCH 1/4] Added Wiki link in ISSUE_TEMPLATE.md --- .github/ISSUE_TEMPLATE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 4440c381f..2d1f66549 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -6,4 +6,5 @@ Operating system: ` ...replace me... ` #### Issue description: -> ...Your description goes here... \ No newline at end of file +> ... Your description goes here ... +... You can also read the [wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki) ... \ No newline at end of file From 71a306b71c3bb59c152b5c13e8090ea5b29d5850 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Fri, 14 Jul 2017 03:26:02 +0200 Subject: [PATCH 2/4] Fixed #497 --- src/phpFastCache/Helper/Psr16Adapter.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/phpFastCache/Helper/Psr16Adapter.php b/src/phpFastCache/Helper/Psr16Adapter.php index a6e1bae10..c15fc7d6b 100644 --- a/src/phpFastCache/Helper/Psr16Adapter.php +++ b/src/phpFastCache/Helper/Psr16Adapter.php @@ -142,6 +142,9 @@ public function setMultiple($values, $ttl = null) try { foreach ($values as $key => $value) { $cacheItem = $this->internalCacheInstance->getItem($key)->set($value); + if ($ttl) { + $cacheItem->expiresAfter($ttl); + } $this->internalCacheInstance->saveDeferred($cacheItem); unset($cacheItem); } From d6cf085e6780cf8bae1dfeeec81f59b63e745af1 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Sun, 30 Jul 2017 23:31:15 +0200 Subject: [PATCH 3/4] Fixed issue on (P)Redis with negative TTLs awaiting more specification of the PSR6 about this. --- src/phpFastCache/Drivers/Predis/Driver.php | 10 +++- src/phpFastCache/Drivers/Redis/Driver.php | 10 +++- tests/RedisExpireTtl0.test.php | 62 ++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 tests/RedisExpireTtl0.test.php 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..0ed5bee62 --- /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('Predis', []); +$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 From 1b2bdd2e1d46a5f4fdc1d27388a8d78f93ba11c7 Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Sun, 30 Jul 2017 23:43:47 +0200 Subject: [PATCH 4/4] Use Redis on test instead of Predis when we can --- tests/RedisExpireTtl0.test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/RedisExpireTtl0.test.php b/tests/RedisExpireTtl0.test.php index 0ed5bee62..f1b5df5b8 100644 --- a/tests/RedisExpireTtl0.test.php +++ b/tests/RedisExpireTtl0.test.php @@ -11,7 +11,7 @@ chdir(__DIR__); require_once __DIR__ . '/../vendor/autoload.php'; $testHelper = new TestHelper('(P)Redis Expire TTL to 0'); -$cacheInstance = CacheManager::getInstance('Predis', []); +$cacheInstance = CacheManager::getInstance('Redis', []); $cacheKey = 'cacheKey'; $RandomCacheValue = str_shuffle(uniqid('pfc', true)); $loops = 10;