...
|
...
|
@@ -2,19 +2,15 @@ |
|
|
|
|
|
namespace Hood\Cache;
|
|
|
|
|
|
defined('CACHE_PATH') or define('CACHE_PATH', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cached' . DIRECTORY_SEPARATOR);
|
|
|
|
|
|
/*
|
|
|
* To change this template, choose Tools | Templates
|
|
|
* and open the template in the editor.
|
|
|
*/
|
|
|
use Hood\Core\Root;
|
|
|
use Yaf\Exception;
|
|
|
|
|
|
/**
|
|
|
* Description of cache
|
|
|
*
|
|
|
* @author 13011908
|
|
|
*/
|
|
|
class FileCache implements CacheInterface
|
|
|
class FileCache extends Root implements CacheInterface
|
|
|
{
|
|
|
|
|
|
const DEFAULT_EXPIRE = 3600;
|
...
|
...
|
@@ -24,6 +20,58 @@ class FileCache implements CacheInterface |
|
|
protected $_cache_dir;
|
|
|
protected $_tag = null;
|
|
|
|
|
|
private $section = 'file';
|
|
|
private $node = 'cache';
|
|
|
|
|
|
public function __construct($childNode = null, $node = 'cache', $cachePath = null)
|
|
|
{
|
|
|
if ($cachePath == null) {
|
|
|
$server = $this->getServerHost('cache');
|
|
|
$this->node = ($node == null ? 'cache' : $node);
|
|
|
$_pathList = $cachePath = $server->getServerConfig($this->section, $this->node);
|
|
|
if (empty($_pathList)) {
|
|
|
$cachePath = (sys_get_temp_dir() . 'cached' . DIRECTORY_SEPARATOR);
|
|
|
} elseif ($childNode != null) {
|
|
|
$cachePath = $_pathList[$childNode];
|
|
|
} elseif (is_array($cachePath)) {
|
|
|
throw new \Exception('Cache Path is Array.');
|
|
|
}
|
|
|
}
|
|
|
$this->initFileInfo($cachePath);
|
|
|
}
|
|
|
|
|
|
private function initFileInfo($directory)
|
|
|
{
|
|
|
try {
|
|
|
$this->_cache_dir = new \SplFileInfo($directory);
|
|
|
} // PHP < 5.3 exception handle
|
|
|
catch (ErrorException $e) {
|
|
|
$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
|
|
|
} // PHP >= 5.3 exception handle
|
|
|
catch (UnexpectedValueException $e) {
|
|
|
$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
|
|
|
}
|
|
|
|
|
|
// If the defined directory is a file, get outta here
|
|
|
if ($this->_cache_dir->isFile()) {
|
|
|
throw new Exception('Unable to create cache directory as a file already exists : ' . $this->_cache_dir->getRealPath());
|
|
|
}
|
|
|
|
|
|
if (!$this->_cache_dir->isDir()) {
|
|
|
$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
|
|
|
}
|
|
|
|
|
|
// Check the read status of the directory
|
|
|
if (!$this->_cache_dir->isReadable()) {
|
|
|
throw new Exception('Unable to read from the cache directory ' . $this->_cache_dir->getRealPath());
|
|
|
}
|
|
|
|
|
|
// Check the write status of the directory
|
|
|
if (!$this->_cache_dir->isWritable()) {
|
|
|
throw new Exception('Unable to write to the cache directory ' . $this->_cache_dir->getRealPath());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function add($key, $value, $minutes)
|
|
|
{
|
|
|
}
|
...
|
...
|
@@ -92,39 +140,6 @@ class FileCache implements CacheInterface |
|
|
return self::$instances[$group];
|
|
|
}
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
try {
|
|
|
$directory = CACHE_PATH;
|
|
|
$this->_cache_dir = new \SplFileInfo($directory);
|
|
|
} // PHP < 5.3 exception handle
|
|
|
catch (ErrorException $e) {
|
|
|
$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
|
|
|
} // PHP >= 5.3 exception handle
|
|
|
catch (UnexpectedValueException $e) {
|
|
|
$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
|
|
|
}
|
|
|
|
|
|
// If the defined directory is a file, get outta here
|
|
|
if ($this->_cache_dir->isFile()) {
|
|
|
throw new Exception('Unable to create cache directory as a file already exists : ' . $this->_cache_dir->getRealPath());
|
|
|
}
|
|
|
|
|
|
if (!$this->_cache_dir->isDir()) {
|
|
|
$this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
|
|
|
}
|
|
|
|
|
|
// Check the read status of the directory
|
|
|
if (!$this->_cache_dir->isReadable()) {
|
|
|
throw new Exception('Unable to read from the cache directory ' . $this->_cache_dir->getRealPath());
|
|
|
}
|
|
|
|
|
|
// Check the write status of the directory
|
|
|
if (!$this->_cache_dir->isWritable()) {
|
|
|
throw new Exception('Unable to write to the cache directory ' . $this->_cache_dir->getRealPath());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Retrieve a cached value entry by id.
|
|
|
*
|
...
|
...
|
@@ -211,7 +226,6 @@ class FileCache implements CacheInterface |
|
|
{
|
|
|
$filename = self::filename($this->_sanitize_id($id));
|
|
|
$directory = $this->_resolve_directory($filename);
|
|
|
|
|
|
// If lifetime is NULL
|
|
|
if ($lifetime === NULL) {
|
|
|
// Set to the default expiry
|
...
|
...
|
|