diff --git a/src/AbstractCache.php b/src/AbstractCache.php index 378e8fd..df45cf6 100644 --- a/src/AbstractCache.php +++ b/src/AbstractCache.php @@ -14,6 +14,20 @@ abstract class AbstractCache implements CacheInterface { + /** @var int */ + protected int $_ttl = 3600; + + /** + * @param int $ttl + * + * @return AbstractCache + */ + public function setTtl(int $ttl): AbstractCache + { + $this->_ttl = $ttl; + return $this; + } + /** * @param string $key * diff --git a/src/ApcuCache.php b/src/ApcuCache.php index 7d29f67..21922e5 100644 --- a/src/ApcuCache.php +++ b/src/ApcuCache.php @@ -64,6 +64,11 @@ public function set($key, $value, $ttl = null) apcu_delete($key); } + if(!$ttl) + { + $ttl = $this->_ttl; + } + if(apcu_add($key, $value, $ttl)) { $this->_cacheKeys[] = $key; diff --git a/src/FileCache.php b/src/FileCache.php index ff7f2ab..eab6ce4 100644 --- a/src/FileCache.php +++ b/src/FileCache.php @@ -18,16 +18,12 @@ class FileCache extends AbstractCache /** @var string|null */ protected ?string $_directoryPath; - /** @var int */ - protected int $_ttl = 3600; - /** * FileCache constructor. * * @param string|null $directoryPath - * @param int|null $ttl */ - public function __construct(string $directoryPath = null, int $ttl = null) + public function __construct(string $directoryPath = null) { $this->_directoryPath = $directoryPath; @@ -37,11 +33,6 @@ public function __construct(string $directoryPath = null, int $ttl = null) $this->_directoryPath = dirname(__DIR__, 4) . self::CACHE_PATH; } - if($ttl !== null) - { - $this->_ttl = $ttl; - } - // Try to create the directory if it doesn't exist if(!file_exists($this->_directoryPath) && !@mkdir($this->_directoryPath, 0777, true) && !is_dir( $this->_directoryPath @@ -110,6 +101,11 @@ public function set($key, $value, $ttl = null): bool $path = $this->_getPath($key); $this->_validateKey($key); + if(!$ttl) + { + $ttl = $this->_ttl; + } + $success = file_put_contents($path, serialize($value)); if(!$success)