|
@@ -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
|