Authored by ziy

添加文件缓存

... ... @@ -11,6 +11,7 @@ namespace Hood;
use Hood\Cache\Memcached as Mcd;
use Hood\Cache\Memcache as Mc;
use Hood\Cache\CacheRedis;
use Hood\Cache\FileCache;
class Cache
{
... ... @@ -47,4 +48,17 @@ class Cache
$persistentID = '';
return new CacheRedis($servers, $persistentID);
}
/**
*
* @param null $childNode
* @param string $node
* @param null $cachePath
* @return FileCache
*/
static public function File($childNode = null, $node = 'cache', $cachePath = null)
{
return new FileCache($childNode, $node, $cachePath);
}
}
\ No newline at end of file
... ...
... ... @@ -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
... ...
#File
缓存路径配置在cache.config.ini里面
[file]
cache=/tmp/yoho
$file = Cache::File();
[file]
cache.path = /tmp/yoho
$file = Cache::File('path');
[file]
fileCache.path = /tmp/yoho
$file = Cache::File('path','fileCache');
实现了get、set、del 等方法
$file = Cache::File('path','cache');
$file->set('a', '00');
$file->get('a');
\ No newline at end of file
... ...
... ... @@ -221,6 +221,18 @@ class Quick extends Connection
}
/**
* 取回所有结果行的第一个字段名
* @param $sql
* @param array $parameterMap
* @param array $replaceMap
* @return string
*/
public function fetchCol($sql, $parameterMap = array(), $replaceMap = array())
{
return $this->quicken($sql, $parameterMap, $replaceMap, __FUNCTION__);
}
/**
* db cache 对象
* @return \Hood\Cache\CacheInterface
*/
... ...