1 | <?php |
||
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) |
||
175 | |||
176 | /** |
||
177 | * @param array $tags |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | private function prefixTags(array $tags) |
||
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) |
||
205 | |||
206 | /** |
||
207 | * @return StorageInterface |
||
208 | */ |
||
209 | public function getCacheStorage() |
||
213 | |||
214 | /** |
||
215 | * @param StorageInterface $cacheStorage |
||
216 | * @return self |
||
217 | */ |
||
218 | public function setCacheStorage($cacheStorage) |
||
224 | |||
225 | /** |
||
226 | * @return ModuleOptions |
||
227 | */ |
||
228 | public function getOptions() |
||
232 | |||
233 | /** |
||
234 | * @param ModuleOptions $options |
||
235 | * @return self |
||
236 | */ |
||
237 | public function setOptions($options) |
||
243 | |||
244 | /** |
||
245 | * Inject an EventManager instance |
||
246 | * |
||
247 | * @param EventManagerInterface $eventManager |
||
248 | * @return self |
||
249 | */ |
||
250 | public function setEventManager(EventManagerInterface $eventManager) |
||
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() |
||
274 | |||
275 | /** |
||
276 | * @return IdGeneratorInterface |
||
277 | */ |
||
278 | public function getIdGenerator() |
||
282 | |||
283 | /** |
||
284 | * @param IdGeneratorInterface $idGenerator |
||
285 | */ |
||
286 | public function setIdGenerator($idGenerator = null) |
||
290 | } |
||
291 |