Skip to content

Commit 2c80fa5

Browse files
committed
Fixed #545
1 parent e3bf066 commit 2c80fa5

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/phpFastCache/Helper/Psr16Adapter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public function __construct($driver, array $config = [])
5454
public function get($key, $default = null)
5555
{
5656
try {
57-
$cacheItemValue = $this->internalCacheInstance->getItem($key)->get();
58-
if ($cacheItemValue !== null) {
59-
return $cacheItemValue;
57+
$cacheItem = $this->internalCacheInstance->getItem($key);
58+
if (!$cacheItem->isExpired() && $cacheItem->get() !== null) {
59+
return $cacheItem->get();
6060
} else {
6161
return $default;
6262
}
@@ -181,7 +181,8 @@ public function deleteMultiple($keys)
181181
public function has($key)
182182
{
183183
try {
184-
return $this->internalCacheInstance->getItem($key)->isHit();
184+
$cacheItem = $this->internalCacheInstance->getItem($key);
185+
return $cacheItem->isHit() && !$cacheItem->isExpired();
185186
} catch (phpFastCacheInvalidArgumentException $e) {
186187
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
187188
}

tests/issues/Github-545.test.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Helper\Psr16Adapter;
9+
use phpFastCache\Helper\TestHelper;
10+
11+
12+
chdir(__DIR__);
13+
require_once __DIR__ . '/../../src/autoload.php';
14+
$testHelper = new TestHelper('Github issue #545 - Psr16Adapter get item even if it is expired');
15+
$defaultDriver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
16+
$Psr16Adapter = new Psr16Adapter($defaultDriver);
17+
$ttl = 5;
18+
19+
$testHelper->printText('Preparing test item...');
20+
$value = str_shuffle(uniqid('pfc', true));
21+
$Psr16Adapter->set('test-key', $value, $ttl);
22+
$testHelper->printText(sprintf('Sleeping for %d seconds...', $ttl + 1));
23+
24+
sleep($ttl + 1);
25+
26+
if(!$Psr16Adapter->has('test-key')){
27+
$testHelper->printPassText('1/2 [Testing has()] Psr16 adapter does not return an expired cache item anymore');
28+
}else{
29+
$testHelper->printFailText('1/2 [Testing has()] Psr16 adapter returned an expired cache item');
30+
}
31+
32+
if(!$Psr16Adapter->has('test-key')){
33+
$testHelper->printPassText('2/2 [Testing get()] Psr16 adapter does not return an expired cache item anymore');
34+
}else{
35+
$testHelper->printFailText('2/2 [Testing get()] Psr16 adapter returned an expired cache item');
36+
}
37+
38+
$testHelper->terminateTest();

0 commit comments

Comments
 (0)