1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\system; |
4
|
|
|
|
5
|
|
|
use Ubiquity\controllers\admin\popo\CacheFile; |
6
|
|
|
use Ubiquity\utils\base\UFileSystem; |
7
|
|
|
use Ubiquity\exceptions\CacheException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class is responsible for storing Arrays in PHP files. |
11
|
|
|
*/ |
12
|
|
|
class ArrayCache extends AbstractDataCache { |
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* @var int The file mode used when creating new cache files |
16
|
|
|
*/ |
17
|
|
|
private $_fileMode; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initializes the file cache-provider |
21
|
|
|
* |
22
|
|
|
* @param string $root |
23
|
|
|
* absolute path to the root-folder where cache-files will be stored |
24
|
|
|
* @param string $postfix |
25
|
|
|
* Termination of file names |
26
|
|
|
* @param array $cacheParams |
27
|
|
|
* defaults to ["fileMode"=>"0755"] |
28
|
|
|
*/ |
29
|
33 |
|
public function __construct($root, $postfix = "", $cacheParams = []) { |
30
|
33 |
|
parent::__construct ( $root, $postfix ); |
31
|
33 |
|
$this->_fileMode = (isset ( $cacheParams ["fileMode"] )) ? $cacheParams ["fileMode"] : 0755; |
32
|
33 |
|
if (! is_dir ( $root )) |
33
|
1 |
|
\mkdir ( $root, $this->_fileMode, true ); |
34
|
33 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check if annotation-data for the key has been stored. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* cache key |
41
|
|
|
* @return boolean true if data with the given key has been stored; otherwise false |
42
|
|
|
*/ |
43
|
23 |
|
public function exists($key) { |
44
|
23 |
|
return file_exists ( $this->_getPath ( $key ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Caches the given data with the given key. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* cache key |
52
|
|
|
* @param string $content |
53
|
|
|
* the source-code to be cached |
54
|
|
|
* @throws CacheException if file could not be written |
55
|
|
|
*/ |
56
|
3 |
|
protected function storeContent($key, $content, $tag) { |
57
|
3 |
|
$path = $this->_getPath ( $key ); |
58
|
3 |
|
$dir = pathinfo ( $path, PATHINFO_DIRNAME ); |
59
|
3 |
|
if (UFileSystem::safeMkdir ( $dir )) { |
60
|
3 |
|
if (@\file_put_contents ( $path, $content, LOCK_EX ) === false) { |
61
|
|
|
throw new CacheException ( "Unable to write cache file: {$path}" ); |
62
|
|
|
} |
63
|
3 |
|
if (@\chmod ( $path, $this->_fileMode ) === false) { |
64
|
|
|
throw new CacheException ( "Unable to set permissions of cache file: {$path}" ); |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
|
|
throw new CacheException ( "Unable to create folder : {$dir}" ); |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Fetches data stored for the given key. |
73
|
|
|
* |
74
|
|
|
* @param string $key |
75
|
|
|
* cache key |
76
|
|
|
* @return mixed the cached data |
77
|
|
|
*/ |
78
|
21 |
|
public function fetch($key) { |
79
|
21 |
|
return include ($this->_getPath ( $key )); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* return data stored for the given key. |
84
|
|
|
* |
85
|
|
|
* @param string $key |
86
|
|
|
* cache key |
87
|
|
|
* @return mixed the cached data |
88
|
|
|
*/ |
89
|
|
|
public function file_get_contents($key) { |
90
|
|
|
return \file_get_contents ( $this->_getPath ( $key ) ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the timestamp of the last cache update for the given key. |
95
|
|
|
* |
96
|
|
|
* @param string $key |
97
|
|
|
* cache key |
98
|
|
|
* @return int unix timestamp |
99
|
|
|
*/ |
100
|
|
|
public function getTimestamp($key) { |
101
|
|
|
return \filemtime ( $this->_getPath ( $key ) ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Maps a cache-key to the absolute path of a PHP file |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* cache key |
109
|
|
|
* @return string absolute path of the PHP file |
110
|
|
|
*/ |
111
|
23 |
|
private function _getPath($key) { |
112
|
23 |
|
return $this->_root . $key . $this->postfix . '.php'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::remove() |
119
|
|
|
*/ |
120
|
|
|
public function remove($key) { |
121
|
|
|
$file = $this->_getPath ( $key ); |
122
|
|
|
if (\file_exists ( $file )) |
123
|
|
|
return \unlink ( $file ); |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::clear() |
131
|
|
|
*/ |
132
|
|
|
public function clear($matches = "") { |
133
|
|
|
$files = glob ( $this->_root . $matches . '*' ); |
134
|
|
|
foreach ( $files as $file ) { |
135
|
|
|
if (\is_file ( $file )) |
136
|
|
|
\unlink ( $file ); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::getCacheFiles() |
144
|
|
|
*/ |
145
|
1 |
|
public function getCacheFiles($type) { |
146
|
|
|
return CacheFile::initFromFiles ( $this->_root . $type, \ucfirst ( $type ), function ($file) use ($type) { |
147
|
1 |
|
$file = \basename ( $file ); |
148
|
1 |
|
return $type . \DS . substr ( $file, 0, strpos ( $file, $this->postfix . '.php' ) ); |
149
|
1 |
|
} ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::clearCache() |
156
|
|
|
*/ |
157
|
|
|
public function clearCache($type) { |
158
|
|
|
CacheFile::delete ( $this->_root . \strtolower ( $type ) ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::getCacheInfo() |
165
|
|
|
*/ |
166
|
3 |
|
public function getCacheInfo() { |
167
|
3 |
|
$result = parent::getCacheInfo (); |
168
|
3 |
|
$result .= "<br>Root cache directory is <b>" . UFileSystem::cleanPathname ( $this->_root ) . "</b>."; |
169
|
3 |
|
return $result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::getEntryKey() |
176
|
|
|
*/ |
177
|
3 |
|
public function getEntryKey($key) { |
178
|
3 |
|
return UFileSystem::cleanFilePathname ( $this->_getPath ( $key ) ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
* @see \Ubiquity\cache\system\AbstractDataCache::setRoot() |
185
|
|
|
*/ |
186
|
33 |
|
public function setRoot($root) { |
187
|
33 |
|
$this->_root = rtrim ( $root, \DS ) . \DS; |
188
|
33 |
|
} |
189
|
|
|
} |
190
|
|
|
|