1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Cache; |
4
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* File based cache in line with PSR-3. |
9
|
|
|
*/ |
10
|
|
|
class FileCache implements CacheInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string $cachePath the path to the cache dir. |
14
|
|
|
*/ |
15
|
|
|
private $cachePath; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set the base for the cache path where all items are stored. |
21
|
|
|
* |
22
|
|
|
* @param string $path A valid writable path. |
23
|
|
|
* |
24
|
|
|
* @return void. |
|
|
|
|
25
|
|
|
* |
26
|
|
|
* @throws \Psr\SimpleCache\CacheException when the path is not writable. |
27
|
|
|
*/ |
28
|
|
|
public function setPath(string $path) : void |
29
|
|
|
{ |
30
|
|
|
if (!is_writable($path)) { |
31
|
|
|
throw new Exception("The path to the cache is not writable."); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->cachePath = $path; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Fetches a value from the cache. |
41
|
|
|
* |
42
|
|
|
* @param string $key The unique key of this item in the cache. |
43
|
|
|
* @param mixed $default Default value to return if the key does not exist. |
44
|
|
|
* |
45
|
|
|
* @return mixed The value of the item from the cache, or $default in case |
46
|
|
|
* of cache miss. |
47
|
|
|
* |
48
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
49
|
|
|
* MUST be thrown if the $key string is not a legal value. |
50
|
|
|
*/ |
51
|
|
|
public function get($key, $default = null) |
52
|
|
|
{ |
53
|
|
|
$file = $this->filename($key); |
54
|
|
|
|
55
|
|
|
if (is_file($file)) { |
56
|
|
|
if ($age) { |
|
|
|
|
57
|
|
|
$age = filemtime($file) + $this->config['age'] > time(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$age) { |
|
|
|
|
61
|
|
|
// json |
62
|
|
|
// text |
63
|
|
|
return unserialize(file_get_contents($file)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Persists data in the cache, uniquely referenced by a key with an |
76
|
|
|
* optional expiration TTL time. |
77
|
|
|
* |
78
|
|
|
* @param string $key The key of the item to store. |
79
|
|
|
* @param mixed $value The value of the item to store, |
80
|
|
|
* must be serializable. |
81
|
|
|
* @param null|int|\DateInterval $ttl Optional. The TTL value of this |
82
|
|
|
* item. If no value is sent and |
83
|
|
|
* the driver supports TTL then the |
84
|
|
|
* library may set a default value |
85
|
|
|
* for it or let the driver take care |
86
|
|
|
* of that. |
87
|
|
|
* |
88
|
|
|
* @return bool True on success and false on failure. |
89
|
|
|
* |
90
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
91
|
|
|
* MUST be thrown if the $key string is not a legal value. |
92
|
|
|
*/ |
93
|
|
|
public function set($key, $value, $ttl = null) |
94
|
|
|
{ |
95
|
|
|
$file = $this->filename($key); |
96
|
|
|
|
97
|
|
|
// json |
98
|
|
|
// text |
99
|
|
|
if (!file_put_contents($file, serialize($item))) { |
|
|
|
|
100
|
|
|
throw new Exception("Failed writing cache object '$key'."); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Delete an item from the cache by its unique key. |
108
|
|
|
* |
109
|
|
|
* @param string $key The unique cache key of the item to delete. |
110
|
|
|
* |
111
|
|
|
* @return bool True if the item was successfully removed. False if there was an error. |
112
|
|
|
* |
113
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
114
|
|
|
* MUST be thrown if the $key string is not a legal value. |
115
|
|
|
*/ |
116
|
|
|
public function delete($key) |
117
|
|
|
{ |
118
|
|
|
@unlink($this->filename($key)); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Wipes clean the entire cache's keys. |
125
|
|
|
* |
126
|
|
|
* @return bool True on success and false on failure. |
127
|
|
|
*/ |
128
|
|
|
public function clear() |
129
|
|
|
{ |
130
|
|
|
$files = glob($this->config['basepath'] . '/*'); |
|
|
|
|
131
|
|
|
$items = count($files); |
|
|
|
|
132
|
|
|
array_map('unlink', $files); |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Obtains multiple cache items by their unique keys. |
140
|
|
|
* |
141
|
|
|
* @param iterable $keys A list of keys that can obtained in a single |
142
|
|
|
* operation. |
143
|
|
|
* @param mixed $default Default value to return for keys that do not |
144
|
|
|
* exist. |
145
|
|
|
* |
146
|
|
|
* @return iterable A list of key => value pairs. Cache keys that do not |
147
|
|
|
* exist or are stale will have $default as value. |
148
|
|
|
* |
149
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
150
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable, |
151
|
|
|
* or if any of the $keys are not a legal value. |
152
|
|
|
*/ |
153
|
|
|
public function getMultiple($keys, $default = null) |
154
|
|
|
{ |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Persists a set of key => value pairs in the cache, with an optional TTL. |
162
|
|
|
* |
163
|
|
|
* @param iterable $values A list of key => value pairs for a |
164
|
|
|
* multiple-set operation. |
165
|
|
|
* @param null|int|\DateInterval $ttl Optional. The TTL value of this |
166
|
|
|
* item. If no value is sent and |
167
|
|
|
* the driver supports TTL then the |
168
|
|
|
* library may set a default value |
169
|
|
|
* for it or let the driver take care |
170
|
|
|
* of that. |
171
|
|
|
* |
172
|
|
|
* @return bool True on success and false on failure. |
173
|
|
|
* |
174
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
175
|
|
|
* MUST be thrown if $values is neither an array nor a Traversable, |
176
|
|
|
* or if any of the $values are not a legal value. |
177
|
|
|
*/ |
178
|
|
|
public function setMultiple($values, $ttl = null) |
179
|
|
|
{ |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Deletes multiple cache items in a single operation. |
187
|
|
|
* |
188
|
|
|
* @param iterable $keys A list of string-based keys to be deleted. |
189
|
|
|
* |
190
|
|
|
* @return bool True if the items were successfully removed. False if there |
191
|
|
|
* was an error. |
192
|
|
|
* |
193
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
194
|
|
|
* MUST be thrown if $keys is neither an array nor a Traversable, |
195
|
|
|
* or if any of the $keys are not a legal value. |
196
|
|
|
*/ |
197
|
|
|
public function deleteMultiple($keys) |
198
|
|
|
{ |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Determines whether an item is present in the cache. |
206
|
|
|
* |
207
|
|
|
* NOTE: It is recommended that has() is only to be used for cache warming |
208
|
|
|
* type purposes and not to be used within your live applications |
209
|
|
|
* operations for get/set, as this method is subject to a race condition |
210
|
|
|
* where your has() will return true and immediately after, another script |
211
|
|
|
* can remove it making the state of your app out of date. |
212
|
|
|
* |
213
|
|
|
* @param string $key The cache item key. |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
* |
217
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
218
|
|
|
* MUST be thrown if the $key string is not a legal value. |
219
|
|
|
*/ |
220
|
|
|
public function has($key) |
221
|
|
|
{ |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Create a key to use for the cache. |
229
|
|
|
* |
230
|
|
|
* @param string $class name of the class, including |
231
|
|
|
* namespace. |
232
|
|
|
* @param string $id unique id for item in each class. |
233
|
|
|
* |
234
|
|
|
* @return string the filename. |
235
|
|
|
*/ |
236
|
|
|
private function createKey($class, $id) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
return str_replace('\\', '-', $class) . '#' . $id; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Generate a filename for the cached object. |
245
|
|
|
* |
246
|
|
|
* @param string $key to the cached object. |
247
|
|
|
* |
248
|
|
|
* @return string the filename. |
249
|
|
|
*/ |
250
|
|
|
private function filename($key) |
251
|
|
|
{ |
252
|
|
|
return $cachePath . '/' . $key; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
|
256
|
|
|
|
257
|
|
|
// /** |
258
|
|
|
// * Get an item from the cache if available. |
259
|
|
|
// * |
260
|
|
|
// * @param string $key to the cached object. |
261
|
|
|
// * @param boolean $age check the age or not, defaults to |
262
|
|
|
// * false. |
263
|
|
|
// * |
264
|
|
|
// * @return mixed the cached object or false if it has aged |
265
|
|
|
// * or null if it does not exists. |
266
|
|
|
// */ |
267
|
|
|
// public function get($key, $age = false) |
268
|
|
|
// { |
269
|
|
|
// $file = $this->filename($key); |
270
|
|
|
// |
271
|
|
|
// if (is_file($file)) { |
272
|
|
|
// if ($age) { |
273
|
|
|
// $age = filemtime($file) + $this->config['age'] > time(); |
274
|
|
|
// } |
275
|
|
|
// |
276
|
|
|
// if (!$age) { |
277
|
|
|
// // json |
278
|
|
|
// // text |
279
|
|
|
// return unserialize(file_get_contents($file)); |
280
|
|
|
// } |
281
|
|
|
// return false; |
282
|
|
|
// } |
283
|
|
|
// return null; |
284
|
|
|
// } |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
|
288
|
|
|
// /** |
289
|
|
|
// * Put an item to the cache. |
290
|
|
|
// * |
291
|
|
|
// * @param string $key to the cached object. |
292
|
|
|
// * @param mixed $item the object to be cached. |
293
|
|
|
// * |
294
|
|
|
// * @throws Exception if failing to write to cache. |
295
|
|
|
// * |
296
|
|
|
// * @return void |
297
|
|
|
// */ |
298
|
|
|
// public function put($key, $item) |
299
|
|
|
// { |
300
|
|
|
// $file = $this->filename($key); |
301
|
|
|
// |
302
|
|
|
// // json |
303
|
|
|
// // text |
304
|
|
|
// if (!file_put_contents($file, serialize($item))) { |
305
|
|
|
// throw new \Exception( |
306
|
|
|
// t("Failed writing cache object '!key'.", [ |
307
|
|
|
// '!key' => $key |
308
|
|
|
// ]) |
309
|
|
|
// ); |
310
|
|
|
// } |
311
|
|
|
// } |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
|
315
|
|
|
// /** |
316
|
|
|
// * Prune a item from cache. |
317
|
|
|
// * |
318
|
|
|
// * @param string $key to the cached object. |
319
|
|
|
// * |
320
|
|
|
// * @return void |
321
|
|
|
// */ |
322
|
|
|
// public function prune($key) |
323
|
|
|
// { |
324
|
|
|
// @unlink($this->filename($key)); |
325
|
|
|
// } |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
|
329
|
|
|
// /** |
330
|
|
|
// * Prune all items from cache. |
331
|
|
|
// * |
332
|
|
|
// * @return int number of items removed. |
333
|
|
|
// */ |
334
|
|
|
// public function pruneAll() |
335
|
|
|
// { |
336
|
|
|
// $files = glob($this->config['basepath'] . '/*'); |
337
|
|
|
// $items = count($files); |
338
|
|
|
// array_map('unlink', $files); |
339
|
|
|
// return $items; |
340
|
|
|
// } |
341
|
|
|
} |
342
|
|
|
|