Skip to content

Commit 374999b

Browse files
authored
Merge pull request #503 from Geolim4/final
Multiples fixes
2 parents 6e7d0ef + 1b2bdd2 commit 374999b

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/phpFastCache/Drivers/Predis/Driver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ protected function driverWrite(CacheItemInterface $item)
7474
if ($item instanceof Item) {
7575
$ttl = $item->getExpirationDate()->getTimestamp() - time();
7676

77-
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
77+
/**
78+
* @see https://redis.io/commands/setex
79+
* @see https://redis.io/commands/expire
80+
*/
81+
if($ttl <= 0){
82+
return $this->instance->expire($item->getKey(), 0);
83+
}else{
84+
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
85+
}
7886
} else {
7987
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
8088
}

src/phpFastCache/Drivers/Redis/Driver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ protected function driverWrite(CacheItemInterface $item)
7070
if ($item instanceof Item) {
7171
$ttl = $item->getExpirationDate()->getTimestamp() - time();
7272

73-
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
73+
/**
74+
* @see https://redis.io/commands/setex
75+
* @see https://redis.io/commands/expire
76+
*/
77+
if($ttl <= 0){
78+
return $this->instance->expire($item->getKey(), 0);
79+
}else{
80+
return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
81+
}
7482
} else {
7583
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
7684
}

tests/RedisExpireTtl0.test.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
8+
use phpFastCache\CacheManager;
9+
use phpFastCache\Helper\TestHelper;
10+
11+
chdir(__DIR__);
12+
require_once __DIR__ . '/../vendor/autoload.php';
13+
$testHelper = new TestHelper('(P)Redis Expire TTL to 0');
14+
$cacheInstance = CacheManager::getInstance('Redis', []);
15+
$cacheKey = 'cacheKey';
16+
$RandomCacheValue = str_shuffle(uniqid('pfc', true));
17+
$loops = 10;
18+
19+
$testHelper->printText('See https://redis.io/commands/setex');
20+
$testHelper->printText('See https://redis.io/commands/expire');
21+
$testHelper->printNewLine();
22+
23+
for ($i = 0; $i <= $loops; $i++)
24+
{
25+
$cacheItem = $cacheInstance->getItem("{$cacheKey}-{$i}");
26+
$cacheItem->set($RandomCacheValue)
27+
->expiresAt(new DateTime());
28+
29+
$cacheInstance->saveDeferred($cacheItem);
30+
}
31+
32+
try{
33+
$cacheInstance->commit();
34+
$testHelper->printPassText('The COMMIT operation has finished successfully');
35+
}catch (Predis\Response\ServerException $e){
36+
if(strpos($e->getMessage(), 'setex')){
37+
$testHelper->printFailText('The COMMIT operation has failed due to to an invalid time detection.');
38+
}else{
39+
$testHelper->printFailText('The COMMIT operation has failed due to to an unexpected error: ' . $e->getMessage());
40+
}
41+
}
42+
$cacheInstance->detachAllItems();
43+
44+
$testHelper->printText('Sleeping a second...');
45+
46+
47+
sleep(1);
48+
49+
for ($i = 0; $i <= $loops; $i++)
50+
{
51+
$cacheItem = $cacheInstance->getItem("{$cacheKey}-{$i}");
52+
53+
if($cacheItem->isHit()){
54+
$testHelper->printFailText(sprintf('The cache item "%s" is considered as HIT with the following value: %s', $cacheItem->getKey(), $cacheItem->get()));
55+
}else{
56+
$testHelper->printPassText(sprintf('The cache item "%s" is not considered as HIT.', $cacheItem->getKey()));
57+
}
58+
}
59+
60+
$cacheInstance->clear();
61+
62+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)