Ver Fonte

[add][refs #1] CacheVersion
[add] CacheIndex
[add][wip] CacheFlammenkuchen, actually always true

isundil há 9 anos atrás
pai
commit
8234e496d5
4 ficheiros alterados com 168 adições e 0 exclusões
  1. 63 0
      src/CacheController.php
  2. 16 0
      src/CacheFlammenkuchenManager.php
  3. 42 0
      src/CacheIndex.php
  4. 47 0
      src/CacheVersion.php

+ 63 - 0
src/CacheController.php

@@ -0,0 +1,63 @@
+<?php
+
+define("DATA_DIR", "../data/");
+define("DATA_VERSION", 1);
+
+require_once("CacheFlammenkuchenManager.php");
+require_once("CacheVersion.php");
+require_once("CacheIndex.php");
+
+class CacheController
+{
+    private static $cacheVersion = null;
+    private static $cacheIndex = null;
+
+    static function check()
+    {
+        if (self::$cacheVersion !== null)
+            return;
+        self::$cacheVersion = new CacheVersion(apcu_fetch("CACHEVERSION"));
+        self::$cacheIndex = new CacheIndex(apcu_fetch("CACHEINDEX"));
+        if (self::$cacheVersion->needWipe())
+            self::wipe();
+        else if (self::$cacheVersion->needRefresh())
+            self::refresh();
+    }
+
+    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);
+                apcu_store("CACHEINDEX", self::$cacheIndex->getData());
+            }
+        }
+        return self::$cacheIndex;
+    }
+
+    protected static function wipe()
+    {
+        echo "CC: wipe\n";
+        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
+    }
+}
+

+ 16 - 0
src/CacheFlammenkuchenManager.php

@@ -0,0 +1,16 @@
+<?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);
+    }
+}
+

+ 42 - 0
src/CacheIndex.php

@@ -0,0 +1,42 @@
+<?php
+
+class CacheIndex
+{
+    private $data;
+
+    public function __construct($data)
+    {
+        $this->data = $data;
+    }
+
+    public static function rebuild()
+    {
+        echo "CI: rebuild\n";
+        $data = array();
+        $datadir = opendir(DATA_DIR);
+        while (($file = readdir($datadir)) !== false)
+        {
+            if (is_dir(DATA_DIR.$file)
+                || strlen($file) != 11
+                || substr($file, 6) !== ".json")
+                continue;
+
+            $yymmdd = (int) substr($file, 0, 6);
+            $data[] = $yymmdd;
+        }
+        closedir($datadir);
+        sort($data);
+        return new self($data);
+    }
+
+    public function isInvalid()
+    {
+        return !is_array($this->data);
+    }
+
+    public function getData()
+    {
+        return $this->data;
+    }
+}
+

+ 47 - 0
src/CacheVersion.php

@@ -0,0 +1,47 @@
+<?php
+
+class CacheVersion
+{
+    private $index;
+    private $data;
+
+    public function __construct($cached)
+    {
+        if (!is_array($cached))
+            $cached = array(
+                "data" => -1
+                ,"index" => -1
+            );
+        $this->data = $cached["data"];
+        $this->index = $cached["index"];
+    }
+
+    public static function rebuild()
+    {
+        return new CacheVersion(array(
+            "index" => self::getYymmdd(),
+            "data"  => DATA_VERSION
+        ));
+    }
+
+    public function needWipe()
+    {
+        return $this->data !== DATA_VERSION;
+    }
+
+    public function needRefresh()
+    {
+        return $this->index == -1 || self::getYymmdd() !== $this->index;
+    }
+
+    public static function getYymmdd()
+    {
+        return (new \DateTime())->format("ymd");
+    }
+
+    public function getData()
+    {
+        return array("data" => $this->data, "index" => $this->index);
+    }
+}
+