Skip to content

Commit

Permalink
Update CacheMiddleware.php
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Dec 10, 2024
1 parent 67a5ca1 commit 555fd4d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions webfiori/framework/middleware/CacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand All @@ -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) {
Expand Down

0 comments on commit 555fd4d

Please sign in to comment.