diff --git a/webfiori/framework/middleware/CacheMiddleware.php b/webfiori/framework/middleware/CacheMiddleware.php index 7c12c2ef..8c206ba5 100644 --- a/webfiori/framework/middleware/CacheMiddleware.php +++ b/webfiori/framework/middleware/CacheMiddleware.php @@ -10,27 +10,27 @@ class CacheMiddleware extends AbstractMiddleware { - + private $fromCache; public function __construct() { parent::__construct('cache'); $this->setPriority(50); $this->addToGroups(['web', 'api']); - + $this->fromCache = false; } public function after(Request $request, Response $response) { - $uriObj = Router::getRouteUri(); + if (!$this->fromCache) { + $uriObj = Router::getRouteUri(); - if ($uriObj !== null) { - $key = $this->getKey(); - - Cache::set($key, function (Response $response) { - return [ + if ($uriObj !== null) { + $key = $this->getKey(); + $data = [ 'headers' => $response->getHeaders(), 'http-code' => $response->getCode(), 'body' => $response->getBody() ]; - }, $uriObj->getCacheDuration(), [$response]); + Cache::set($key, $data, $uriObj->getCacheDuration()); + } } } @@ -39,9 +39,11 @@ public function afterSend(Request $request, Response $response) { } public function before(Request $request, Response $response) { - $data = Cache::get($this->getKey()); + $key = $this->getKey(); + $data = Cache::get($key); if ($data !== null) { + $this->fromCache = true; $response->write($data['body']); $response->setCode($data['http-code']); foreach ($data['headers'] as $headerObj) {