]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add caching
authorRoeland Jago Douma <roeland@famdouma.nl>
Wed, 22 Mar 2017 14:20:29 +0000 (15:20 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Fri, 24 Mar 2017 09:58:11 +0000 (10:58 +0100)
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
lib/private/Template/JSCombiner.php
lib/private/TemplateLayout.php

index 10560c8edf47e5d5e12b393ab4ef87ac98f0f00e..f470ee330c7984e02853f16c8b545149df5e283c 100644 (file)
@@ -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;
index 064e590b20a1d5c090a5b0cabfc10e08e44933fb..d172da1184b4b10c551be3acc4236d11a5a901f5 100644 (file)
@@ -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);