1 | <?php |
||
15 | class FileCache implements CacheInterface { |
||
16 | |||
17 | const JSON_FORMAT = '.json'; |
||
18 | |||
19 | private $cacheDirectory; |
||
20 | private $ttl; |
||
21 | private $lastError = []; |
||
22 | private $namespace; |
||
23 | |||
24 | /** |
||
25 | * FileCache constructor. |
||
26 | * |
||
27 | * @param string $directory the directory where cache operations will happen. |
||
28 | * @param int $ttl the cache life time in seconds (0 = Forever). |
||
29 | * @param string $namespace the cache namespace. |
||
30 | */ |
||
31 | 6 | public function __construct(string $directory, int $ttl = 0, string $namespace = null) { |
|
38 | |||
39 | /** |
||
40 | * Set the cache namespace. |
||
41 | * |
||
42 | * @param string $namespace the cache namespace. |
||
43 | */ |
||
44 | 1 | public function setNamespace(string $namespace) : bool { |
|
55 | |||
56 | /** |
||
57 | * Set a value to a key on cache. |
||
58 | * |
||
59 | * @param string $key the key to be setted. |
||
60 | * @param mixed $value the correspondent value of that cache key. |
||
61 | * @param int|null $ttl the cache life time in seconds (If no value passed will use the default value). |
||
62 | * |
||
63 | * @return bool true on success or false on failure. |
||
64 | */ |
||
65 | 5 | public function set(string $key, $value, int $ttl = null) : bool { |
|
82 | |||
83 | /** |
||
84 | * Return the valid cache value stored with the given key. |
||
85 | * |
||
86 | * @param string $key the cache key to be found. |
||
87 | * |
||
88 | * @return mixed the data found. |
||
89 | */ |
||
90 | 4 | public function get(string $key) { |
|
100 | |||
101 | /** |
||
102 | * Delete cache especified by key. |
||
103 | * |
||
104 | * @param string $key the cache key to be deleted. |
||
105 | * |
||
106 | * @return bool true on success or false on failure. |
||
107 | */ |
||
108 | 1 | public function delete(string $key) : bool { |
|
111 | |||
112 | /** |
||
113 | * Check if given key exists and is valid on cache. |
||
114 | * |
||
115 | * @param string $key the cache key to be verified. |
||
116 | * @param bool $isValid if set to true the function will verify if it is valid (not expired). |
||
117 | * |
||
118 | * @return bool true if exists false otherwise. |
||
119 | */ |
||
120 | 1 | public function exists(string $key, bool $isValid = false) : bool { |
|
124 | |||
125 | /** |
||
126 | * Renew the cache expiration time. |
||
127 | * |
||
128 | * @param string $key the cache key to be renewed. |
||
129 | * @param int|null $ttl extra time to live in seconds. |
||
130 | * |
||
131 | * @return bool true on success or false on failure. |
||
132 | */ |
||
133 | 1 | public function renew(string $key, int $ttl = null) : bool { |
|
148 | |||
149 | /** |
||
150 | * Clear all cache files at directory. |
||
151 | * |
||
152 | * @param string|null $namespace the cache namespace. |
||
153 | * |
||
154 | * @return bool true on success or false on failure. |
||
155 | */ |
||
156 | 1 | public function clear(string $namespace = null) : bool { |
|
169 | |||
170 | /** |
||
171 | * Set the directory where cache operations will happen. |
||
172 | * |
||
173 | * @param string $cacheDirectory the directory where cache operations will happen. |
||
174 | * |
||
175 | * @return bool true on success or false on failure. |
||
176 | */ |
||
177 | 6 | public function setCacheDirectory(string $cacheDirectory) : bool { |
|
187 | |||
188 | /** |
||
189 | * Return the last error occurred. |
||
190 | * |
||
191 | * @return array the array with the last error data. |
||
192 | */ |
||
193 | public function getLastError() : array { |
||
196 | |||
197 | /** |
||
198 | * Return the path where the cache file should be located. |
||
199 | * |
||
200 | * @param string $key the cache key |
||
201 | * |
||
202 | * @return string the file path |
||
203 | */ |
||
204 | 5 | private function getFilePath(string $key) : string { |
|
207 | |||
208 | /** |
||
209 | * Return the base path where the cache files should be located. |
||
210 | * |
||
211 | * @return string the file path |
||
212 | */ |
||
213 | 6 | private function getBasePath(string $namespace = null) : string { |
|
216 | |||
217 | /** |
||
218 | * Set the last error that ocurred using the lib |
||
219 | * |
||
220 | * @param Exception $ex the exception |
||
221 | */ |
||
222 | private function setLastError(Exception $ex) { |
||
227 | |||
228 | /** |
||
229 | * Get the file data |
||
230 | * |
||
231 | * @param string $key the cache key |
||
232 | * |
||
233 | * @return array with the file data or empty array when no data found |
||
234 | */ |
||
235 | 5 | private function getFileData(string $key) : array { |
|
248 | |||
249 | /** |
||
250 | * Glob that is safe with streams (vfs for example) |
||
251 | * |
||
252 | * @param string $directory the directory |
||
253 | * @param string $filePattern the file pattern |
||
254 | * |
||
255 | * @return array containing match files |
||
256 | */ |
||
257 | 1 | private function streamSafeGlob($directory, $filePattern) : array { |
|
273 | } |