|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cachearium\Backend; |
|
4
|
|
|
|
|
5
|
|
|
use Cachearium\CacheAbstract; |
|
6
|
|
|
use Cachearium\CacheKey; |
|
7
|
|
|
use Cachearium\CacheLogEnum; |
|
8
|
|
|
|
|
9
|
|
|
require_once(__DIR__ . '/external/Timed.php'); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Caches in filesystem |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
class CacheFilesystem extends CacheAbstract { |
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* @var \Cache_Lite_Timed |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cache; |
|
21
|
|
|
|
|
22
|
|
|
private $path = '/tmp/'; |
|
23
|
|
|
|
|
24
|
|
|
// @codeCoverageIgnoreStart |
|
25
|
|
|
/** |
|
26
|
|
|
* Cache constructor (this is a singleton). |
|
27
|
|
|
* |
|
28
|
|
|
* @return CacheFS The cache singleton. |
|
29
|
|
|
*/ |
|
30
|
|
|
static public function singleton() { |
|
31
|
|
|
static $instances; |
|
32
|
|
|
|
|
33
|
|
|
if (!isset($instances)) { |
|
34
|
|
|
$instances = new CacheFilesystem(); |
|
35
|
|
|
} |
|
36
|
|
|
return $instances; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Prevent users to clone the instance |
|
40
|
|
|
public function __clone() { |
|
41
|
|
|
trigger_error('Cloning is not allowed.', LH_TRIGGER_UNEXPECTED); |
|
42
|
|
|
} |
|
43
|
|
|
// @codeCoverageIgnoreEnd |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Constructor. |
|
47
|
|
|
* @codeCoverageIgnore |
|
48
|
|
|
*/ |
|
49
|
|
|
private function __construct() { |
|
50
|
|
|
$this->lifetime = 3600 * 24 * 30; |
|
51
|
|
|
$this->enable(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets path to store data |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $path |
|
58
|
|
|
* @throws RuntimeException |
|
59
|
|
|
* @return CacheFS |
|
60
|
|
|
* @codeCoverageIgnore |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setPath($path) { |
|
63
|
|
|
if (!is_writable($this->path) || !file_exists($this->path) || !is_dir($this->path)) { |
|
64
|
|
|
throw new RuntimeException('Invalid dir or missing permissions'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->path = $path . '/'; |
|
68
|
|
|
|
|
69
|
|
|
// reload |
|
70
|
|
|
if ($this->isEnabled()) { |
|
71
|
|
|
$this->enable(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function enable() { |
|
78
|
|
|
$this->cache = new \Cache_Lite_Timed( |
|
79
|
|
|
array( |
|
80
|
|
|
'cacheDir' => $this->path, |
|
81
|
|
|
'lifeTime' => $this->getDefaultLifetime(), // in seconds |
|
82
|
|
|
'automaticCleaningFactor' => 200, |
|
83
|
|
|
'hashedDirectoryLevel' => 1, |
|
84
|
|
|
'writeControl' => false, |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
return parent::enable(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* (non-PHPdoc) |
|
92
|
|
|
* @see \Cachearium\CacheAbstract::hashKey($k) |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function hashKey(CacheKey $k) { |
|
95
|
|
|
$group = $this->namespace . $k->base . $k->id; |
|
96
|
|
|
return $group; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* (non-PHPdoc) |
|
101
|
|
|
* @see \Cachearium\CacheAbstract::increment($value, $k, $default) |
|
102
|
|
|
*/ |
|
103
|
|
|
public function increment($value, CacheKey $k, $default = 0) { |
|
104
|
|
|
throw new \Cachearium\Exceptions\CacheUnsupportedOperation(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* (non-PHPdoc) |
|
109
|
|
|
* @see \Cachearium\CacheAbstract::get($k) |
|
110
|
|
|
*/ |
|
111
|
|
|
public function get(CacheKey $k) { |
|
112
|
|
|
// @codeCoverageIgnoreStart |
|
113
|
|
|
if (!$this->enabled) { |
|
114
|
|
|
throw new \Cachearium\Exceptions\NotCachedException(); |
|
115
|
|
|
} |
|
116
|
|
|
// @codeCoverageIgnoreEnd |
|
117
|
|
|
|
|
118
|
|
|
$group = $this->hashKey($k); |
|
119
|
|
|
if (!is_string($k->sub)) { |
|
120
|
|
|
$cacheid = md5(serialize($k->sub)); |
|
121
|
|
|
} |
|
122
|
|
|
else { |
|
123
|
|
|
$cacheid = $k->sub; |
|
124
|
|
|
} |
|
125
|
|
|
$retval = $this->cache->get($cacheid, $group); |
|
126
|
|
|
|
|
127
|
|
|
$this->log( |
|
128
|
|
|
($retval !== false ? CacheLogEnum::ACCESSED : CacheLogEnum::MISSED), |
|
129
|
|
|
$k |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
if ($retval) { |
|
133
|
|
|
return unserialize($retval); |
|
134
|
|
|
} |
|
135
|
|
|
throw new \Cachearium\Exceptions\NotCachedException(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* (non-PHPdoc) |
|
140
|
|
|
* @see \Cachearium\CacheAbstract::store($data, $k, $lifetime) |
|
141
|
|
|
*/ |
|
142
|
|
|
public function store($data, CacheKey $k, $lifetime = -1) { |
|
143
|
|
|
// @codeCoverageIgnoreStart |
|
144
|
|
|
if (!$this->enabled) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
// @codeCoverageIgnoreEnd |
|
148
|
|
|
|
|
149
|
|
|
$group = $this->hashKey($k); |
|
150
|
|
|
if (!is_string($k->sub)) { |
|
151
|
|
|
$cacheid = md5(serialize($k->sub)); |
|
152
|
|
|
} |
|
153
|
|
|
else { |
|
154
|
|
|
$cacheid = $k->sub; |
|
155
|
|
|
} |
|
156
|
|
|
return $this->cache->save( |
|
157
|
|
|
serialize($data), $cacheid, $group, ($lifetime < 0 ? $this->getDefaultLifetime() : $lifetime) |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* (non-PHPdoc) |
|
163
|
|
|
* @see \Cachearium\CacheAbstract::delete($k) |
|
164
|
|
|
*/ |
|
165
|
|
|
public function delete(CacheKey $k) { |
|
166
|
|
|
$group = $this->hashKey($k); |
|
167
|
|
|
if (!is_string($k->sub)) { |
|
168
|
|
|
$cacheid = md5(serialize($k->sub)); |
|
169
|
|
|
} |
|
170
|
|
|
else { |
|
171
|
|
|
$cacheid = $k->sub; |
|
172
|
|
|
} |
|
173
|
|
|
$this->log(CacheLogEnum::DELETED, $k); |
|
174
|
|
|
return $this->cache->remove($cacheid, $group); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* (non-PHPdoc) |
|
179
|
|
|
* @see \Cachearium\CacheAbstract::setDefaultLifetime($lifetime) |
|
180
|
|
|
*/ |
|
181
|
|
|
public function setDefaultLifetime($lifetime = 0) { |
|
182
|
|
|
parent::setDefaultLifetime($lifetime); |
|
183
|
|
|
$this->cache->setLifeTime($this->getDefaultLifetime()); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* (non-PHPdoc) |
|
188
|
|
|
* @see \Cachearium\CacheAbstract::cleanP($base, $id) |
|
189
|
|
|
*/ |
|
190
|
|
|
public function cleanP($base, $id) { |
|
191
|
|
|
// @codeCoverageIgnoreStart |
|
192
|
|
|
if (!$this->enabled) { |
|
193
|
|
|
return false; |
|
194
|
|
|
} |
|
195
|
|
|
// @codeCoverageIgnoreEnd |
|
196
|
|
|
|
|
197
|
|
|
$group = $this->hashKey(new CacheKey($base, $id)); |
|
198
|
|
|
$retval = $this->cache->clean($group, 'ingroup'); |
|
199
|
|
|
$this->log(CacheLogEnum::CLEANED, new CacheKey($base, $id)); |
|
200
|
|
|
return $retval; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* (non-PHPdoc) |
|
205
|
|
|
* @see \Cachearium\CacheAbstract::clear() |
|
206
|
|
|
*/ |
|
207
|
|
|
public function clear() { |
|
208
|
|
|
if ($this->cache) { |
|
209
|
|
|
$this->cache->clean(); |
|
210
|
|
|
} |
|
211
|
|
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* (non-PHPdoc) |
|
216
|
|
|
* @see CacheAbstract::prefetch() |
|
217
|
|
|
* @codeCoverageIgnore |
|
218
|
|
|
*/ |
|
219
|
|
|
public function prefetch($data) { |
|
220
|
|
|
// nothing. |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* (non-PHPdoc) |
|
225
|
|
|
* @see \Cachearium\CacheAbstract::report() |
|
226
|
|
|
* @codeCoverageIgnore |
|
227
|
|
|
*/ |
|
228
|
|
|
public function report() { |
|
229
|
|
|
if ($this->should_log == false) { |
|
230
|
|
|
return; |
|
231
|
|
|
} |
|
232
|
|
|
echo '<div class="cachearium cachearium-filesystem"><h2>Cache FS system</h2>'; |
|
233
|
|
|
echo '<h3>System is: ' . ($this->enabled ? 'enabled' : 'disabled') . '</h3>'; |
|
234
|
|
|
echo '<ul>'; |
|
235
|
|
|
foreach ($this->cache_log as $entry) { |
|
236
|
|
|
echo '<li>' . CacheLogEnum::getName($entry['status']) . $entry['message'] . '</li>'; |
|
237
|
|
|
} |
|
238
|
|
|
echo '</ul></div>'; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|