File based cache adapter.
Constructor.
| Type | Description |
|---|---|
| array | Configuration |
NULL
public function __construct(array $config)
{
parent::__construct($config['identifier']);
$this->path = $config['path'];
if(file_exists($this->path) === false || is_readable($this->path) === false || is_writable($this->path) === false)
{
throw new RuntimeException(vsprintf("%s(): Cache directory ('%s') is not writable.", array(__METHOD__, $this->path)));
}
}
Store variable in the cache.
| Type | Description |
|---|---|
| string | Cache key |
| mixed | The variable to store |
| int | (optional) Time to live |
boolean
public function write($key, $value, $ttl = 0)
{
$ttl = (((int) $ttl === 0) ? 31556926 : (int) $ttl) + time();
$data = "<?php defined('MAKO_APPLICATION') or die(); ?>\n{$ttl}\n" . serialize($value);
return is_int(file_put_contents("{$this->path}/mako_{$this->identifier}_{$key}.php", $data, LOCK_EX));
}
Fetch variable from the cache.
| Type | Description |
|---|---|
| string | Cache key |
mixed
public function read($key)
{
if(file_exists("{$this->path}/mako_{$this->identifier}_{$key}.php"))
{
// Cache exists
$handle = fopen("{$this->path}/mako_{$this->identifier}_{$key}.php", 'r');
fgets($handle); // skip first line
if(time() < (int) fgets($handle))
{
// Cache has not expired ... fetch it
$cache = '';
while(!feof($handle))
{
$cache .= fgets($handle);
}
fclose($handle);
return unserialize($cache);
}
else
{
// Cache has expired ... delete it
unlink("{$this->path}/mako_{$this->identifier}_{$key}.php");
return false;
}
}
else
{
// Cache doesn't exist
return false;
}
}
Delete a variable from the cache.
| Type | Description |
|---|---|
| string | Cache key |
boolean
public function delete($key)
{
if(file_exists("{$this->path}/mako_{$this->identifier}_{$key}.php"))
{
return unlink("{$this->path}/mako_{$this->identifier}_{$key}.php");
}
return false;
}
Clears the user cache.
boolean
public function clear()
{
$files = scandir($this->path);
if($files !== false)
{
foreach($files as $file)
{
if(mb_substr($file, 0, 5) === 'mako_')
{
if(unlink("{$this->path}/{$file}") === false)
{
return false;
}
}
}
}
return true;
}
Fetches variable from cache and stores it if it doesn't exist.
| Type | Description |
|---|---|
| string | Cache key |
| closure | Closure (anonymous function) that returns value to store if it doesn't already exist |
| int | (optional) Time to live |
mixed
Magic setter.
| Type | Description |
|---|---|
| string | Cache key |
| mixed | The variable to store |
NULL
Magic getter.
| Type | Description |
|---|---|
| string | Cache key |
mixed
Magic isset.
| Type | Description |
|---|---|
| string | Cache key |
boolean
Magic unsetter.
| Type | Description |
|---|---|
| string | Cache key |
NULL