From aca3f3771f92352e38811ba6ba7e29ccbb62c172 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 1 Sep 2023 19:27:03 +0200 Subject: [PATCH] wip #6237 @1 --- app/Fluidbook/Compiler/Cache.php | 80 ++++++++++++++++++++++++++ app/Fluidbook/Compiler/Compiler.php | 11 +++- app/Fluidbook/Compiler/Images.php | 89 +++++++++++++++++++---------- 3 files changed, 146 insertions(+), 34 deletions(-) create mode 100644 app/Fluidbook/Compiler/Cache.php diff --git a/app/Fluidbook/Compiler/Cache.php b/app/Fluidbook/Compiler/Cache.php new file mode 100644 index 000000000..f877e8492 --- /dev/null +++ b/app/Fluidbook/Compiler/Cache.php @@ -0,0 +1,80 @@ +_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; + } + + +} diff --git a/app/Fluidbook/Compiler/Compiler.php b/app/Fluidbook/Compiler/Compiler.php index f57b88251..95f2736bc 100644 --- a/app/Fluidbook/Compiler/Compiler.php +++ b/app/Fluidbook/Compiler/Compiler.php @@ -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() diff --git a/app/Fluidbook/Compiler/Images.php b/app/Fluidbook/Compiler/Images.php index 9340d7e5c..f76c7302c 100644 --- a/app/Fluidbook/Compiler/Images.php +++ b/app/Fluidbook/Compiler/Images.php @@ -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) -- 2.39.5