Skip to content

Commit ca059cb

Browse files
committed
Fixed #560 (V7)
(cherry picked from commit ee41c47) (cherry picked from commit 5212893)
1 parent 66f4035 commit ca059cb

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

src/phpFastCache/Core/Item/ItemExtendedTrait.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,7 @@ public function setModificationDate(\DateTimeInterface $date): ExtendedCacheItem
190190
*/
191191
public function getTtl(): int
192192
{
193-
$ttl = $this->expirationDate->getTimestamp() - time();
194-
if ($ttl > 2592000) {
195-
$ttl = time() + $ttl;
196-
}
197-
198-
return $ttl;
193+
return max(0, $this->expirationDate->getTimestamp() - time());
199194
}
200195

201196
/**

src/phpFastCache/Drivers/Memcache/Driver.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ protected function driverWrite(CacheItemInterface $item): bool
113113
* Check for Cross-Driver type confusion
114114
*/
115115
if ($item instanceof Item) {
116-
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $item->getTtl());
116+
$ttl = $item->getExpirationDate()->getTimestamp() - time();
117+
118+
// Memcache will only allow a expiration timer less than 2592000 seconds,
119+
// otherwise, it will assume you're giving it a UNIX timestamp.
120+
if ($ttl > 2592000) {
121+
$ttl = time() + $ttl;
122+
}
123+
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl);
117124
} else {
118125
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
119126
}

src/phpFastCache/Drivers/Sqlite/Driver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
/**
2727
* Class Driver
2828
* @package phpFastCache\Drivers
29+
* @todo Remove "exp" column in V7
2930
*/
3031
class Driver implements ExtendedCacheItemPoolInterface
3132
{
@@ -164,7 +165,7 @@ protected function driverWrite(CacheItemInterface $item): bool
164165
$stm->execute([
165166
':keyword' => $item->getKey(),
166167
':object' => $this->encode($this->driverPreWrap($item)),
167-
':exp' => time() + $item->getTtl(),
168+
':exp' => $item->getExpirationDate()->getTimestamp(),
168169
]);
169170

170171
return true;
@@ -176,7 +177,7 @@ protected function driverWrite(CacheItemInterface $item): bool
176177
$stm->execute([
177178
':keyword' => $item->getKey(),
178179
':object' => $this->encode($this->driverPreWrap($item)),
179-
':exp' => time() + $item->getTtl(),
180+
':exp' => $item->getExpirationDate()->getTimestamp(),
180181
]);
181182
} catch (PDOException $e) {
182183
return false;

tests/issues/Github-560.test.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
12+
chdir(__DIR__);
13+
require_once __DIR__ . '/../../src/autoload.php';
14+
$testHelper = new TestHelper('Github issue #560 - Expiration date bug with sqlite driver');
15+
$defaultTTl = 60 * 60 * 24 * 31;
16+
$cacheInstance = CacheManager::getInstance('Sqlite', [
17+
'defaultTtl' => $defaultTTl
18+
]);
19+
/**
20+
* Clear the cache to avoid
21+
* unexpected results
22+
*/
23+
$cacheInstance->clear();
24+
25+
$cacheKey = uniqid('ck', true);
26+
$string = uniqid('pfc', true);
27+
$testHelper->printText('Preparing test item...');
28+
29+
/**
30+
* Setup the cache item
31+
*/
32+
$cacheItem = $cacheInstance->getItem($cacheKey);
33+
$cacheItem->set($string);
34+
$now = time();
35+
$cacheInstance->save($cacheItem);
36+
37+
/**
38+
* Delete memory references
39+
* to be sure that the values
40+
* come from the cache itself
41+
*/
42+
unset($cacheItem);
43+
$cacheInstance->detachAllItems();
44+
$cacheItem = $cacheInstance->getItem($cacheKey);
45+
46+
if($cacheItem->getTtl() === $defaultTTl){
47+
$testHelper->printPassText('The cache Item TTL matches the default TTL after 30 days.');
48+
}else{
49+
$testHelper->printFailText('The cache Item TTL des not matches the default TTL after 30 days, got the following value: ' . $cacheItem->getTtl());
50+
}
51+
52+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)