cache = $cache; } /** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response. * * */ public function process(Request $request, RequestHandlerInterface $handler): Response { $cacheKey = $request->getUri()->getPath(); // If this QR code is already cached, just return it if ($this->cache->contains($cacheKey)) { $qrData = $this->cache->fetch($cacheKey); $response = new DiactResp(); $response->getBody()->write($qrData['body']); return $response->withHeader('Content-Type', $qrData['content-type']); } // If not, call the next middleware and cache it /** @var Response $resp */ $resp = $handler->handle($request); $this->cache->save($cacheKey, [ 'body' => $resp->getBody()->__toString(), 'content-type' => $resp->getHeaderLine('Content-Type'), ]); return $resp; } }