1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Cache; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File based cache in line with PSR-3. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class FileCache |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a key to use for the cache. |
16
|
|
|
* |
17
|
|
|
* @param string $class name of the class, including |
18
|
|
|
* namespace. |
19
|
|
|
* @param string $id unique id for item in each class. |
20
|
|
|
* |
21
|
|
|
* @return string the filename. |
22
|
|
|
*/ |
23
|
|
|
public function createKey($class, $id) |
24
|
|
|
{ |
25
|
|
|
return str_replace('\\', '-', $class) . '#' . $id; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Generate a filename for the cached object. |
32
|
|
|
* |
33
|
|
|
* @param string $key to the cached object. |
34
|
|
|
* |
35
|
|
|
* @return string the filename. |
36
|
|
|
*/ |
37
|
|
|
public function filename($key) |
38
|
|
|
{ |
39
|
|
|
return $this->config['basepath'] . '/' . $key; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get an item from the cache if available. |
46
|
|
|
* |
47
|
|
|
* @param string $key to the cached object. |
48
|
|
|
* @param boolean $age check the age or not, defaults to |
49
|
|
|
* false. |
50
|
|
|
* |
51
|
|
|
* @return mixed the cached object or false if it has aged |
52
|
|
|
* or null if it does not exists. |
53
|
|
|
*/ |
54
|
|
|
public function get($key, $age = false) |
55
|
|
|
{ |
56
|
|
|
$file = $this->filename($key); |
57
|
|
|
|
58
|
|
|
if (is_file($file)) { |
59
|
|
|
if ($age) { |
60
|
|
|
$age = filemtime($file) + $this->config['age'] > time(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$age) { |
64
|
|
|
// json |
65
|
|
|
// text |
66
|
|
|
return unserialize(file_get_contents($file)); |
67
|
|
|
} |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Put an item to the cache. |
77
|
|
|
* |
78
|
|
|
* @param string $key to the cached object. |
79
|
|
|
* @param mixed $item the object to be cached. |
80
|
|
|
* |
81
|
|
|
* @throws Exception if failing to write to cache. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function put($key, $item) |
86
|
|
|
{ |
87
|
|
|
$file = $this->filename($key); |
88
|
|
|
|
89
|
|
|
// json |
90
|
|
|
// text |
91
|
|
|
if (!file_put_contents($file, serialize($item))) { |
92
|
|
|
throw new \Exception( |
93
|
|
|
t("Failed writing cache object '!key'.", [ |
|
|
|
|
94
|
|
|
'!key' => $key |
95
|
|
|
]) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Prune a item from cache. |
104
|
|
|
* |
105
|
|
|
* @param string $key to the cached object. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function prune($key) |
110
|
|
|
{ |
111
|
|
|
@unlink($this->filename($key)); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Prune all items from cache. |
118
|
|
|
* |
119
|
|
|
* @return int number of items removed. |
120
|
|
|
*/ |
121
|
|
|
public function pruneAll() |
122
|
|
|
{ |
123
|
|
|
$files = glob($this->config['basepath'] . '/*'); |
|
|
|
|
124
|
|
|
$items = count($files); |
125
|
|
|
array_map('unlink', $files); |
126
|
|
|
return $items; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|