Skip to content

Commit

Permalink
Fixed #545
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Nov 27, 2017
1 parent 1add56e commit 93108cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/phpFastCache/Helper/Psr16Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function __construct($driver, array $config = [])
public function get($key, $default = null)
{
try {
$cacheItemValue = $this->internalCacheInstance->getItem($key)->get();
if ($cacheItemValue !== null) {
return $cacheItemValue;
$cacheItem = $this->internalCacheInstance->getItem($key);
if (!$cacheItem->isExpired() && $cacheItem->get() !== null) {
return $cacheItem->get();
} else {
return $default;
}
Expand Down Expand Up @@ -181,7 +181,8 @@ public function deleteMultiple($keys)
public function has($key)
{
try {
return $this->internalCacheInstance->getItem($key)->isHit();
$cacheItem = $this->internalCacheInstance->getItem($key);
return $cacheItem->isHit() && !$cacheItem->isExpired();
} catch (phpFastCacheInvalidArgumentException $e) {
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/issues/Github-545.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> http://www.phpfastcache.com
* @author Georges.L (Geolim4) <contact@geolim4.com>
*/

use phpFastCache\Helper\Psr16Adapter;
use phpFastCache\Helper\TestHelper;


chdir(__DIR__);
require_once __DIR__ . '/../../src/autoload.php';
$testHelper = new TestHelper('Github issue #545 - Psr16Adapter get item even if it is expired');
$defaultDriver = (!empty($argv[1]) ? ucfirst($argv[1]) : 'Files');
$Psr16Adapter = new Psr16Adapter($defaultDriver);
$ttl = 5;

$testHelper->printText('Preparing test item...');
$value = str_shuffle(uniqid('pfc', true));
$Psr16Adapter->set('test-key', $value, $ttl);
$testHelper->printText(sprintf('Sleeping for %d seconds...', $ttl + 1));

sleep($ttl + 1);

if(!$Psr16Adapter->has('test-key')){
$testHelper->printPassText('1/2 [Testing has()] Psr16 adapter does not return an expired cache item anymore');
}else{
$testHelper->printFailText('1/2 [Testing has()] Psr16 adapter returned an expired cache item');
}

if(!$Psr16Adapter->has('test-key')){
$testHelper->printPassText('2/2 [Testing get()] Psr16 adapter does not return an expired cache item anymore');
}else{
$testHelper->printFailText('2/2 [Testing get()] Psr16 adapter returned an expired cache item');
}

$testHelper->terminateTest();

0 comments on commit 93108cf

Please sign in to comment.