Skip to content

Commit d7948a7

Browse files
committed
Fix TTL
1 parent 495b435 commit d7948a7

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

.github/workflows/phpunit.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-version:
19+
- "8.2"
1920
- "8.1"
2021
- "8.0"
2122
- "7.4"
22-
- "7.3"
23-
- "7.2"
24-
- "7.1"
2523

2624
# Service containers to run
2725
services:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88
},
99
"require": {
10-
"php": ">=7.0",
10+
"php": ">=7.4",
1111
"psr/cache": "^1.0",
1212
"psr/log": "^1.1",
1313
"psr/simple-cache": "^1.0"

src/Psr16/ArrayCacheEngine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ByJG\Cache\Psr16;
44

5+
use DateInterval;
56
use Psr\Log\NullLogger;
67

78
class ArrayCacheEngine extends BaseCacheEngine
@@ -67,13 +68,12 @@ public function get($key, $default = null)
6768
*
6869
* @param string $key The key of the item to store.
6970
* @param mixed $value The value of the item to store, must be serializable.
70-
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
71+
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
7172
* the driver supports TTL then the library may set a default value
7273
* for it or let the driver take care of that.
7374
*
7475
* @return bool True on success and false on failure.
7576
*
76-
* @throws \Psr\SimpleCache\InvalidArgumentException
7777
* MUST be thrown if the $key string is not a legal value.
7878
*/
7979
public function set($key, $value, $ttl = null)

src/Psr16/BaseCacheEngine.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use ByJG\Cache\CacheAvailabilityInterface;
66
use ByJG\Cache\Exception\InvalidArgumentException;
7+
use DateInterval;
8+
use DateTime;
79
use Psr\SimpleCache\CacheInterface;
810

911
abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInterface
@@ -59,12 +61,25 @@ protected function addToNow($ttl)
5961
return strtotime("+$ttl second");
6062
}
6163

62-
if ($ttl instanceof \DateInterval) {
63-
$now = new \DateTime();
64+
if ($ttl instanceof DateInterval) {
65+
$now = new DateTime();
6466
$now->add($ttl);
6567
return $now->getTimestamp();
6668
}
6769

6870
return null;
6971
}
72+
73+
protected function convertToSeconds($ttl)
74+
{
75+
if (empty($ttl) || is_numeric($ttl)) {
76+
return $ttl;
77+
}
78+
79+
if ($ttl instanceof DateInterval) {
80+
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
81+
}
82+
83+
throw new InvalidArgumentException('Invalid TTL');
84+
}
7085
}

src/Psr16/FileSystemCacheEngine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ByJG\Cache\Psr16;
44

55
use ByJG\Cache\CacheLockInterface;
6+
use DateInterval;
67
use Exception;
78
use Psr\Log\NullLogger;
89

@@ -69,13 +70,12 @@ public function get($key, $default = null)
6970
*
7071
* @param string $key The key of the item to store.
7172
* @param mixed $value The value of the item to store, must be serializable.
72-
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
73+
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
7374
* the driver supports TTL then the library may set a default value
7475
* for it or let the driver take care of that.
7576
*
7677
* @return bool True on success and false on failure.
7778
*
78-
* @throws \Psr\SimpleCache\InvalidArgumentException
7979
* MUST be thrown if the $key string is not a legal value.
8080
*/
8181
public function set($key, $value, $ttl = null)

src/Psr16/MemcachedEngine.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ByJG\Cache\Psr16;
44

55
use ByJG\Cache\Exception\StorageErrorException;
6+
use DateInterval;
67
use Memcached;
78
use Psr\Log\NullLogger;
89

@@ -78,15 +79,17 @@ public function get($key, $default = null)
7879

7980
/**
8081
* @param string $key The object Key
81-
* @param object $value The object to be cached
82-
* @param int $ttl The time to live in seconds of this objects
82+
* @param mixed $value The object to be cached
83+
* @param DateInterval|int|null $ttl The time to live in seconds of this objects
8384
* @return bool If the object is successfully posted
8485
* @throws StorageErrorException
8586
*/
8687
public function set($key, $value, $ttl = null)
8788
{
8889
$this->lazyLoadMemCachedServers();
8990

91+
$ttl = $this->convertToSeconds($ttl);
92+
9093
$this->memCached->set($this->fixKey($key), serialize($value), is_null($ttl) ? 0 : $ttl);
9194
$this->logger->info("[Memcached] Set '$key' result " . $this->memCached->getResultCode());
9295
if ($this->memCached->getResultCode() !== Memcached::RES_SUCCESS) {

src/Psr16/RedisCacheEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public function set($key, $value, $ttl = null)
7979
{
8080
$this->lazyLoadRedisServer();
8181

82+
$ttl = $this->convertToSeconds($ttl);
83+
8284
$this->redis->set($this->fixKey($key), serialize($value), $ttl);
8385
$this->logger->info("[Redis Cache] Set '$key' result ");
8486

@@ -98,7 +100,7 @@ public function clear()
98100
{
99101
$keys = $this->redis->keys('cache:*');
100102
foreach ((array)$keys as $key) {
101-
if (preg_match('/^cache\:(?<key>.*)/', $key, $matches)) {
103+
if (preg_match('/^cache:(?<key>.*)/', $key, $matches)) {
102104
$this->delete($matches['key']);
103105
}
104106
}

0 commit comments

Comments
 (0)