Skip to content

Commit

Permalink
Fix logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrylinooo committed Oct 20, 2020
1 parent 0079edb commit f11bca9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
29 changes: 16 additions & 13 deletions src/SimpleCache/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public function set($key, $value, $ttl = null)

$timestamp = time();

if (is_null($ttl)) {
$ttl = 0;

} elseif ($ttl instanceof DateInterval) {
$datetimeObj = new DateTime();
$datetimeObj->add($ttl);

$ttl = $datetimeObj->getTimestamp() - $timestamp;
}

return $this->doSet($key, $value, $ttl, $timestamp);
}

Expand Down Expand Up @@ -148,28 +158,21 @@ public function deleteMultiple($keys)
/**
* Check if the TTL is expired or not.
*
* @param int|null|DateInterval $ttl The time to live of a cached data.
* @param int $timestamp The unix timesamp that want to check.
* @param int $ttl The time to live of a cached data.
* @param int $timestamp The unix timesamp that want to check.
*
* @return bool
*/
protected function isExpired($ttl, int $timestamp): bool
protected function isExpired(int $ttl, int $timestamp): bool
{
$now = time();

if (is_null($ttl)) {
// If $ttl equal to 0 means that never expires.
if (empty($ttl)) {
return false;

} elseif (is_integer($ttl) && ($now - $timestamp < $ttl)) {
} elseif ($now - $timestamp < $ttl) {
return false;

} elseif ($ttl instanceof DateInterval) {
$datetimeObj = new DateTime();
$datetimeObj->add($ttl);

if ($now - $timestamp < $datetimeObj->getTimestamp()) {
return false;
}
}

return true;
Expand Down
4 changes: 4 additions & 0 deletions src/SimpleCache/Driver/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
'value' => $value
];

if (empty($ttl)) {
$ttl = null;
}

$result = $this->redis->set(
$this->getKeyName($key),
serialize($contents),
Expand Down
10 changes: 2 additions & 8 deletions tests/SimpleCache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,8 @@ public function testIsExpired()
$method = $reflection->getMethod('isExpired');
$method->setAccessible(true);

// Test null
$result = $method->invokeArgs($cache, [null, $time]);
$this->assertFalse($result);

// Test DateInterval
$dateInterval = new DateInterval('PT5M');

$result = $method->invokeArgs($cache, [$dateInterval, $time]);
// Test zero
$result = $method->invokeArgs($cache, [0, $time]);
$this->assertFalse($result);
}

Expand Down
13 changes: 12 additions & 1 deletion tests/SimpleCache/DriverIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Psr\SimpleCache\CacheInterface;
use Shieldon\Test\SimpleCache\CacheTestCase;
use DateInterval;
use function method_exists;

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ public function testDriverCombinedTests()
}

// Test method `set()` and `get()`
$cache->set('foo', 'bar', 300);
$cache->set('foo', 'bar');
$this->assertSame('bar', $cache->get('foo'));

$cache->set('foo', 'bar bar', 300);
Expand Down Expand Up @@ -113,5 +114,15 @@ public function testDriverCacheExpired()
sleep(6);
$this->assertSame(null, $cache->get('foo'));
$this->assertFalse($cache->has('foo'));

// Test DateInterval
$dateInterval = new DateInterval('PT2S');
$cache->set('fkk', 'bar', $dateInterval);
$this->assertSame('bar', $cache->get('fkk'));
$this->assertTrue($cache->has('fkk'));

sleep(3);
$this->assertSame(null, $cache->get('fkk'));
$this->assertFalse($cache->has('fkk'));
}
}

0 comments on commit f11bca9

Please sign in to comment.