1
|
|
|
<?php namespace Comodojo\Cache\Drivers; |
2
|
|
|
|
3
|
|
|
use \Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package Comodojo Cache |
7
|
|
|
* @author Marco Giovinazzi <[email protected]> |
8
|
|
|
* @license MIT |
9
|
|
|
* |
10
|
|
|
* LICENSE: |
11
|
|
|
* |
12
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
13
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
14
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
15
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
16
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
17
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
18
|
|
|
* THE SOFTWARE. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class FilesystemXattr extends AbstractDriver { |
22
|
|
|
|
23
|
|
|
const DRIVER_NAME = "filesystem-xattr"; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Cache (files) repository |
27
|
|
|
* |
28
|
|
|
* @param string |
29
|
|
|
*/ |
30
|
|
|
protected $cache_folder; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
47 |
|
public function __construct(array $configuration) { |
36
|
|
|
|
37
|
47 |
|
$this->cache_folder = $configuration['cache-folder']; |
38
|
|
|
|
39
|
47 |
|
if ( $this->test() === false ) throw new Exception("Folder ".$this->cache_folder." not writable"); |
40
|
|
|
|
41
|
47 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
47 |
|
public function test() { |
47
|
|
|
|
48
|
47 |
|
return file_exists($this->cache_folder) && is_writable($this->cache_folder); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
34 |
|
public function get($key, $namespace) { |
56
|
|
|
|
57
|
34 |
|
if ( $this->has($key, $namespace) ) { |
58
|
|
|
|
59
|
19 |
|
$cacheFile = $this->cache_folder."$key-$namespace.cache"; |
60
|
|
|
|
61
|
19 |
|
$data = @file_get_contents($cacheFile); |
62
|
|
|
|
63
|
19 |
|
if ( $data === false ) throw new Exception("Error reading cache content $cacheFile"); |
64
|
|
|
|
65
|
19 |
|
return $data; |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
22 |
|
return null; |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function set($key, $namespace, $value, $ttl = null) { |
77
|
|
|
|
78
|
|
|
$cacheFile = $this->cache_folder."$key-$namespace.cache"; |
79
|
|
|
|
80
|
|
|
if ( $ttl == null || $ttl == 0 ) { |
81
|
|
|
$ttl = 0; |
82
|
|
|
} else { |
83
|
|
|
$ttl = time() + intval($ttl); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$cached = @file_put_contents($cacheFile, $value, LOCK_EX); |
87
|
|
|
|
88
|
|
|
if ( $cached === false ) throw new Exception("Error writing cache object $cacheFile"); |
89
|
|
|
|
90
|
|
|
$tagged = @xattr_set($cacheFile, "EXPIRE", $ttl, XATTR_DONTFOLLOW); |
|
|
|
|
91
|
|
|
|
92
|
|
|
if ( $tagged === false ) throw new Exception("Error writing cache ttl $cacheFile"); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function delete($key, $namespace) { |
102
|
|
|
|
103
|
|
|
$cacheFile = $this->cache_folder."$key-$namespace.cache"; |
104
|
|
|
|
105
|
|
|
if ( file_exists($cacheFile) ) { |
106
|
|
|
|
107
|
|
|
$u = unlink($cacheFile); |
108
|
|
|
|
109
|
|
|
return $u; |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function clear($namespace = null) { |
121
|
|
|
|
122
|
|
|
$result = []; |
123
|
|
|
|
124
|
|
|
if ( $namespace === null ) { |
125
|
|
|
|
126
|
|
|
$file_list = glob($this->cache_folder."*.cache"); |
127
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
|
130
|
|
|
$file_list = glob($this->cache_folder."*-$namespace.cache"); |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ( empty($file_list) ) return true; |
135
|
|
|
|
136
|
|
|
foreach ( $file_list as $file ) $result[] = unlink($file); |
137
|
|
|
|
138
|
|
|
return !in_array(false, $result); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
2 |
|
public function getMultiple(array $keys, $namespace) { |
146
|
|
|
|
147
|
2 |
|
$result = []; |
148
|
|
|
|
149
|
2 |
|
foreach ( $keys as $key ) { |
150
|
2 |
|
$result[$key] = $this->get($key, $namespace); |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
return $result; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
1 |
|
public function setMultiple(array $key_values, $namespace, $ttl = null) { |
161
|
|
|
|
162
|
1 |
|
$result = []; |
163
|
|
|
|
164
|
1 |
|
foreach ( $key_values as $key => $value ) { |
165
|
1 |
|
$result[] = $this->set($key, $namespace, $value, $ttl); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
return !in_array(false, $result); |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
3 |
|
public function deleteMultiple(array $keys, $namespace) { |
176
|
|
|
|
177
|
3 |
|
$result = []; |
178
|
|
|
|
179
|
3 |
|
foreach ( $keys as $key ) { |
180
|
3 |
|
$result[] = $this->delete($key, $namespace); |
181
|
|
|
} |
182
|
|
|
|
183
|
3 |
|
return !in_array(false, $result); |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
|
|
public function has($key, $namespace) { |
191
|
|
|
|
192
|
|
|
$time = time(); |
193
|
|
|
|
194
|
|
|
$cacheFile = $this->cache_folder."$key-$namespace.cache"; |
195
|
|
|
|
196
|
|
|
if ( file_exists($cacheFile) ) { |
197
|
|
|
|
198
|
|
|
$expire = xattr_get($cacheFile, "EXPIRE", XATTR_DONTFOLLOW); |
|
|
|
|
199
|
|
|
|
200
|
|
|
if ( $expire === false ) { |
201
|
|
|
throw new Exception("Error reading cache ttl for $cacheFile"); |
202
|
|
|
} else if ( $expire < $time && $expire != 0 ) { |
203
|
|
|
$this->delete($key, $namespace); |
204
|
|
|
return false; |
205
|
|
|
} else { |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return false; |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
2 |
|
public function stats() { |
219
|
|
|
|
220
|
2 |
|
return ['objects' => count(glob($this->cache_folder."*.cache"))]; |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|