add logging; fix caching
[pharext/pharext.org] / app / Github / API / Call.php
index f40e4866f6b837764b4b999798bca96453f13560..d4afd19eeb3c375408162728293622a2f2e2b12c 100644 (file)
@@ -3,6 +3,7 @@
 namespace app\Github\API;
 
 use app\Github\API;
+use app\Github\Storage\Item;
 use http\QueryString;
 use http\Url;
 use merry\Config;
@@ -57,17 +58,12 @@ abstract class Call
        }
        
        function __invoke(callable $callback) {
-               if (empty($this->args["fresh"]) && ($cache = $this->api->getCacheStorage())) {
-                       $key = $this->getCacheKey();
-                       
-                       if ($cache->get($key, $cached)) {
-                               call_user_func_array($callback, $cached);
-                               return $this->api->getClient();
-                       }
+               if ($this->readFromCache($cached)) {
+                       call_user_func_array($callback, $cached);
+               } else {
+                       $this->enqueue($callback);
                }
-               
-               $this->enqueue($callback);
-               return $this->api->getClient();
+               return $this;
        }
        
        /**
@@ -79,6 +75,13 @@ abstract class Call
                return strtolower(end($parts));
        }
        
+       /**
+        * Call Client::send()
+        */
+       function send() {
+               return $this->api->getClient()->send();
+       }
+       
        /**
         * Get associated cache storage
         * @param int $ttl out param of configure ttl
@@ -92,28 +95,54 @@ abstract class Call
        }
        
        function getCacheKey() {
-               return sprintf("github:%s:%s:%s", $this->api->getToken(), $this, 
-                       new QueryString($this->args));
+               $args = $this->args;
+               unset($args["fresh"]);
+               if (isset($args["page"]) && !strcmp($args["page"], "1")) {
+                       unset($args["page"]);
+               }
+               ksort($args);
+               return sprintf("%s:%s:%s", $this->api->getToken(), $this, 
+                       new QueryString($args));
        }
 
-       function readFromCache(&$cached = null, &$ttl = null) {
-               if (empty($this->args["fresh"]) && ($cache = $this->api->getCacheStorage())) {
-                       $key = $this->getCacheKey();
-                       return $cache->get($key, $cached, $ttl);
+       function readFromCache(array &$value = null) {
+               if (!empty($this->args["fresh"])) {
+                       return false;
                }
-               return false;
+               if (!($cache = $this->api->getCacheStorage())) {
+                       return false;
+               }
+               if (!strlen($key = $this->getCacheKey())) {
+                       return false;
+               }
+               if (!$cache->get($key, $cached)) {
+                       return false;
+               }
+               if (null !== $this->api->getMaxAge() && $cached->getAge() > $this->api->getMaxAge()) {
+                       return false;
+               }
+               $this->api->getLogger()->debug("Cache-Hit: $this", $this->args);
+               $value = $cached->getValue();
+               return true;
        }
        
-       function saveToCache($fresh) {
+       function saveToCache(array $fresh) {
                if (($cache = $this->api->getCacheStorage())) {
                        if (isset($this->config->storage->cache->{$this}->ttl)) {
                                $ttl = $this->config->storage->cache->{$this}->ttl;
                        } else {
-                               $ttl = 0;
+                               $ttl = null;
                        }
                        
                        $key = $this->getCacheKey();
-                       $cache->set($key, $fresh, $ttl);
+                       $cache->set($key, new Item($fresh, $ttl));
+               }
+       }
+       
+       function dropFromCache() {
+               if (($cache = $this->api->getCacheStorage())) {
+                       $key = $this->getCacheKey();
+                       $cache->del($key);
                }
        }
 }