1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Saito\Cache; |
14
|
|
|
|
15
|
|
|
use Cake\Cache\Cache; |
16
|
|
|
use Cake\Event\Event; |
17
|
|
|
use Cake\Event\EventListenerInterface; |
18
|
|
|
use Cake\Event\EventManager; |
19
|
|
|
use Saito\Event\SaitoEventListener; |
20
|
|
|
use Saito\Event\SaitoEventManager; |
21
|
|
|
|
22
|
|
|
class CacheSupport implements EventListenerInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
protected $_Caches = []; |
26
|
|
|
|
27
|
|
|
protected $_buildInCaches = [ |
28
|
|
|
// php caches |
29
|
|
|
'ApcCacheSupportCachelet', |
30
|
|
|
'OpCacheSupportCachelet', |
31
|
|
|
// application caches |
32
|
|
|
'EntriesCacheSupportCachelet', |
33
|
|
|
'CakeCacheSupportCachelet', |
34
|
|
|
'SaitoCacheSupportCachelet', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
protected $metaKeys = [ |
38
|
|
|
'Thread' => ['EntriesCache', 'LineCache'], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
foreach ($this->_buildInCaches as $_name) { |
47
|
|
|
$name = 'Saito\Cache\\' . $_name; |
48
|
|
|
$this->add(new $name()); |
49
|
|
|
} |
50
|
|
|
EventManager::instance()->on($this); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function implementedEvents() |
57
|
|
|
{ |
58
|
|
|
return ['Cmd.Cache.clear' => 'onClear']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Clears out cache by name in $event['cache']; |
63
|
|
|
* |
64
|
|
|
* @param Event $event event |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function onClear(Event $event) |
68
|
|
|
{ |
69
|
|
|
$cache = $event->getData('cache'); |
70
|
|
|
$id = $event->getData('id'); |
71
|
|
|
$this->clear($cache, $id); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Clear cache |
76
|
|
|
* |
77
|
|
|
* @param mixed $cache cache to clear |
78
|
|
|
* null: all |
79
|
|
|
* string: name of specific cache |
80
|
|
|
* array: multiple name strings |
81
|
|
|
* @param null $id id |
|
|
|
|
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function clear($cache = null, $id = null) |
85
|
|
|
{ |
86
|
|
|
if (is_string($cache) && isset($this->metaKeys[$cache])) { |
87
|
|
|
$cache = $this->metaKeys[$cache]; |
88
|
|
|
} |
89
|
|
|
if (is_array($cache)) { |
90
|
|
|
foreach ($cache as $_c) { |
91
|
|
|
$this->clear($_c, $id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
if ($cache === null) { |
97
|
|
|
foreach ($this->_Caches as $_Cache) { |
98
|
|
|
$_Cache->clear(); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
if (isset($this->_Caches[$cache])) { |
102
|
|
|
$this->_Caches[$cache]->clear($id); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* add cachelet |
109
|
|
|
* |
110
|
|
|
* @param CacheSupportCacheletInterface $cache cachelet |
111
|
|
|
* @param string $id id |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function add(CacheSupportCacheletInterface $cache, $id = null) |
115
|
|
|
{ |
116
|
|
|
if ($id === null) { |
117
|
|
|
$id = $cache->getId(); |
118
|
|
|
} |
119
|
|
|
if (!isset($this->_Caches[$id])) { |
120
|
|
|
$this->_Caches[$id] = $cache; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
//@codingStandardsIgnoreStart |
126
|
|
|
interface CacheSupportCacheletInterface |
127
|
|
|
//@codingStandardsIgnoreEnd |
128
|
|
|
{ |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* clear cachelet cache |
132
|
|
|
* |
133
|
|
|
* @param string $id id |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function clear($id = null); |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get cachelet id |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getId(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
//@codingStandardsIgnoreStart |
147
|
|
|
class SaitoCacheSupportCachelet extends CacheSupportCachelet |
148
|
|
|
//@codingStandardsIgnoreEnd |
149
|
|
|
{ |
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
|
|
public function clear($id = null) |
154
|
|
|
{ |
155
|
|
|
Cache::clear(false, 'default'); |
156
|
|
|
Cache::clear(false, 'long'); |
157
|
|
|
Cache::clear(false, 'short'); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
//@codingStandardsIgnoreStart |
162
|
|
|
class ApcCacheSupportCachelet extends CacheSupportCachelet |
163
|
|
|
//@codingStandardsIgnoreEnd |
164
|
|
|
{ |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritDoc} |
168
|
|
|
*/ |
169
|
|
|
public function clear($id = null) |
170
|
|
|
{ |
171
|
|
|
if (!function_exists('apcu_store')) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
apcu_clear_cache(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
//@codingStandardsIgnoreStart |
179
|
|
|
class OpCacheSupportCachelet extends CacheSupportCachelet |
180
|
|
|
//@codingStandardsIgnoreEnd |
181
|
|
|
{ |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
|
|
public function clear($id = null) |
187
|
|
|
{ |
188
|
|
|
if (!function_exists('opcache_reset')) { |
189
|
|
|
return; |
190
|
|
|
} |
191
|
|
|
opcache_reset(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
//@codingStandardsIgnoreStart |
196
|
|
|
class CakeCacheSupportCachelet extends CacheSupportCachelet |
197
|
|
|
//@codingStandardsIgnoreEnd |
198
|
|
|
{ |
199
|
|
|
|
200
|
|
|
protected $_title = 'Cake'; |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritDoc} |
204
|
|
|
*/ |
205
|
|
|
public function clear($id = null) |
206
|
|
|
{ |
207
|
|
|
Cache::clearGroup('persistent'); |
208
|
|
|
Cache::clearGroup('models'); |
209
|
|
|
Cache::clearGroup('views'); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
//@codingStandardsIgnoreStart |
214
|
|
|
class EntriesCacheSupportCachelet extends CacheSupportCachelet implements |
215
|
|
|
//@codingStandardsIgnoreEnd |
216
|
|
|
EventListenerInterface, |
217
|
|
|
SaitoEventListener |
218
|
|
|
{ |
219
|
|
|
|
220
|
|
|
// only rename if you rename event cmds triggering this cache |
221
|
|
|
protected $_title = 'EntriesCache'; |
222
|
|
|
|
223
|
|
|
protected $_CacheTree; |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Constructor. |
227
|
|
|
*/ |
228
|
|
|
public function __construct() |
229
|
|
|
{ |
230
|
|
|
EventManager::instance()->on($this); |
231
|
|
|
SaitoEventManager::getInstance()->attach($this); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritDoc} |
236
|
|
|
*/ |
237
|
|
|
public function implementedEvents() |
238
|
|
|
{ |
239
|
|
|
return [ |
240
|
|
|
'Model.Thread.change' => 'onThreadChanged', |
241
|
|
|
'Model.Entry.replyToEntry' => 'onEntryChanged', |
242
|
|
|
'Model.Entry.update' => 'onEntryChanged', |
243
|
|
|
]; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritDoc} |
248
|
|
|
*/ |
249
|
|
|
public function implementedSaitoEvents() |
250
|
|
|
{ |
251
|
|
|
return [ |
252
|
|
|
'saito.core.posting.delete.after' => 'onDelete', |
253
|
|
|
]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* on delete |
258
|
|
|
* |
259
|
|
|
* @param Event $event event |
260
|
|
|
* @return void |
261
|
|
|
*/ |
262
|
|
|
public function onDelete($event) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$this->clear(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* on thread changed |
269
|
|
|
* |
270
|
|
|
* @param Event $event event |
271
|
|
|
* @return void |
272
|
|
|
*/ |
273
|
|
|
public function onThreadChanged($event) |
|
|
|
|
274
|
|
|
{ |
275
|
|
|
$this->clear(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* on entry changed |
280
|
|
|
* |
281
|
|
|
* @param Event $event event |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
|
public function onEntryChanged($event) |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
$this->clear(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* {@inheritDoc} |
291
|
|
|
*/ |
292
|
|
|
public function clear($id = null) |
293
|
|
|
{ |
294
|
|
|
Cache::clearGroup('entries', 'entries'); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|