update to PHP-8.1
[m6w6/seekat] / lib / API / Call / Cache.php
index 3947259b51d5a09ef57a7157599bc3c6d50f3e9c..1b7e327e84c5f242506e831c0f6d2c4488b82b3d 100644 (file)
@@ -4,22 +4,17 @@ namespace seekat\API\Call;
 
 use http\Client\Request;
 use http\Client\Response;
+use Psr\Log\LoggerInterface;
 use seekat\API\Call\Cache\Control;
 use seekat\API\Call\Cache\Service;
 use seekat\API\Call\Cache\Service\Hollow;
 
 
 final class Cache {
-       /**
-        * @var Service
-        */
-       private $cache;
-
-       /**
-        * @param Service $cache
-        */
-       public function __construct(Service $cache = null) {
-               $this->cache = $cache ?? new Hollow;
+       public function __construct(
+               private readonly LoggerInterface $logger,
+               private readonly Service $cache = new Hollow
+       ) {
        }
 
        /**
@@ -31,15 +26,18 @@ final class Cache {
        public function save(Request $request, Response $response) : bool {
                $ctl = new Control($request);
                if (!$ctl->isValid()) {
+                       $this->logger->warning("cache -> save = invalid key", compact("ctl"));
                        return false;
                }
 
                $time = time();
                if ($time - 1 <= $response->getHeader("X-Cache-Time")) {
+                       $this->logger->info("cache -> save = no", compact("time"));
                        return true;
                }
                $response->setHeader("X-Cache-Time", $time);
 
+               $this->logger->info("cache -> save = yes", compact("time"));
                return $this->cache->store($ctl->getKey(), $response);
        }
 
@@ -52,10 +50,12 @@ final class Cache {
        public function load(Request $request, Response &$response = null) : bool {
                $ctl = new Control($request);
                if (!$ctl->isValid()) {
+                       $this->logger->warning("cache -> load = invalid key", compact("ctl"));
                        return false;
                }
 
                if (!$this->cache->fetch($ctl->getKey(), $response)) {
+                       $this->logger->info("cache -> load = no");
                        return false;
                }
                if ($ctl->isStale($response)) {
@@ -65,9 +65,11 @@ final class Cache {
                        if (($etag = $response->getHeader("ETag"))) {
                                $request->setOptions(["etag" => $etag]);
                        }
+                       $this->logger->info("cache -> load = stale");
                        return false;
                }
                $response->setHeader("X-Cache-Hit", $response->getHeader("X-Cache-Hit") + 1);
+               $this->logger->info("cache -> load = yes");
                return true;
        }
 
@@ -80,24 +82,30 @@ final class Cache {
        public function update(Request $request, Response &$response) : bool {
                $ctl = new Control($request);
                if (!$ctl->isValid()) {
+                       $this->logger->warning("cache -> update = invalid key", compact("ctl"));
                        return false;
                }
 
                if ($response->getResponseCode() !== 304) {
+                       $this->logger->info("cache -> update = yes");
                        return $this->save($request, $response);
                }
 
                /** @var Response $cached */
                if (!$this->cache->fetch($ctl->getKey(), $cached)) {
+                       $this->logger->info("cache -> update = yes; no-exist");
                        return $this->save($request, $response);
                }
 
                if ($response->getHeader("ETag") !== $cached->getHeader("ETag")) {
+                       $this->logger->info("cache -> update = yes; etag");
                        return $this->save($request, $response);
                }
 
-               $cached->setHeader("X-Cache-Update", $cached->getHeader("X-Cache-Update") + 1);
                $response = $cached;
-               return true;
+               $response->setHeader("X-Cache-Update", $cached->getHeader("X-Cache-Update") + 1);
+
+               $this->logger->info("cache -> update = yes; touch");
+               return $this->save($request, $response);
        }
 }