[icinga-checkins] icinga.org: icingaweb2/feature/translation-catalog-statistics-object-13013 : Catalog: Utilize our file cache to help in terms of performance

git at icinga.org git at icinga.org
Mon Dec 12 13:06:19 CET 2016


Module: icingaweb2
Branch: feature/translation-catalog-statistics-object-13013
Commit: 02f8558660bbe431a0a4250ab1ca83e1efe11bdd
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=02f8558660bbe431a0a4250ab1ca83e1efe11bdd

Author: Johannes Meyer <johannes.meyer at netways.de>
Date:   Mon Dec 12 13:00:22 2016 +0100

Catalog: Utilize our file cache to help in terms of performance

refs #13013

---

 .../library/Translation/Catalog/Catalog.php        |   52 +++++++++++++++++++-
 1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/modules/translation/library/Translation/Catalog/Catalog.php b/modules/translation/library/Translation/Catalog/Catalog.php
index 39bdca4..177c2e8 100644
--- a/modules/translation/library/Translation/Catalog/Catalog.php
+++ b/modules/translation/library/Translation/Catalog/Catalog.php
@@ -9,6 +9,7 @@ use Exception;
 use IteratorAggregate;
 use Icinga\Application\Benchmark;
 use Icinga\Exception\IcingaException;
+use Icinga\Web\FileCache;
 use Icinga\Module\Translation\Exception\CatalogEntryException;
 use Icinga\Module\Translation\Exception\CatalogException;
 use Icinga\Module\Translation\Exception\CatalogHeaderException;
@@ -89,17 +90,26 @@ class Catalog implements IteratorAggregate
      * Create and return a new Catalog from the given path
      *
      * @param   string  $catalogPath
+     * @param   bool    $cache          Whether to utilize the cache or not
      *
      * @return  Catalog
      *
      * @throws  CatalogException
      */
-    public static function fromPath($catalogPath)
+    public static function fromPath($catalogPath, $cache = false)
     {
         Benchmark::measure('Catalog::fromPath()');
 
+        if ($cache) {
+            $catalog = static::fromCache($catalogPath);
+            if ($catalog !== null) {
+                return $catalog;
+            }
+        }
+
         try {
-            return Catalog::fromArray(CatalogParser::parsePath($catalogPath));
+            $entries = CatalogParser::parsePath($catalogPath);
+            $catalog = Catalog::fromArray($entries);
         } catch (CatalogHeaderException $e) {
             throw new CatalogException(
                 'An exception occurred while reading "' . $catalogPath . '": ' . $e->getMessage()
@@ -109,6 +119,44 @@ class Catalog implements IteratorAggregate
                 'Invalid entry #' . $e->getEntryNumber() . ' in "' . $catalogPath . '": ' . $e->getMessage()
             );
         }
+
+        if ($cache) {
+            static::cacheEntries($catalogPath, $entries);
+        }
+
+        return $catalog;
+    }
+
+    /**
+     * Attempt to create and return a new Catalog from the given cached path
+     *
+     * @param   string  $catalogPath
+     *
+     * @return  Catalog|null            Null in case the path has not been cached yet
+     */
+    protected static function fromCache($catalogPath)
+    {
+        Benchmark::measure('Catalog::fromCache()');
+
+        $eTag = FileCache::etagForFiles($catalogPath);
+        $cacheFile = 'translation-' . $eTag . '.po.json';
+        $cache = FileCache::instance();
+        if ($cache->has($cacheFile)) {
+            return static::fromArray(json_decode($cache->get($cacheFile), true));
+        }
+    }
+
+    /**
+     * Cache the given catalog entries
+     *
+     * @param   string  $catalogPath    The path to the po file which has initially been parsed
+     * @param   array   $entries        The entries as produced by CatalogParser::parsePath()
+     */
+    protected static function cacheEntries($catalogPath, array $entries)
+    {
+        $eTag = FileCache::etagForFiles($catalogPath);
+        $cacheFile = 'translation-' . $eTag . '.po.json';
+        FileCache::instance()->store($cacheFile, json_encode($entries));
     }
 
     /**



More information about the icinga-checkins mailing list