Skip to content

Commit

Permalink
- default ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEssex committed Feb 9, 2021
1 parent d7ff5b1 commit 623eeea
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
5 changes: 5 additions & 0 deletions src/ApcuCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 6 additions & 10 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 623eeea

Please sign in to comment.