caching and cache time configuration
[phancap.git] / src / phancap / Repository.php
index 2058bb6252606c2c0f75b069ddf6a3300205b944..157a0620b266fbe1a6ccdeec3c04a18e44388606 100644 (file)
@@ -13,7 +13,7 @@ class Repository
         $name = $this->getFilename($options);
         $img = new Image($name);
         $img->setConfig($this->config);
-        if (!$this->isAvailable($img)) {
+        if (!$this->isAvailable($img, $options)) {
             $this->render($img, $options);
         }
         return $img;
@@ -21,18 +21,34 @@ class Repository
 
     public function getFilename(Options $options)
     {
-        return parse_url($options->values['url'], PHP_URL_HOST)
-            . '-' . md5(\serialize($options->values))
-            . '.' . $options->values['sformat'];
+        $optValues = $options->values;
+        unset($optValues['smaxage']);
+        unset($optValues['atimestamp']);
+        unset($optValues['asignature']);
+        unset($optValues['atoken']);
+
+        return parse_url($optValues['url'], PHP_URL_HOST)
+            . '-' . md5(\serialize($optValues))
+            . '.' . $optValues['sformat'];
     }
 
-    public function isAvailable(Image $img)
+    /**
+     * Check if the image is available locally.
+     *
+     * @return boolean True if we have it and it's within the cache lifetime,
+     *                 false if the cache expired or the screenshot does not
+     *                 exist.
+     */
+    public function isAvailable(Image $img, Options $options)
     {
         $path = $img->getPath();
         if (!file_exists($path)) {
             return false;
         }
-        //FIXME: add cache lifetime check
+
+        if (filemtime($path) < time() - $options->values['smaxage']) {
+            return false;
+        }
 
         return true;
     }