X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=inline;f=lib%2FAPI%2FCall%2FCache.php;h=3947259b51d5a09ef57a7157599bc3c6d50f3e9c;hb=2121556150be871684b5046af7cf250b8219128d;hp=b6d062c7a69810e0a09cd9229a1155b92add50f5;hpb=cac6bea94e6cde142c951566fa6387ffa54eb3cb;p=m6w6%2Fseekat diff --git a/lib/API/Call/Cache.php b/lib/API/Call/Cache.php index b6d062c..3947259 100644 --- a/lib/API/Call/Cache.php +++ b/lib/API/Call/Cache.php @@ -9,8 +9,7 @@ use seekat\API\Call\Cache\Service; use seekat\API\Call\Cache\Service\Hollow; -final class Cache -{ +final class Cache { /** * @var Service */ @@ -68,7 +67,37 @@ final class Cache } return false; } + $response->setHeader("X-Cache-Hit", $response->getHeader("X-Cache-Hit") + 1); return true; } + /** + * Update call data + * @param Request $request + * @param Response $response + * @return bool + */ + public function update(Request $request, Response &$response) : bool { + $ctl = new Control($request); + if (!$ctl->isValid()) { + return false; + } + + if ($response->getResponseCode() !== 304) { + return $this->save($request, $response); + } + + /** @var Response $cached */ + if (!$this->cache->fetch($ctl->getKey(), $cached)) { + return $this->save($request, $response); + } + + if ($response->getHeader("ETag") !== $cached->getHeader("ETag")) { + return $this->save($request, $response); + } + + $cached->setHeader("X-Cache-Update", $cached->getHeader("X-Cache-Update") + 1); + $response = $cached; + return true; + } }