1 | <?php |
||
14 | class FileCache implements CacheInterface |
||
15 | { |
||
16 | use CacheUtilsTrait; |
||
17 | |||
18 | const JSON_FORMAT = '.json'; |
||
19 | |||
20 | private $cacheDirectory; |
||
21 | private $ttl; |
||
22 | private $lastError = []; |
||
23 | private $namespace; |
||
24 | |||
25 | /** |
||
26 | * FileCache constructor. |
||
27 | * |
||
28 | * @param string $directory the directory where cache operations will happen. |
||
29 | * @param int $ttl the cache life time in seconds (0 = Forever). |
||
30 | * @param string $namespace the cache namespace. |
||
31 | */ |
||
32 | 7 | 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 | * @return bool true on success or false on failure. |
||
47 | */ |
||
48 | 2 | public function setNamespace(string $namespace) : bool |
|
62 | |||
63 | /** |
||
64 | * Set a value to a key on cache. |
||
65 | * |
||
66 | * @param string $key the key to be set. |
||
67 | * @param mixed $value the correspondent value of that cache key. |
||
68 | * @param int|null $ttl the cache life time in seconds (If no value passed will use the default value). |
||
69 | * |
||
70 | * @return bool true on success or false on failure. |
||
71 | */ |
||
72 | 5 | public function set(string $key, $value, int $ttl = null) : bool |
|
89 | |||
90 | /** |
||
91 | * Return the valid cache value stored with the given key. |
||
92 | * |
||
93 | * @param string $key the cache key to be found. |
||
94 | * |
||
95 | * @return mixed the data found. |
||
96 | */ |
||
97 | 5 | public function get(string $key) |
|
107 | |||
108 | /** |
||
109 | * Delete cache especified by key. |
||
110 | * |
||
111 | * @param string $key the cache key to be deleted. |
||
112 | * |
||
113 | * @return bool true on success or false on failure. |
||
114 | */ |
||
115 | 1 | public function delete(string $key) : bool |
|
119 | |||
120 | /** |
||
121 | * Check if given key exists and is valid on cache. |
||
122 | * |
||
123 | * @param string $key the cache key to be verified. |
||
124 | * @param bool $isValid if set to true the function will verify if it is valid (not expired). |
||
125 | * |
||
126 | * @return bool true if exists false otherwise. |
||
127 | */ |
||
128 | 1 | public function exists(string $key, bool $isValid = false) : bool |
|
134 | |||
135 | /** |
||
136 | * Renew the cache expiration time. |
||
137 | * |
||
138 | * @param string $key the cache key to be renewed. |
||
139 | * @param int|null $ttl extra time to live in seconds. |
||
140 | * |
||
141 | * @return bool true on success or false on failure. |
||
142 | */ |
||
143 | 1 | public function renew(string $key, int $ttl = null) : bool |
|
159 | |||
160 | /** |
||
161 | * Clear all cache files at directory. |
||
162 | * |
||
163 | * @param string|null $namespace the cache namespace. |
||
164 | * |
||
165 | * @return bool true on success or false on failure. |
||
166 | */ |
||
167 | 1 | public function clear(string $namespace = null) : bool |
|
181 | |||
182 | /** |
||
183 | * Set the directory where cache operations will happen. |
||
184 | * |
||
185 | * @param string $cacheDirectory the directory where cache operations will happen. |
||
186 | * |
||
187 | * @return bool true on success or false on failure. |
||
188 | */ |
||
189 | 7 | public function setCacheDirectory(string $cacheDirectory) : bool |
|
202 | |||
203 | /** |
||
204 | * Return the last error occurred. |
||
205 | * |
||
206 | * @return array the array with the last error data. |
||
207 | */ |
||
208 | 1 | public function getLastError() : array |
|
212 | |||
213 | /** |
||
214 | * Return the path where the cache file should be located. |
||
215 | * |
||
216 | * @param string $key the cache key |
||
217 | * |
||
218 | * @return string the file path |
||
219 | */ |
||
220 | 6 | private function getFilePath(string $key) : string |
|
224 | |||
225 | /** |
||
226 | * Return the base path where the cache files should be located. |
||
227 | * |
||
228 | * @param string $namespace the namespace |
||
229 | * |
||
230 | * @return string the file path |
||
231 | */ |
||
232 | 7 | private function getBasePath(string $namespace = null) : string |
|
236 | |||
237 | /** |
||
238 | * Set the last error that ocurred using the lib. |
||
239 | * |
||
240 | * @param Exception $ex the exception |
||
241 | */ |
||
242 | 1 | private function setLastError(Exception $ex) |
|
248 | |||
249 | /** |
||
250 | * Get the file data. |
||
251 | * |
||
252 | * @param string $key the cache key |
||
253 | * |
||
254 | * @return array with the file data or empty array when no data found |
||
255 | */ |
||
256 | 6 | private function getFileData(string $key) : array |
|
271 | } |
||
272 |