1 | <?php |
||
16 | class CacheListener implements ListenerAggregateInterface |
||
17 | { |
||
18 | use ListenerAggregateTrait; |
||
19 | |||
20 | /** |
||
21 | * Cache instance. |
||
22 | * |
||
23 | * @var StorageInterface |
||
24 | */ |
||
25 | protected $cache; |
||
26 | /** |
||
27 | * Cacheable actions. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $cacheableActions = []; |
||
32 | |||
33 | /** |
||
34 | * CacheListener constructor. |
||
35 | * |
||
36 | * @param StorageInterface $cache Cache to use |
||
37 | * @param array $cacheableActions Actions to cache |
||
38 | */ |
||
39 | 3 | public function __construct(StorageInterface $cache, $cacheableActions = []) |
|
44 | |||
45 | /** |
||
46 | * Return the cache storage. |
||
47 | * |
||
48 | * @return StorageInterface |
||
49 | */ |
||
50 | 1 | public function getCache() |
|
54 | |||
55 | /** |
||
56 | * Set the cache storage to use. |
||
57 | * |
||
58 | * @param StorageInterface $cache Cache storage to use |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | 3 | public function setCache(StorageInterface $cache) |
|
68 | |||
69 | /** |
||
70 | * Return the action classes to cache. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 1 | public function getCacheableActions() |
|
78 | |||
79 | /** |
||
80 | * Set the actions to cache. |
||
81 | * |
||
82 | * @param array $cacheableActions Actions to cache |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | 3 | public function setCacheableActions(array $cacheableActions) |
|
92 | |||
93 | /** |
||
94 | * Attach one or more listeners. |
||
95 | * |
||
96 | * Implementors may add an optional $priority argument; the EventManager |
||
97 | * implementation will pass this to the aggregate. |
||
98 | * |
||
99 | * @param EventManagerInterface $events The event manager |
||
100 | * @param int $priority |
||
101 | */ |
||
102 | 1 | public function attach(EventManagerInterface $events, $priority = 1) |
|
107 | |||
108 | /** |
||
109 | * Triggered on sendAction.pre. |
||
110 | * |
||
111 | * @param EventInterface $event Triggered event |
||
112 | * |
||
113 | * @throws \Zend\Cache\Exception\ExceptionInterface |
||
114 | * |
||
115 | * @return void|\PAMI\Message\Response\ResponseMessage |
||
116 | */ |
||
117 | 2 | public function onSendPre(EventInterface $event) |
|
135 | |||
136 | /** |
||
137 | * Triggered on sendAction.post. |
||
138 | * |
||
139 | * @param EventInterface $event Triggered event |
||
140 | */ |
||
141 | 2 | public function onSendPost(EventInterface $event) |
|
160 | |||
161 | /** |
||
162 | * Return true if we can cache the action. |
||
163 | * |
||
164 | * @param OutgoingMessage $action Requested action |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 2 | protected function isActionCacheable(OutgoingMessage $action) |
|
174 | |||
175 | /** |
||
176 | * Generate a cache ID based on action keys. |
||
177 | * |
||
178 | * @param OutgoingMessage $action The action |
||
179 | * @param string $prefix Cache ID prefix |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | 1 | protected function generateCacheId(OutgoingMessage $action, $prefix = '') |
|
195 | } |
||
196 |