Authored by ziy

添加文件缓存

@@ -11,6 +11,7 @@ namespace Hood; @@ -11,6 +11,7 @@ namespace Hood;
11 use Hood\Cache\Memcached as Mcd; 11 use Hood\Cache\Memcached as Mcd;
12 use Hood\Cache\Memcache as Mc; 12 use Hood\Cache\Memcache as Mc;
13 use Hood\Cache\CacheRedis; 13 use Hood\Cache\CacheRedis;
  14 +use Hood\Cache\FileCache;
14 15
15 class Cache 16 class Cache
16 { 17 {
@@ -47,4 +48,17 @@ class Cache @@ -47,4 +48,17 @@ class Cache
47 $persistentID = ''; 48 $persistentID = '';
48 return new CacheRedis($servers, $persistentID); 49 return new CacheRedis($servers, $persistentID);
49 } 50 }
  51 +
  52 +
  53 + /**
  54 + *
  55 + * @param null $childNode
  56 + * @param string $node
  57 + * @param null $cachePath
  58 + * @return FileCache
  59 + */
  60 + static public function File($childNode = null, $node = 'cache', $cachePath = null)
  61 + {
  62 + return new FileCache($childNode, $node, $cachePath);
  63 + }
50 } 64 }
@@ -2,19 +2,15 @@ @@ -2,19 +2,15 @@
2 2
3 namespace Hood\Cache; 3 namespace Hood\Cache;
4 4
5 -defined('CACHE_PATH') or define('CACHE_PATH', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cached' . DIRECTORY_SEPARATOR);  
6 -  
7 -/*  
8 - * To change this template, choose Tools | Templates  
9 - * and open the template in the editor.  
10 - */ 5 +use Hood\Core\Root;
  6 +use Yaf\Exception;
11 7
12 /** 8 /**
13 * Description of cache 9 * Description of cache
14 * 10 *
15 * @author 13011908 11 * @author 13011908
16 */ 12 */
17 -class FileCache implements CacheInterface 13 +class FileCache extends Root implements CacheInterface
18 { 14 {
19 15
20 const DEFAULT_EXPIRE = 3600; 16 const DEFAULT_EXPIRE = 3600;
@@ -24,6 +20,58 @@ class FileCache implements CacheInterface @@ -24,6 +20,58 @@ class FileCache implements CacheInterface
24 protected $_cache_dir; 20 protected $_cache_dir;
25 protected $_tag = null; 21 protected $_tag = null;
26 22
  23 + private $section = 'file';
  24 + private $node = 'cache';
  25 +
  26 + public function __construct($childNode = null, $node = 'cache', $cachePath = null)
  27 + {
  28 + if ($cachePath == null) {
  29 + $server = $this->getServerHost('cache');
  30 + $this->node = ($node == null ? 'cache' : $node);
  31 + $_pathList = $cachePath = $server->getServerConfig($this->section, $this->node);
  32 + if (empty($_pathList)) {
  33 + $cachePath = (sys_get_temp_dir() . 'cached' . DIRECTORY_SEPARATOR);
  34 + } elseif ($childNode != null) {
  35 + $cachePath = $_pathList[$childNode];
  36 + } elseif (is_array($cachePath)) {
  37 + throw new \Exception('Cache Path is Array.');
  38 + }
  39 + }
  40 + $this->initFileInfo($cachePath);
  41 + }
  42 +
  43 + private function initFileInfo($directory)
  44 + {
  45 + try {
  46 + $this->_cache_dir = new \SplFileInfo($directory);
  47 + } // PHP < 5.3 exception handle
  48 + catch (ErrorException $e) {
  49 + $this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
  50 + } // PHP >= 5.3 exception handle
  51 + catch (UnexpectedValueException $e) {
  52 + $this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
  53 + }
  54 +
  55 + // If the defined directory is a file, get outta here
  56 + if ($this->_cache_dir->isFile()) {
  57 + throw new Exception('Unable to create cache directory as a file already exists : ' . $this->_cache_dir->getRealPath());
  58 + }
  59 +
  60 + if (!$this->_cache_dir->isDir()) {
  61 + $this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);
  62 + }
  63 +
  64 + // Check the read status of the directory
  65 + if (!$this->_cache_dir->isReadable()) {
  66 + throw new Exception('Unable to read from the cache directory ' . $this->_cache_dir->getRealPath());
  67 + }
  68 +
  69 + // Check the write status of the directory
  70 + if (!$this->_cache_dir->isWritable()) {
  71 + throw new Exception('Unable to write to the cache directory ' . $this->_cache_dir->getRealPath());
  72 + }
  73 + }
  74 +
27 public function add($key, $value, $minutes) 75 public function add($key, $value, $minutes)
28 { 76 {
29 } 77 }
@@ -92,39 +140,6 @@ class FileCache implements CacheInterface @@ -92,39 +140,6 @@ class FileCache implements CacheInterface
92 return self::$instances[$group]; 140 return self::$instances[$group];
93 } 141 }
94 142
95 - public function __construct()  
96 - {  
97 - try {  
98 - $directory = CACHE_PATH;  
99 - $this->_cache_dir = new \SplFileInfo($directory);  
100 - } // PHP < 5.3 exception handle  
101 - catch (ErrorException $e) {  
102 - $this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);  
103 - } // PHP >= 5.3 exception handle  
104 - catch (UnexpectedValueException $e) {  
105 - $this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);  
106 - }  
107 -  
108 - // If the defined directory is a file, get outta here  
109 - if ($this->_cache_dir->isFile()) {  
110 - throw new Exception('Unable to create cache directory as a file already exists : ' . $this->_cache_dir->getRealPath());  
111 - }  
112 -  
113 - if (!$this->_cache_dir->isDir()) {  
114 - $this->_cache_dir = $this->_make_directory($directory, 0777, TRUE);  
115 - }  
116 -  
117 - // Check the read status of the directory  
118 - if (!$this->_cache_dir->isReadable()) {  
119 - throw new Exception('Unable to read from the cache directory ' . $this->_cache_dir->getRealPath());  
120 - }  
121 -  
122 - // Check the write status of the directory  
123 - if (!$this->_cache_dir->isWritable()) {  
124 - throw new Exception('Unable to write to the cache directory ' . $this->_cache_dir->getRealPath());  
125 - }  
126 - }  
127 -  
128 /** 143 /**
129 * Retrieve a cached value entry by id. 144 * Retrieve a cached value entry by id.
130 * 145 *
@@ -211,7 +226,6 @@ class FileCache implements CacheInterface @@ -211,7 +226,6 @@ class FileCache implements CacheInterface
211 { 226 {
212 $filename = self::filename($this->_sanitize_id($id)); 227 $filename = self::filename($this->_sanitize_id($id));
213 $directory = $this->_resolve_directory($filename); 228 $directory = $this->_resolve_directory($filename);
214 -  
215 // If lifetime is NULL 229 // If lifetime is NULL
216 if ($lifetime === NULL) { 230 if ($lifetime === NULL) {
217 // Set to the default expiry 231 // Set to the default expiry
  1 +#File
  2 + 缓存路径配置在cache.config.ini里面
  3 + [file]
  4 + cache=/tmp/yoho
  5 + $file = Cache::File();
  6 +
  7 + [file]
  8 + cache.path = /tmp/yoho
  9 + $file = Cache::File('path');
  10 +
  11 + [file]
  12 + fileCache.path = /tmp/yoho
  13 + $file = Cache::File('path','fileCache');
  14 +
  15 + 实现了get、set、del 等方法
  16 + $file = Cache::File('path','cache');
  17 + $file->set('a', '00');
  18 + $file->get('a');
@@ -221,6 +221,18 @@ class Quick extends Connection @@ -221,6 +221,18 @@ class Quick extends Connection
221 } 221 }
222 222
223 /** 223 /**
  224 + * 取回所有结果行的第一个字段名
  225 + * @param $sql
  226 + * @param array $parameterMap
  227 + * @param array $replaceMap
  228 + * @return string
  229 + */
  230 + public function fetchCol($sql, $parameterMap = array(), $replaceMap = array())
  231 + {
  232 + return $this->quicken($sql, $parameterMap, $replaceMap, __FUNCTION__);
  233 + }
  234 +
  235 + /**
224 * db cache 对象 236 * db cache 对象
225 * @return \Hood\Cache\CacheInterface 237 * @return \Hood\Cache\CacheInterface
226 */ 238 */