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) |
||
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 | $cacheStorage->setTags($id, $this->getTags($mvcEvent)); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Determine if we should cache the current request |
||
116 | * |
||
117 | * @param MvcEvent $mvcEvent |
||
118 | * @return bool |
||
119 | */ |
||
120 | protected function shouldCacheRequest(MvcEvent $mvcEvent) |
||
134 | |||
135 | /** |
||
136 | * @param array $tags |
||
137 | * @param bool|null $disjunction |
||
138 | * @return bool |
||
139 | * @throws UnsupportedAdapterException |
||
140 | */ |
||
141 | public function clearByTags(array $tags = array(), $disjunction = null) |
||
154 | |||
155 | /** |
||
156 | * Cache tags to use for this page |
||
157 | * |
||
158 | * @param MvcEvent $event |
||
159 | * @return array |
||
160 | */ |
||
161 | public function getTags(MvcEvent $event) |
||
177 | |||
178 | /** |
||
179 | * @param string $eventName |
||
180 | * @param string $cacheKey |
||
181 | * @param MvcEvent|null $mvcEvent |
||
182 | * @return CacheEvent |
||
183 | */ |
||
184 | protected function createCacheEvent($eventName, MvcEvent $mvcEvent = null, $cacheKey = null) |
||
194 | |||
195 | /** |
||
196 | * @return StorageInterface |
||
197 | */ |
||
198 | public function getCacheStorage() |
||
202 | |||
203 | /** |
||
204 | * @param StorageInterface $cacheStorage |
||
205 | * @return self |
||
206 | */ |
||
207 | public function setCacheStorage($cacheStorage) |
||
213 | |||
214 | /** |
||
215 | * @return ModuleOptions |
||
216 | */ |
||
217 | public function getOptions() |
||
221 | |||
222 | /** |
||
223 | * @param ModuleOptions $options |
||
224 | * @return self |
||
225 | */ |
||
226 | public function setOptions($options) |
||
232 | |||
233 | /** |
||
234 | * Inject an EventManager instance |
||
235 | * |
||
236 | * @param EventManagerInterface $eventManager |
||
237 | * @return self |
||
238 | */ |
||
239 | public function setEventManager(EventManagerInterface $eventManager) |
||
247 | |||
248 | /** |
||
249 | * Retrieve the event manager |
||
250 | * |
||
251 | * Lazy-loads an EventManager instance if none registered. |
||
252 | * |
||
253 | * @return EventManagerInterface |
||
254 | */ |
||
255 | public function getEventManager() |
||
263 | |||
264 | /** |
||
265 | * @return IdGeneratorInterface |
||
266 | */ |
||
267 | public function getIdGenerator() |
||
271 | |||
272 | /** |
||
273 | * @param IdGeneratorInterface $idGenerator |
||
274 | */ |
||
275 | public function setIdGenerator($idGenerator = null) |
||
279 | } |
||
280 |