simple cache for rendered files
authorChristian Weiske <cweiske@cweiske.de>
Tue, 7 Jul 2015 14:10:56 +0000 (16:10 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 7 Jul 2015 14:10:56 +0000 (16:10 +0200)
.gitignore
data/config.default.php
src/phorkie/File.php
src/phorkie/Renderer/Cache.php [new file with mode: 0644]
src/phorkie/Repository.php
src/phorkie/SetupCheck.php
www/setup.php

index 6235ed53285150ee2de07f77b112b8165254e486..f9dbcb1134d56223e58d8336d409dc5cb72bddac 100644 (file)
@@ -8,3 +8,4 @@
 /src/gen-rewritemap.php
 /www/*.phar
 /www/*.phar.bz2
+/data/cache/
index d603622f0cf4207279d177f4915fc2016ee65016..c062d025ac9efcf4502d501762a8386dd4b738d4 100644 (file)
@@ -15,6 +15,7 @@ $GLOBALS['phorkie']['cfg'] = array(
         'public'    => '%BASEURL%' . 'repos/git/',
         'private'   => null,
     ),
+    'cachedir'      => __DIR__ . '/cache/',
     'gitdir'        => $wwwDir . 'repos/git/',
     'workdir'       => $wwwDir . 'repos/work/',
     'tpl'           => __DIR__ . '/templates/',
index 2afda4c89fc8b2514ebef68a41d5ba3e1bc499aa..300e8106564c823d5a1d4d80c1928a83671de3c2 100644 (file)
@@ -84,22 +84,8 @@ class File
 
     public function getRenderedContent(Tool_Result $res = null)
     {
-        $ext   = $this->getExt();
-        $class = '\\phorkie\\Renderer_Unknown';
-
-        if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
-            $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
-        } else if ($this->isText()) {
-            $class = '\\phorkie\\Renderer_Geshi';
-        } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
-            $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
-            if (substr($type, 0, 6) == 'image/') {
-                $class = '\\phorkie\\Renderer_Image';
-            }
-        }
-
-        $rend = new $class();
-        return $rend->toHtml($this, $res);
+        $cache = new Renderer_Cache();
+        return $cache->toHtml($this, $res);
     }
 
     /**
diff --git a/src/phorkie/Renderer/Cache.php b/src/phorkie/Renderer/Cache.php
new file mode 100644 (file)
index 0000000..4137b05
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+namespace phorkie;
+
+class Renderer_Cache
+{
+    /**
+     * Converts the code to HTML by fetching it from cache,
+     * or by letting the other renderes generate it and then
+     * storing it in the cache.
+     *
+     * @param File        $file File to render
+     * @param Tool_Result $res  Tool result to integrate
+     *
+     * @return string HTML
+     */
+    public function toHtml(File $file, Tool_Result $res = null)
+    {
+        $html = null;
+        $cacheFile = $this->getCacheFile($file);
+        if ($res === null && $cacheFile !== null) {
+            $html = $this->loadHtmlFromCache($cacheFile);
+        }
+        if ($html === null) {
+            $html = $this->renderFile($file, $res);
+            if ($res === null && $cacheFile !== null) {
+                $this->storeHtmlIntoCache($cacheFile, $html);
+            }
+        }
+        return $html;
+    }
+
+    protected function renderFile(File $file, Tool_Result $res = null)
+    {
+        $ext   = $file->getExt();
+        $class = '\\phorkie\\Renderer_Unknown';
+
+        if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
+            $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
+        } else if ($file->isText()) {
+            $class = '\\phorkie\\Renderer_Geshi';
+        } else if (isset($GLOBALS['phorkie']['languages'][$ext]['mime'])) {
+            $type = $GLOBALS['phorkie']['languages'][$ext]['mime'];
+            if (substr($type, 0, 6) == 'image/') {
+                $class = '\\phorkie\\Renderer_Image';
+            }
+        }
+
+        $rend = new $class();
+        return $rend->toHtml($file, $res);
+    }
+
+    /**
+     * @return null|string NULL when there is no cache, string with HTML
+     *                     otherwise
+     */
+    protected function loadHtmlFromCache($cacheFile)
+    {
+        if (!file_exists($cacheFile)) {
+            return null;
+        }
+        return file_get_contents($cacheFile);
+    }
+
+    protected function storeHtmlIntoCache($cacheFile, $html)
+    {
+        file_put_contents($cacheFile, $html);
+    }
+
+    protected function getCacheFile(File $file)
+    {
+        if (!$GLOBALS['phorkie']['cfg']['cachedir']
+            || !is_dir($GLOBALS['phorkie']['cfg']['cachedir'])
+        ) {
+            return null;
+        }
+
+        return $GLOBALS['phorkie']['cfg']['cachedir']
+            . '/' . $file->repo->id
+            . '-' . $file->repo->hash
+            . '-' . str_replace('/', '-', $file->getFilename())
+            . '.html';
+    }
+}
+?>
index 8cf5ff62d2fff510a50b0ad0abc0fa8389e0bb45..62a6e38c4da8a30c6690381935693047b6606725 100644 (file)
@@ -96,7 +96,6 @@ class Repository
 
     public function loadHash()
     {
-        return;
         if ($this->hash !== null) {
             return;
         }
index 32fb79e2d2ef94aba344d2a53d04b80ccb9c23b7..9cae724698168e4adf200450db4580d4ca4083ba 100644 (file)
@@ -26,6 +26,7 @@ class SetupCheck
         $this->writableDirs = array(
             'gitdir'  => Tools::foldPath($cfg['gitdir']),
             'workdir' => Tools::foldPath($cfg['workdir']),
+            'cachedir' => Tools::foldPath($cfg['cachedir']),
         );
         $this->elasticsearch = $cfg['elasticsearch'];
     }
index 485c19fb01c1a3d1ab56ef6eec9e510392ec7af7..c7320d8e751769a65b712f31b255be7a0e580acc 100644 (file)
@@ -36,6 +36,7 @@ $out = <<<HTM
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
   <title>phorkie setup check</title>
+  <meta charset="utf-8" />
   <link rel="stylesheet" href="css/bootstrap.min.css"/>
   <link rel="stylesheet" href="css/font-awesome.css"/>
   <link rel="stylesheet" href="css/phorkie.css"/>