1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Bram Gerritsen [email protected] |
4
|
|
|
* @copyright (c) Bram Gerritsen 2013 |
5
|
|
|
* @license http://opensource.org/licenses/mit-license.php |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace StrokerCache\Service; |
9
|
|
|
|
10
|
|
|
use StrokerCache\Exception\UnsupportedAdapterException; |
11
|
|
|
use StrokerCache\IdGenerator\IdGeneratorInterface; |
12
|
|
|
use Zend\Mvc\MvcEvent; |
13
|
|
|
use StrokerCache\Event\CacheEvent; |
14
|
|
|
use Zend\EventManager\EventManager; |
15
|
|
|
use Zend\EventManager\EventManagerInterface; |
16
|
|
|
use StrokerCache\Options\ModuleOptions; |
17
|
|
|
use Zend\Cache\Storage\TaggableInterface; |
18
|
|
|
use Zend\Cache\Storage\StorageInterface; |
19
|
|
|
|
20
|
|
|
class CacheService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Prefix to use for the tag key |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const TAG_PREFIX = 'strokercache_'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventManagerInterface |
30
|
|
|
*/ |
31
|
|
|
protected $eventManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var StorageInterface |
35
|
|
|
*/ |
36
|
|
|
protected $cacheStorage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var IdGeneratorInterface |
40
|
|
|
*/ |
41
|
|
|
protected $idGenerator; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ModuleOptions |
45
|
|
|
*/ |
46
|
|
|
protected $options; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Default constructor |
50
|
|
|
* |
51
|
|
|
* @param StorageInterface $cacheStorage |
52
|
|
|
* @param ModuleOptions $options |
53
|
|
|
* @param IdGeneratorInterface $idGenerator |
54
|
|
|
*/ |
55
|
|
|
public function __construct(StorageInterface $cacheStorage, ModuleOptions $options, IdGeneratorInterface $idGenerator = null) |
56
|
|
|
{ |
57
|
|
|
$this->setCacheStorage($cacheStorage); |
58
|
|
|
$this->setOptions($options); |
59
|
|
|
$this->setIdGenerator($idGenerator); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if a page is saved in the cache and return contents. Return null when no item is found. |
64
|
|
|
* |
65
|
|
|
* @param MvcEvent $mvcEvent |
66
|
|
|
* @return mixed|null |
67
|
|
|
*/ |
68
|
|
|
public function load(MvcEvent $mvcEvent) |
69
|
|
|
{ |
70
|
|
|
$id = $this->getIdGenerator()->generate(); |
71
|
|
|
if (!$this->getCacheStorage()->hasItem($id)) { |
72
|
|
|
return null; |
73
|
|
|
}; |
74
|
|
|
|
75
|
|
|
$event = $this->createCacheEvent(CacheEvent::EVENT_LOAD, $mvcEvent, $id); |
76
|
|
|
|
77
|
|
|
$results = $this->getEventManager()->triggerEventUntil(function ($result) { |
78
|
|
|
return ($result === false); |
79
|
|
|
}, $event); |
80
|
|
|
|
81
|
|
|
if ($results->stopped()) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->getCacheStorage()->getItem($id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Save the page contents to the cache storage. |
90
|
|
|
* |
91
|
|
|
* @param MvcEvent $mvcEvent |
92
|
|
|
*/ |
93
|
|
|
public function save(MvcEvent $mvcEvent) |
94
|
|
|
{ |
95
|
|
|
if (!$this->shouldCacheRequest($mvcEvent)) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$id = $this->getIdGenerator()->generate(); |
100
|
|
|
|
101
|
|
|
$item = ($this->getOptions()->getCacheResponse() === true) ? serialize($mvcEvent->getResponse()) : $mvcEvent->getResponse()->getContent(); |
102
|
|
|
|
103
|
|
|
$this->getCacheStorage()->setItem($id, $item); |
104
|
|
|
|
105
|
|
|
$cacheEvent = $this->createCacheEvent(CacheEvent::EVENT_SAVE, $mvcEvent, $id); |
106
|
|
|
$this->getEventManager()->triggerEvent($cacheEvent); |
107
|
|
|
|
108
|
|
|
$cacheStorage = $this->getCacheStorage(); |
109
|
|
|
if ($cacheStorage instanceof TaggableInterface) { |
110
|
|
|
$tags = array_unique(array_merge($this->getTags($mvcEvent), $cacheEvent->getTags())); |
111
|
|
|
$cacheStorage->setTags($id, $this->prefixTags($tags)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine if we should cache the current request |
117
|
|
|
* |
118
|
|
|
* @param MvcEvent $mvcEvent |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
protected function shouldCacheRequest(MvcEvent $mvcEvent) |
122
|
|
|
{ |
123
|
|
|
$event = $this->createCacheEvent(CacheEvent::EVENT_SHOULDCACHE, $mvcEvent); |
124
|
|
|
|
125
|
|
|
$results = $this->getEventManager()->triggerEventUntil(function ($result) { |
126
|
|
|
return $result; |
127
|
|
|
}, $event); |
128
|
|
|
|
129
|
|
|
if ($results->stopped()) { |
130
|
|
|
return $results->last(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $tags |
138
|
|
|
* @param bool|null $disjunction |
139
|
|
|
* @return bool |
140
|
|
|
* @throws UnsupportedAdapterException |
141
|
|
|
*/ |
142
|
|
|
public function clearByTags(array $tags = array(), $disjunction = null) |
143
|
|
|
{ |
144
|
|
|
$cacheStorage = $this->getCacheStorage(); |
145
|
|
|
if (!$cacheStorage instanceof TaggableInterface) { |
146
|
|
|
throw new UnsupportedAdapterException('purging by tags is only supported on adapters implementing the TaggableInterface'); |
147
|
|
|
} |
148
|
|
|
$tags = $this->prefixTags($tags); |
149
|
|
|
|
150
|
|
|
return $cacheStorage->clearByTags($tags, $disjunction); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Cache tags to use for this page |
155
|
|
|
* |
156
|
|
|
* @param MvcEvent $event |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function getTags(MvcEvent $event) |
160
|
|
|
{ |
161
|
|
|
$routeName = $event->getRouteMatch()->getMatchedRouteName(); |
162
|
|
|
$tags = [ |
163
|
|
|
'route_' . $routeName |
164
|
|
|
]; |
165
|
|
|
foreach ($event->getRouteMatch()->getParams() as $key => $value) { |
166
|
|
|
if ($key == 'controller') { |
167
|
|
|
$tags[] = 'controller_' . $value; |
168
|
|
|
} else { |
169
|
|
|
$tags[] = 'param_' . $key . '_' . $value; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $tags; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $tags |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
private function prefixTags(array $tags) |
182
|
|
|
{ |
183
|
|
|
return array_map( |
184
|
|
|
function ($tag) { return CacheService::TAG_PREFIX . $tag; }, |
185
|
|
|
$tags |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $eventName |
191
|
|
|
* @param string $cacheKey |
192
|
|
|
* @param MvcEvent|null $mvcEvent |
193
|
|
|
* @return CacheEvent |
194
|
|
|
*/ |
195
|
|
|
protected function createCacheEvent($eventName, MvcEvent $mvcEvent = null, $cacheKey = null) |
196
|
|
|
{ |
197
|
|
|
$cacheEvent = new CacheEvent($eventName, $this); |
198
|
|
|
$cacheEvent->setCacheKey($cacheKey); |
199
|
|
|
if ($mvcEvent !== null) { |
200
|
|
|
$cacheEvent->setMvcEvent($mvcEvent); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $cacheEvent; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return StorageInterface |
208
|
|
|
*/ |
209
|
|
|
public function getCacheStorage() |
210
|
|
|
{ |
211
|
|
|
return $this->cacheStorage; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param StorageInterface $cacheStorage |
216
|
|
|
* @return self |
217
|
|
|
*/ |
218
|
|
|
public function setCacheStorage($cacheStorage) |
219
|
|
|
{ |
220
|
|
|
$this->cacheStorage = $cacheStorage; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return ModuleOptions |
227
|
|
|
*/ |
228
|
|
|
public function getOptions() |
229
|
|
|
{ |
230
|
|
|
return $this->options; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param ModuleOptions $options |
235
|
|
|
* @return self |
236
|
|
|
*/ |
237
|
|
|
public function setOptions($options) |
238
|
|
|
{ |
239
|
|
|
$this->options = $options; |
240
|
|
|
|
241
|
|
|
return $this; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Inject an EventManager instance |
246
|
|
|
* |
247
|
|
|
* @param EventManagerInterface $eventManager |
248
|
|
|
* @return self |
249
|
|
|
*/ |
250
|
|
|
public function setEventManager(EventManagerInterface $eventManager) |
251
|
|
|
{ |
252
|
|
|
$eventManager->setIdentifiers([__CLASS__, get_called_class()]); |
253
|
|
|
|
254
|
|
|
$this->eventManager = $eventManager; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Retrieve the event manager |
261
|
|
|
* |
262
|
|
|
* Lazy-loads an EventManager instance if none registered. |
263
|
|
|
* |
264
|
|
|
* @return EventManagerInterface |
265
|
|
|
*/ |
266
|
|
|
public function getEventManager() |
267
|
|
|
{ |
268
|
|
|
if (!$this->eventManager instanceof EventManagerInterface) { |
269
|
|
|
$this->setEventManager(new EventManager()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $this->eventManager; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return IdGeneratorInterface |
277
|
|
|
*/ |
278
|
|
|
public function getIdGenerator() |
279
|
|
|
{ |
280
|
|
|
return $this->idGenerator; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param IdGeneratorInterface $idGenerator |
285
|
|
|
*/ |
286
|
|
|
public function setIdGenerator($idGenerator = null) |
287
|
|
|
{ |
288
|
|
|
$this->idGenerator = $idGenerator; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|