]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6237 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Sep 2023 17:27:03 +0000 (19:27 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 1 Sep 2023 17:27:03 +0000 (19:27 +0200)
app/Fluidbook/Compiler/Cache.php [new file with mode: 0644]
app/Fluidbook/Compiler/Compiler.php
app/Fluidbook/Compiler/Images.php

diff --git a/app/Fluidbook/Compiler/Cache.php b/app/Fluidbook/Compiler/Cache.php
new file mode 100644 (file)
index 0000000..f877e84
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+
+namespace App\Fluidbook\Compiler;
+
+trait Cache
+{
+    protected $_compiledCacheDate = null;
+    protected $_compositionCached = null;
+    protected $_compositionTimestamp = null;
+
+
+    /**
+     * @return bool
+     * @throws \Exception
+     */
+    public function compositionCached()
+    {
+        if (null === $this->_compositionCached) {
+            $compiled = $this->getCompiledCacheDate();
+            if ($compiled === 0) {
+                $this->_compositionCached = false;
+            } else {
+
+                $this->_compositionCached = $this->getCompositionTimestamp() <= $compiled;
+            }
+        }
+        return $this->_compositionCached;
+    }
+
+    public function getCompositionTimestamp()
+    {
+        if (null === $this->_compositionTimestamp) {
+            $this->_compositionTimestamp = (new \DateTime($this->getFluidbook()->composition_updated_at))->getTimestamp();
+        }
+        return $this->_compositionTimestamp;
+    }
+
+    public function setCompiledCacheDate()
+    {
+        \Illuminate\Support\Facades\Cache::set('compiler_cached_' . $this->getFluidbook()->id, time(), 7200);
+    }
+
+    protected function getCompiledCacheDate()
+    {
+        if (null === $this->_compiledCacheDate) {
+            $this->_compiledCacheDate = \Illuminate\Support\Facades\Cache::get('compiler_cached_' . $this->getFluidbook()->id, 0);
+        }
+        return $this->_compiledCacheDate;
+    }
+
+    /**
+     * @param $key
+     * @return bool
+     * @throws \Exception
+     */
+    public function isCached($key)
+    {
+        if (!$this->compositionCached()) {
+            return false;
+        }
+        return \Illuminate\Support\Facades\Cache::has($this->_cacheKey($key));
+    }
+
+    public function getCacheValue($key)
+    {
+        return \Illuminate\Support\Facades\Cache::get($this->_cacheKey($key));
+    }
+
+    public function setCacheValue($key, $value)
+    {
+        return \Illuminate\Support\Facades\Cache::set($this->_cacheKey($key), $value, 86400);
+    }
+
+    protected function _cacheKey($key)
+    {
+        return 'compiler_cached_' . $this->getFluidbook()->id . '_' . $this->getCompositionTimestamp() . '_' . $key;
+    }
+
+
+}
index f57b88251ca7e4c7a54df1a76e0ee6676c49d2de..95f2736bc492e5e1fb4367aadb19c58b7e5007fc 100644 (file)
@@ -52,6 +52,7 @@ class Compiler extends Base implements CompilerInterface
     use Images;
     use Sound;
     use Articles;
+    use Cache;
 
     protected static $uaPrefixes = array('-moz-', '-webkit-', '-o-', '-ms-', '');
 
@@ -591,7 +592,7 @@ class Compiler extends Base implements CompilerInterface
             $this->logtime = $currenttime;
         }
         $time = $currenttime - $this->logtime;
-        $log = $step . ' | ' . round($time, 3) . 's' . "\n";
+        $log = $step . ' | ' . round($time, 4) . 's' . "\n";
         fwrite($this->logfp, $log);
         fflush($this->logfp);
         $this->logtime = $currenttime;
@@ -675,8 +676,10 @@ class Compiler extends Base implements CompilerInterface
 
     public function handle()
     {
-        $this->log('Preprocess images');
-        (new FluidbookImagesPreprocess($this->book_id))->handle();
+        if (!$this->compositionCached()) {
+            $this->log('Preprocess images');
+            (new FluidbookImagesPreprocess($this->book_id))->handle();
+        }
         $this->log('Start compile process');
 
         // Raw copy of some directories
@@ -740,6 +743,8 @@ class Compiler extends Base implements CompilerInterface
         $this->log('Files Synced');
 //        $f=rtrim(str_replace('/html5/', '/compiletime/', $this->dir));
 //        touch($f);
+
+        $this->setCompiledCacheDate();
     }
 
     protected function writeSlider()
