Эх сурвалжийг харах

[add][Closes #1] Cache some bacons

isundil 9 жил өмнө
parent
commit
992eafbdbc

+ 1 - 1
data/.gitignore

@@ -1 +1 @@
-*
+/*

+ 6 - 10
src/CacheController.php

@@ -26,13 +26,11 @@ class CacheController
 
     public static function getCacheIndex()
     {
-        echo "CC: get CI\n";
         if (self::$cacheIndex === null)
         {
             self::check();
             if (self::$cacheIndex->isInvalid())
             {
-                echo "CC: rebuild CI\n";
                 self::$cacheIndex = CacheIndex::rebuild();
                 foreach (self::$cacheIndex->getData() as $yymmdd)
                     CacheFlammenkuchenManager::setExist($yymmdd);
@@ -44,20 +42,18 @@ class CacheController
 
     protected static function wipe()
     {
-        echo "CC: wipe\n";
+        apcu_clear_cache();
+        self::refresh();
+    }
+
+    protected static function refresh()
+    {
         self::$cacheVersion = CacheVersion::rebuild();
         self::$cacheIndex = CacheIndex::rebuild();
         apcu_store("CACHEINDEX", self::$cacheIndex->getData());
         apcu_store("CACHEVERSION", self::$cacheVersion->getData());
         foreach (self::$cacheIndex->getData() as $yymmdd)
             CacheFlammenkuchenManager::setExist($yymmdd);
-        // TODO
-    }
-
-    protected static function refresh()
-    {
-        echo "CC: refresh\n";
-        // TODO
     }
 }
 

+ 64 - 0
src/CacheFlammenkuchen.php

@@ -0,0 +1,64 @@
+<?php
+
+require_once("Flammenkuchen.php");
+
+class CacheFlammenkuchen
+{
+    private $yymmdd;
+    private $title;
+    private $descr;
+    private $img_md5;
+
+    private function __construct()
+    {
+    }
+
+    public static function fromCached($yymmdd, $data)
+    {
+        $flamm = new CacheFlammenkuchen();
+        $flamm->yymmdd = $yymmdd;
+        $flamm->title = $data["title"];
+        $flamm->descr = $data["descr"];
+        $flamm->img_md5 = $data["imgsum"];
+        return $flamm;
+    }
+
+    public static function load($yymmdd)
+    {
+        $flamm = new CacheFlammenkuchen();
+        $data = json_decode(file_get_contents(DATA_DIR."/{$yymmdd}.json"));
+        $flammenkuchenModel = Flammenkuchen::fromJson($data);
+        $flamm->yymmdd = $yymmdd;
+        $flamm->title = $flammenkuchenModel->getTitle();
+        $flamm->descr = $flammenkuchenModel->getDescription();
+        $flamm->img_md5 = $flammenkuchenModel->imageMd5();
+        return $flamm;
+    }
+
+    public function toCached()
+    {
+        return array(
+            "title" => $this->title,
+            "descr" => $this->descr,
+            "imgsum" => $this->img_md5
+        );
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function getDescription()
+    {
+        return $this->descr;
+    }
+
+    public function getImageJpeg()
+    {
+        $data = json_decode(file_get_contents(DATA_DIR."/{$this->yymmdd}.json"));
+        $img = imagecreatefromstring(base64_decode($data->img));
+        return $img;
+    }
+}
+

+ 16 - 2
src/CacheFlammenkuchenManager.php

@@ -1,16 +1,30 @@
 <?php
 
+require_once("CacheFlammenkuchen.php");
+
 class CacheFlammenkuchenManager
 {
     public static function setExist($yymmdd)
     {
-        echo "CF: set {$yymmdd} to be lazilly created\n";
         apcu_add("FLAM_" .$yymmdd, true); // lazy-cache
     }
 
     public static function get($yymmdd)
     {
-        return apcu_fetch("FLAM_".$yymmdd);
+        $cached = apcu_fetch("FLAM_".$yymmdd);
+        if ($cached === false)
+            return false;
+        else if ($cached !== true)
+            return CacheFlammenkuchen::fromCached($yymmdd, $cached);
+        // Lazy-load data
+        $cacheFlamm = CacheFlammenkuchen::load($yymmdd);
+        apcu_store("FLAM_{$yymmdd}", $cacheFlamm->toCached());
+        return $cacheFlamm;
+    }
+
+    public static function exists($yymmdd)
+    {
+        return apcu_fetch("FLAM_".$yymmdd) !== false;
     }
 }
 

+ 0 - 1
src/CacheIndex.php

@@ -11,7 +11,6 @@ class CacheIndex
 
     public static function rebuild()
     {
-        echo "CI: rebuild\n";
         $data = array();
         $datadir = opendir(DATA_DIR);
         while (($file = readdir($datadir)) !== false)

+ 1 - 1
src/CacheVersion.php

@@ -26,7 +26,7 @@ class CacheVersion
 
     public function needWipe()
     {
-        return $this->data !== DATA_VERSION;
+        return $this->data !== DATA_VERSION || true;
     }
 
     public function needRefresh()

+ 6 - 25
src/Poster.php

@@ -1,6 +1,7 @@
 <?php
 
 require_once ("Flammenkuchen.php");
+require_once ("CacheController.php");
 
 class Poster
 {
@@ -28,31 +29,11 @@ class Poster
 
     private function getNextAvailableDay()
     {
-        //TODO use cache
-        $max = (int) (new \Datetime())->format("ymd");
-        $datadir = opendir("../data/");
-        while (($file = readdir($datadir)) !== false)
-        {
-            if (is_dir("../data/".$file)
-                || strlen($file) != 11
-                || substr($file, 6) !== ".json")
-                continue;
-
-            $current = (int) substr($file, 0, 6);
-
-            if ($current > $max)
-                $max = $current;
-        }
-        closedir($datadir);
-
-        $max = (string) $max;
-        $year = substr($max, 0, 2);
-        $month = substr($max, 2, 2);
-        $day = substr($max, 4, 2);
-        $date = new \DateTime();
-        $date->setDate($year +2000, $month, $day);
-        $date->add(new \DateInterval("P1D"));
-        return ($date->format("ymd"));
+        $now = (int) ((new DateTime())->format("ymd"));
+        $cacheData = CacheController::getCacheIndex()->getData();
+        if (count($cacheData) == 0)
+            return $now +1;
+        return (int)($cacheData[count($cacheData) -1]) +1;
     }
 }
 

+ 18 - 26
src/Router.php

@@ -1,6 +1,7 @@
 <?php
 
 require_once("Flammenkuchen.php");
+require_once("CacheController.php");
 
 class Router
 {
@@ -53,39 +54,25 @@ class Router
         $now = (int) (new \Datetime())->format("ymd");
         if ($yymmdd != $this->LATEST)
         {
-            // TODO use index cache
             if ((int) $yymmdd > $now) // Anti spoil
                 return null;
-            if (file_exists("../data/${yymmdd}.json"))
-                return $yymmdd;
-            return null;
+            return (CacheFlammenkuchenManager::exists($yymmdd))
+                ? $yymmdd : null;
         }
         else
         {
-            $min = 0;
+            $cacheIndexData = CacheController::getCacheIndex()->getData();
+            $last = null;
 
-            // obvious today-is-the-day
-            if ($this->checkFileExists($now))
-                return $now;
-
-            //TODO use index cache
-            $datadir = opendir("../data/");
-            while (($file = readdir($datadir)) !== false)
+            foreach ($cacheIndexData as $currentYymmdd)
             {
-                if (is_dir("../data/".$file)
-                    || strlen($file) != 11
-                    || substr($file, 6) !== ".json")
-                    continue;
-
-                $current = (int) substr($file, 0, 6);
-
-                if ($current > $now)
-                    continue;
-                if ($now - $current < $now - $min)
-                    $min = $current;
+                if ($currentYymmdd <= $now)
+                    $last = $currentYymmdd;
+                else
+                    break;
             }
-            closedir($datadir);
-            return $min === 0 ? null : $min;
+
+            return $last;
         }
     }
 
@@ -124,7 +111,12 @@ class Router
     {
         $req = $this->requestingDate;
 
-        return Flammenkuchen::fromJson(json_decode(file_get_contents("../data/${req}.json")));
+        return CacheFlammenkuchenManager::get($req);
+    }
+
+    public function getRequestYymmdd()
+    {
+        return $this->requestingDate;
     }
 }
 

+ 12 - 1
src/templates/bonjour.php

@@ -1,6 +1,17 @@
 <?php include ("header.php"); ?>
 <title>Bonjour Flammenkuchen !</title></head><body>
 <?php var_dump($data); ?>
-<?php var_dump($router); ?>
+<?php
+$yymmddays = CacheController::getCacheIndex()->getData();
+$currentYymmdd = $router->getRequestYymmdd();
+$currentYymmddIndex = array_search($currentYymmdd, $yymmddays);
+$prevYymmdd = $currentYymmddIndex == 0 ? null : $yymmddays[$currentYymmddIndex -1];
+$nextYymmdd = $currentYymmddIndex == count($yymmddays) -1 ? null : $yymmddays[$currentYymmddIndex +1];
+if ($nextYymmdd > (int) ((new DateTime())->format("ymd")))
+    $nextYymmdd = null;
+
+var_dump(array($prevYymmdd, $currentYymmdd, $nextYymmdd));
+?>
+
 <img src="<?php echo $router->getImageForRequest(); ?>" alt="<?php echo $data->getTitle(); ?>" />
 </body></html>