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); |
76
|
|
|
$event->setCacheKey($id); |
77
|
|
|
|
78
|
|
|
$results = $this->getEventManager()->triggerEventUntil(function ($result) { |
79
|
|
|
return ($result === false); |
80
|
|
|
}, $event); |
81
|
|
|
|
82
|
|
|
if ($results->stopped()) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->getCacheStorage()->getItem($id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Save the page contents to the cache storage. |
91
|
|
|
* |
92
|
|
|
* @param MvcEvent $mvcEvent |
93
|
|
|
*/ |
94
|
|
|
public function save(MvcEvent $mvcEvent) |
95
|
|
|
{ |
96
|
|
|
if (!$this->shouldCacheRequest($mvcEvent)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$id = $this->getIdGenerator()->generate(); |
101
|
|
|
|
102
|
|
|
$item = ($this->getOptions()->getCacheResponse() === true) ? serialize($mvcEvent->getResponse()) : $mvcEvent->getResponse()->getContent(); |
103
|
|
|
|
104
|
|
|
$this->getCacheStorage()->setItem($id, $item); |
105
|
|
|
|
106
|
|
|
$cacheEvent = $this->createCacheEvent(CacheEvent::EVENT_SAVE, $mvcEvent); |
107
|
|
|
$cacheEvent->setCacheKey($id); |
108
|
|
|
|
109
|
|
|
$this->getEventManager()->triggerEvent($cacheEvent); |
110
|
|
|
|
111
|
|
|
$cacheStorage = $this->getCacheStorage(); |
112
|
|
|
if ($cacheStorage instanceof TaggableInterface) { |
113
|
|
|
$cacheStorage->setTags($id, $this->getTags($mvcEvent)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine if we should cache the current request |
119
|
|
|
* |
120
|
|
|
* @param MvcEvent $mvcEvent |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
protected function shouldCacheRequest(MvcEvent $mvcEvent) |
124
|
|
|
{ |
125
|
|
|
$event = $this->createCacheEvent(CacheEvent::EVENT_SHOULDCACHE, $mvcEvent); |
126
|
|
|
|
127
|
|
|
$results = $this->getEventManager()->triggerEventUntil(function ($result) { |
128
|
|
|
return $result; |
129
|
|
|
}, $event); |
130
|
|
|
|
131
|
|
|
if ($results->stopped()) { |
132
|
|
|
return $results->last(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $tags |
140
|
|
|
* @param bool|null $disjunction |
141
|
|
|
* @return bool |
142
|
|
|
* @throws UnsupportedAdapterException |
143
|
|
|
*/ |
144
|
|
|
public function clearByTags(array $tags = array(), $disjunction = null) |
145
|
|
|
{ |
146
|
|
|
$cacheStorage = $this->getCacheStorage(); |
147
|
|
|
if (!$cacheStorage instanceof TaggableInterface) { |
148
|
|
|
throw new UnsupportedAdapterException('purging by tags is only supported on adapters implementing the TaggableInterface'); |
149
|
|
|
} |
150
|
|
|
$tags = array_map( |
151
|
|
|
function ($tag) { return CacheService::TAG_PREFIX . $tag; }, |
152
|
|
|
$tags |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
return $cacheStorage->clearByTags($tags, $disjunction); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Cache tags to use for this page |
160
|
|
|
* |
161
|
|
|
* @param MvcEvent $event |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getTags(MvcEvent $event) |
165
|
|
|
{ |
166
|
|
|
$routeName = $event->getRouteMatch()->getMatchedRouteName(); |
167
|
|
|
$tags = [ |
168
|
|
|
self::TAG_PREFIX . 'route_' . $routeName |
169
|
|
|
]; |
170
|
|
|
foreach ($event->getRouteMatch()->getParams() as $key => $value) { |
171
|
|
|
if ($key == 'controller') { |
172
|
|
|
$tags[] = self::TAG_PREFIX . 'controller_' . $value; |
173
|
|
|
} else { |
174
|
|
|
$tags[] = self::TAG_PREFIX . 'param_' . $key . '_' . $value; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $tags; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $eventName |
183
|
|
|
* @param MvcEvent|null $mvcEvent |
184
|
|
|
* @return CacheEvent |
185
|
|
|
*/ |
186
|
|
|
protected function createCacheEvent($eventName, MvcEvent $mvcEvent = null) |
187
|
|
|
{ |
188
|
|
|
$cacheEvent = new CacheEvent($eventName, $this); |
189
|
|
|
if ($mvcEvent !== null) { |
190
|
|
|
$cacheEvent->setMvcEvent($mvcEvent); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $cacheEvent; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return StorageInterface |
198
|
|
|
*/ |
199
|
|
|
public function getCacheStorage() |
200
|
|
|
{ |
201
|
|
|
return $this->cacheStorage; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param StorageInterface $cacheStorage |
206
|
|
|
* @return self |
207
|
|
|
*/ |
208
|
|
|
public function setCacheStorage($cacheStorage) |
209
|
|
|
{ |
210
|
|
|
$this->cacheStorage = $cacheStorage; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return ModuleOptions |
217
|
|
|
*/ |
218
|
|
|
public function getOptions() |
219
|
|
|
{ |
220
|
|
|
return $this->options; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param ModuleOptions $options |
225
|
|
|
* @return self |
226
|
|
|
*/ |
227
|
|
|
public function setOptions($options) |
228
|
|
|
{ |
229
|
|
|
$this->options = $options; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Inject an EventManager instance |
236
|
|
|
* |
237
|
|
|
* @param EventManagerInterface $eventManager |
238
|
|
|
* @return self |
239
|
|
|
*/ |
240
|
|
|
public function setEventManager(EventManagerInterface $eventManager) |
241
|
|
|
{ |
242
|
|
|
$eventManager->setIdentifiers([__CLASS__, get_called_class()]); |
243
|
|
|
|
244
|
|
|
$this->eventManager = $eventManager; |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Retrieve the event manager |
251
|
|
|
* |
252
|
|
|
* Lazy-loads an EventManager instance if none registered. |
253
|
|
|
* |
254
|
|
|
* @return EventManagerInterface |
255
|
|
|
*/ |
256
|
|
|
public function getEventManager() |
257
|
|
|
{ |
258
|
|
|
if (!$this->eventManager instanceof EventManagerInterface) { |
259
|
|
|
$this->setEventManager(new EventManager()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this->eventManager; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return IdGeneratorInterface |
267
|
|
|
*/ |
268
|
|
|
public function getIdGenerator() |
269
|
|
|
{ |
270
|
|
|
return $this->idGenerator; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param IdGeneratorInterface $idGenerator |
275
|
|
|
*/ |
276
|
|
|
public function setIdGenerator($idGenerator = null) |
277
|
|
|
{ |
278
|
|
|
$this->idGenerator = $idGenerator; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|