Skip to content

Commit 76bb1fc

Browse files
authored
Merge pull request #561 from Geolim4/final
Fixed #560
2 parents d36e230 + cd3bf58 commit 76bb1fc

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

src/phpFastCache/Core/Item/ItemExtendedTrait.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ public function setModificationDate(\DateTimeInterface $date)
166166
*/
167167
public function getTtl()
168168
{
169-
$ttl = $this->expirationDate->getTimestamp() - time();
170-
if ($ttl > 2592000) {
171-
$ttl = time() + $ttl;
172-
}
173-
174-
return $ttl;
169+
return max(0, $this->expirationDate->getTimestamp() - time());
175170
}
176171

177172
/**

src/phpFastCache/Drivers/Memcache/Driver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ protected function driverWrite(CacheItemInterface $item)
7878
* Check for Cross-Driver type confusion
7979
*/
8080
if ($item instanceof Item) {
81-
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $item->getTtl());
81+
$ttl = $item->getExpirationDate()->getTimestamp() - time();
82+
83+
// Memcache will only allow a expiration timer less than 2592000 seconds,
84+
// otherwise, it will assume you're giving it a UNIX timestamp.
85+
if ($ttl > 2592000) {
86+
$ttl = time() + $ttl;
87+
}
88+
89+
return $this->instance->set($item->getKey(), $this->driverPreWrap($item), $this->memcacheFlags, $ttl);
8290
} else {
8391
throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected');
8492
}

src/phpFastCache/Drivers/Sqlite/Driver.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/**
2828
* Class Driver
2929
* @package phpFastCache\Drivers
30+
* @todo Remove "exp" column in V7
3031
*/
3132
class Driver implements ExtendedCacheItemPoolInterface
3233
{
@@ -267,25 +268,25 @@ protected function driverWrite(CacheItemInterface $item)
267268
}
268269

269270
if ($toWrite == true) {
271+
$sql = "INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)";
270272
try {
271273
$stm = $this->getDb($item->getKey())
272-
->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)");
274+
->prepare($sql);
273275
$stm->execute([
274276
':keyword' => $item->getKey(),
275277
':object' => $this->encode($this->driverPreWrap($item)),
276-
':exp' => time() + $item->getTtl(),
278+
':exp' => $item->getExpirationDate()->getTimestamp(),
277279
]);
278280

279281
return true;
280282
} catch (\PDOException $e) {
281-
282283
try {
283284
$stm = $this->getDb($item->getKey(), true)
284-
->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)");
285+
->prepare($sql);
285286
$stm->execute([
286287
':keyword' => $item->getKey(),
287288
':object' => $this->encode($this->driverPreWrap($item)),
288-
':exp' => time() + $item->getTtl(),
289+
':exp' => $item->getExpirationDate()->getTimestamp(),
289290
]);
290291
} catch (PDOException $e) {
291292
return false;
@@ -305,18 +306,18 @@ protected function driverWrite(CacheItemInterface $item)
305306
*/
306307
protected function driverRead(CacheItemInterface $item)
307308
{
309+
$sql = "SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1";
308310
try {
309311
$stm = $this->getDb($item->getKey())
310-
->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1");
312+
->prepare($sql);
311313
$stm->execute([
312314
':keyword' => $item->getKey(),
313315
]);
314316
$row = $stm->fetch(PDO::FETCH_ASSOC);
315-
316317
} catch (PDOException $e) {
317318
try {
318319
$stm = $this->getDb($item->getKey(), true)
319-
->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1");
320+
->prepare($sql);
320321
$stm->execute([
321322
':keyword' => $item->getKey(),
322323
]);

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+
$cacheInstance->save($cacheItem);
35+
$now = time();
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)