1 | <?php |
||
15 | class FileCache implements CacheInterface { |
||
16 | |||
17 | use CacheUtilsTrait; |
||
18 | |||
19 | const JSON_FORMAT = '.json'; |
||
20 | |||
21 | private $cacheDirectory; |
||
22 | private $ttl; |
||
23 | private $lastError = []; |
||
24 | private $namespace; |
||
25 | |||
26 | /** |
||
27 | * FileCache constructor. |
||
28 | * |
||
29 | * @param string $directory the directory where cache operations will happen. |
||
30 | * @param int $ttl the cache life time in seconds (0 = Forever). |
||
31 | * @param string $namespace the cache namespace. |
||
32 | */ |
||
33 | 6 | public function __construct(string $directory, int $ttl = 0, string $namespace = null) { |
|
40 | |||
41 | /** |
||
42 | * Set the cache namespace. |
||
43 | * |
||
44 | * @param string $namespace the cache namespace. |
||
45 | */ |
||
46 | 1 | public function setNamespace(string $namespace) : bool { |
|
57 | |||
58 | /** |
||
59 | * Set a value to a key on cache. |
||
60 | * |
||
61 | * @param string $key the key to be setted. |
||
62 | * @param mixed $value the correspondent value of that cache key. |
||
63 | * @param int|null $ttl the cache life time in seconds (If no value passed will use the default value). |
||
64 | * |
||
65 | * @return bool true on success or false on failure. |
||
66 | */ |
||
67 | 5 | public function set(string $key, $value, int $ttl = null) : bool { |
|
84 | |||
85 | /** |
||
86 | * Return the valid cache value stored with the given key. |
||
87 | * |
||
88 | * @param string $key the cache key to be found. |
||
89 | * |
||
90 | * @return mixed the data found. |
||
91 | */ |
||
92 | 4 | public function get(string $key) { |
|
102 | |||
103 | /** |
||
104 | * Delete cache especified by key. |
||
105 | * |
||
106 | * @param string $key the cache key to be deleted. |
||
107 | * |
||
108 | * @return bool true on success or false on failure. |
||
109 | */ |
||
110 | 1 | public function delete(string $key) : bool { |
|
113 | |||
114 | /** |
||
115 | * Check if given key exists and is valid on cache. |
||
116 | * |
||
117 | * @param string $key the cache key to be verified. |
||
118 | * @param bool $isValid if set to true the function will verify if it is valid (not expired). |
||
119 | * |
||
120 | * @return bool true if exists false otherwise. |
||
121 | */ |
||
122 | 1 | public function exists(string $key, bool $isValid = false) : bool { |
|
126 | |||
127 | /** |
||
128 | * Renew the cache expiration time. |
||
129 | * |
||
130 | * @param string $key the cache key to be renewed. |
||
131 | * @param int|null $ttl extra time to live in seconds. |
||
132 | * |
||
133 | * @return bool true on success or false on failure. |
||
134 | */ |
||
135 | 1 | public function renew(string $key, int $ttl = null) : bool { |
|
150 | |||
151 | /** |
||
152 | * Clear all cache files at directory. |
||
153 | * |
||
154 | * @param string|null $namespace the cache namespace. |
||
155 | * |
||
156 | * @return bool true on success or false on failure. |
||
157 | */ |
||
158 | 1 | public function clear(string $namespace = null) : bool { |
|
171 | |||
172 | /** |
||
173 | * Set the directory where cache operations will happen. |
||
174 | * |
||
175 | * @param string $cacheDirectory the directory where cache operations will happen. |
||
176 | * |
||
177 | * @return bool true on success or false on failure. |
||
178 | */ |
||
179 | 6 | public function setCacheDirectory(string $cacheDirectory) : bool { |
|
189 | |||
190 | /** |
||
191 | * Return the last error occurred. |
||
192 | * |
||
193 | * @return array the array with the last error data. |
||
194 | */ |
||
195 | public function getLastError() : array { |
||
198 | |||
199 | /** |
||
200 | * Return the path where the cache file should be located. |
||
201 | * |
||
202 | * @param string $key the cache key |
||
203 | * |
||
204 | * @return string the file path |
||
205 | */ |
||
206 | 5 | private function getFilePath(string $key) : string { |
|
209 | |||
210 | /** |
||
211 | * Return the base path where the cache files should be located. |
||
212 | * |
||
213 | * @return string the file path |
||
214 | */ |
||
215 | 6 | private function getBasePath(string $namespace = null) : string { |
|
218 | |||
219 | /** |
||
220 | * Set the last error that ocurred using the lib |
||
221 | * |
||
222 | * @param Exception $ex the exception |
||
223 | */ |
||
224 | private function setLastError(Exception $ex) { |
||
229 | |||
230 | /** |
||
231 | * Get the file data |
||
232 | * |
||
233 | * @param string $key the cache key |
||
234 | * |
||
235 | * @return array with the file data or empty array when no data found |
||
236 | */ |
||
237 | 5 | private function getFileData(string $key) : array { |
|
250 | } |