diff --git a/src/SimpleCache/CacheProvider.php b/src/SimpleCache/CacheProvider.php index d2cc676..c795702 100644 --- a/src/SimpleCache/CacheProvider.php +++ b/src/SimpleCache/CacheProvider.php @@ -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); } @@ -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; diff --git a/src/SimpleCache/Driver/Redis.php b/src/SimpleCache/Driver/Redis.php index 2a51f31..a1cc4e4 100644 --- a/src/SimpleCache/Driver/Redis.php +++ b/src/SimpleCache/Driver/Redis.php @@ -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), diff --git a/tests/SimpleCache/CacheTest.php b/tests/SimpleCache/CacheTest.php index c3dd870..69cd35b 100644 --- a/tests/SimpleCache/CacheTest.php +++ b/tests/SimpleCache/CacheTest.php @@ -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); } diff --git a/tests/SimpleCache/DriverIntegrationTestCase.php b/tests/SimpleCache/DriverIntegrationTestCase.php index cbcb175..1240c27 100644 --- a/tests/SimpleCache/DriverIntegrationTestCase.php +++ b/tests/SimpleCache/DriverIntegrationTestCase.php @@ -12,6 +12,7 @@ use Psr\SimpleCache\CacheInterface; use Shieldon\Test\SimpleCache\CacheTestCase; +use DateInterval; use function method_exists; /** @@ -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); @@ -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')); } } \ No newline at end of file