From 242f8964cf8e98726a4d6e51cfd5200b17211e3e Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 22 Mar 2017 15:20:29 +0100 Subject: [PATCH] Add caching Signed-off-by: Roeland Jago Douma --- lib/private/Template/JSCombiner.php | 48 +++++++++++++++++++++++++---- lib/private/TemplateLayout.php | 3 +- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/private/Template/JSCombiner.php b/lib/private/Template/JSCombiner.php index 10560c8edf4..f470ee330c7 100644 --- a/lib/private/Template/JSCombiner.php +++ b/lib/private/Template/JSCombiner.php @@ -22,6 +22,7 @@ */ namespace OC\Template; +use OCP\ICache; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; @@ -36,16 +37,22 @@ class JSCombiner { /** @var IURLGenerator */ protected $urlGenerator; + /** @var ICache */ + protected $depsCache; + /** * JSCombiner constructor. * * @param IAppData $appData * @param IURLGenerator $urlGenerator + * @param ICache $depsCache */ public function __construct(IAppData $appData, - IURLGenerator $urlGenerator) { + IURLGenerator $urlGenerator, + ICache $depsCache) { $this->appData = $appData; $this->urlGenerator = $urlGenerator; + $this->depsCache = $depsCache; } /** @@ -79,7 +86,26 @@ class JSCombiner { * @return bool */ protected function isCached($fileName, ISimpleFolder $folder) { - return false; + $fileName = str_replace('.json', '.js', $fileName) . '.deps'; + try { + $deps = $this->depsCache->get($folder->getName() . '-' . $fileName); + if ($deps === null) { + $depFile = $folder->getFile($fileName); + $deps = $depFile->getContent(); + $this->depsCache->set($folder->getName() . '-' . $fileName, $deps); + } + $deps = json_decode($deps, true); + + foreach ($deps as $file=>$mtime) { + if (!file_exists($file) || filemtime($file) > $mtime) { + return false; + } + } + + return true; + } catch(NotFoundException $e) { + return false; + } } /** @@ -89,17 +115,19 @@ class JSCombiner { * @return bool */ protected function cache($path, $fileName, ISimpleFolder $folder) { - $data = json_decode(file_get_contents($path . '/' . $fileName)); + $deps = []; + $fullPath = $path . '/' . $fileName; + $data = json_decode(file_get_contents($fullPath)); + $deps[$fullPath] = filemtime($fullPath); $res = ''; - $deps = []; foreach ($data as $file) { $filePath = $path . '/' . $file; if (is_file($filePath)) { - $res .= file_get_contents($path . '/' . $file); + $res .= file_get_contents($filePath); $res .= PHP_EOL . PHP_EOL; - $deps[$file] = filemtime($path . '/' . $file); + $deps[$filePath] = filemtime($filePath); } } @@ -110,8 +138,16 @@ class JSCombiner { $cachedfile = $folder->newFile($fileName); } + $depFileName = $fileName . '.deps'; + try { + $depFile = $folder->getFile($depFileName); + } catch (NotFoundException $e) { + $depFile = $folder->newFile($depFileName); + } + try { $cachedfile->putContent($res); + $depFile->putContent(json_encode($deps)); return true; } catch (NotPermittedException $e) { return false; diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index 064e590b20a..d172da1184b 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -253,7 +253,8 @@ class TemplateLayout extends \OC_Template { array( \OC::$SERVERROOT => \OC::$WEBROOT ), new JSCombiner( \OC::$server->getAppDataDir('js'), - \OC::$server->getURLGenerator() + \OC::$server->getURLGenerator(), + \OC::$server->getMemCacheFactory()->create('JS') ) ); $locator->find($scripts); -- 2.39.5