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 | 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 | public function setNamespace(string $namespace) { |
||
47 | |||
48 | /** |
||
49 | * Set a value to a key on cache. |
||
50 | * |
||
51 | * @param string $key the key to be setted. |
||
52 | * @param mixed $value the correspondent value of that cache key. |
||
53 | * @param int|null $ttl the cache life time in seconds (If no value passed will use the default value). |
||
54 | * |
||
55 | * @return bool true on success or false on failure. |
||
56 | */ |
||
57 | public function set(string $key, $value, int $ttl = null) : bool { |
||
74 | |||
75 | /** |
||
76 | * Return the valid cache value stored with the given key. |
||
77 | * |
||
78 | * @param string $key the cache key to be found. |
||
79 | * |
||
80 | * @return mixed the data found. |
||
81 | */ |
||
82 | public function get(string $key) { |
||
92 | |||
93 | /** |
||
94 | * Delete cache especified by key. |
||
95 | * |
||
96 | * @param string $key the cache key to be deleted. |
||
97 | * |
||
98 | * @return bool true on success or false on failure. |
||
99 | */ |
||
100 | public function delete(string $key) : bool { |
||
103 | |||
104 | /** |
||
105 | * Check if given key exists and is valid on cache. |
||
106 | * |
||
107 | * @param string $key the cache key to be verified. |
||
108 | * @param bool $isValid if set to true the function will verify if it is valid (not expired). |
||
109 | * |
||
110 | * @return bool true if exists false otherwise. |
||
111 | */ |
||
112 | public function exists(string $key, bool $isValid = false) : bool { |
||
116 | |||
117 | /** |
||
118 | * Renew the cache expiration time. |
||
119 | * |
||
120 | * @param string $key the cache key to be renewed. |
||
121 | * @param int|null $ttl extra time to live in seconds. |
||
122 | * |
||
123 | * @return bool true on success or false on failure. |
||
124 | */ |
||
125 | public function renew(string $key, int $ttl = null) : bool { |
||
140 | |||
141 | /** |
||
142 | * Clear the cache directory. |
||
143 | * |
||
144 | * @param string|null $namespace the cache namespace. |
||
145 | * |
||
146 | * @return bool true on success or false on failure. |
||
147 | */ |
||
148 | public function clear(string $namespace = null) : bool { |
||
151 | |||
152 | /** |
||
153 | * Set the directory where cache operations will happen. |
||
154 | * |
||
155 | * @param string $cacheDirectory the directory where cache operations will happen. |
||
156 | * |
||
157 | * @return bool true on success or false on failure. |
||
158 | */ |
||
159 | public function setCacheDirectory(string $cacheDirectory) : bool { |
||
171 | |||
172 | /** |
||
173 | * Return the last error occurred. |
||
174 | * |
||
175 | * @return array the array with the last error data. |
||
176 | */ |
||
177 | public function getLastError() : array { |
||
180 | |||
181 | /** |
||
182 | * Return the path where the file should be located. |
||
183 | * |
||
184 | * @param string $key the cache key |
||
185 | * |
||
186 | * @return string the file path |
||
187 | */ |
||
188 | private function getFilePath(string $key) : string { |
||
192 | |||
193 | /** |
||
194 | * Set the last error that ocurred using the lib |
||
195 | * |
||
196 | * @param Exception $ex the exception |
||
197 | */ |
||
198 | private function setLastError(Exception $ex) { |
||
203 | |||
204 | /** |
||
205 | * Get the file data |
||
206 | * |
||
207 | * @param string $key the cache key |
||
208 | * |
||
209 | * @return array with the file data or empty array when no data found |
||
210 | */ |
||
211 | private function getFileData(string $key) : array { |
||
224 | } |