index 9340d7e5c8ba4cba8d10c362e2a4a8d6f3c20d92..f76c7302c8604c274848146d9c8f898a7521f027 100644 (file)
@@ -11,6 +11,9 @@ trait Images
 
     protected function writeImages()
     {
+        $compositionCached = $this->compositionCached();
+
+
         set_time_limit(0);
         switch ($this->fluidbookSettings->mobileVersion) {
             case 'html5-desktop':
@@ -27,48 +30,72 @@ trait Images
                 break;
         }
 
-        $rasterizePages = $this->config->rasterizePages;
-        $this->config->pagesDimensions = [];
 
-        if ($this->fluidbookSettings->mobileNavigationType === 'mobilefirst') {
-            $imdir = 'mf';
-        } else {
-            $imdir = 'html';
-        }
+        if (!$this->isCached('imagesToCopy')) {
+            $filesToCopy = [];
+
+            $rasterizePages = $this->config->rasterizePages;
+
+            if ($this->fluidbookSettings->mobileNavigationType === 'mobilefirst') {
+                $imdir = 'mf';
+            } else {
+                $imdir = 'html';
+            }
+
+            $thumbs = array();
+            foreach ($this->pages as $page => $infos) {
+                $thisrasterize = in_array($page, $rasterizePages);
+                $thisimagesvg = !$thisrasterize && $this->svg;
+                $thisbackgroundPrefix = $thisrasterize ? [true] : $this->backgroundsPrefix;
+
+                foreach ($this->getResolutions() as $r) {
+                    foreach ($thisbackgroundPrefix as $backgroundsPrefix) {
+                        $source = $this->getFluidbook()->getFile($page, $this->imageFormat, $r, $backgroundsPrefix, true, $imdir);
 
-        $thumbs = array();
-        foreach ($this->pages as $page => $infos) {
-            $thisrasterize = in_array($page, $rasterizePages);
-            $thisimagesvg = !$thisrasterize && $this->svg;
-            $thisbackgroundPrefix = $thisrasterize ? [true] : $this->backgroundsPrefix;
-
-            foreach ($this->getResolutions() as $r) {
-                foreach ($thisbackgroundPrefix as $backgroundsPrefix) {
-                    $source = $this->getFluidbook()->getFile($page, $this->imageFormat, $r, $backgroundsPrefix, true, $imdir);
-                    if ($r === $this->maxRes) {
-                        $this->getPageDimension($page);
+                        $filesToCopy[$source] = 'data/background/' . $r . '/' . ($backgroundsPrefix ? 't' : 'p') . $page . '.' . $this->imageFormat;
                     }
-                    $this->vdir->copy($source, 'data/background/' . $r . '/' . ($backgroundsPrefix ? 't' : 'p') . $page . '.' . $this->imageFormat);
                 }
-            }
 
-            if ($thisimagesvg) {
-                $this->vdir->copy(
-                    $this->getFluidbook()->getFile($page, 'svg', 150, true,
-                        in_array($page, $this->config->vectorPages), 'html')
-                    , 'data/contents/p' . $page . '.svg');
+                if ($thisimagesvg) {
+                    $source = $this->getFluidbook()->getFile($page, 'svg', 150, true,
+                        in_array($page, $this->config->vectorPages), 'html');
+                    $filesToCopy[$source] = 'data/contents/p' . $page . '.svg';
+                }
+
+                $t = $this->getFluidbook()->getThumbFile($page, $this->imageFormat);
+                $filesToCopy[$t] = 'data/thumbnails/p' . $page . '.' . $this->imageFormat;
+                $this->log('Made image page ' . $page);
             }
 
-            $t = $this->getFluidbook()->getThumbFile($page, $this->imageFormat);
-            $this->vdir->copy($t, 'data/thumbnails/p' . $page . '.' . $this->imageFormat);
-            $this->log('Made image page ' . $page);
-        }
 
+            $this->_makeCover($this->getFluidbook()->getFile(1, 'jpg', 150, true, true));
+            $this->log('Made cover for apps');
+            $this->setCacheValue('imagesToCopy', $filesToCopy);
+        } else {
+            $filesToCopy = $this->getCacheValue('imagesToCopy');
+        }
 
-        $this->_makeCover($this->getFluidbook()->getFile(1, 'jpg', 150, true, true));
-        $this->log('Made cover for apps');
+        foreach ($filesToCopy as $source => $dest) {
+            $this->vdir->copy($source, $dest, false, false);
+        }
 
         $this->log('Made images');
+
+        $this->getPageDimensions();
+    }
+
+    protected function getPageDimensions()
+    {
+        if (!$this->isCached('pagesDimensions')) {
+            $this->config->pagesDimensions = [];
+            foreach ($this->pages as $page => $infos) {
+                $this->getPageDimension($page);
+            }
+            $this->setCacheValue('pagesDimensions', $this->config->pagesDimensions);
+        } else {
+            $this->config->pagesDimensions = $this->getCacheValue('pagesDimensions');
+        }
+        $this->log('Got page dimensions');
     }
 
     protected function _makeCover($orig)