public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $key = $this->getCacheKey($request); if (!empty($key)) { $item = $this->cache->getItem($key); if ($item->isHit()) { // serve cache response $resp = $item->get(); $response->setHeaders($resp['headers']); $response->getBody()->write($resp['body']); } else { $filterChain->handle($request, $response); // save response $resp = array('headers' => $this->getCacheHeaders($response), 'body' => Util::toString($response->getBody())); $item->set($resp, $this->ttl); $this->cache->save($item); } } else { // if we have no key we can not use a cache $filterChain->handle($request, $response); } }