]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache SCSS dependency file in memcache
authorRoeland Jago Douma <roeland@famdouma.nl>
Tue, 21 Mar 2017 20:53:20 +0000 (21:53 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Wed, 22 Mar 2017 19:14:08 +0000 (20:14 +0100)
Since reading a file from disks can be costly. Lets store the dependency
json also in memcache.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
lib/private/Template/SCSSCacher.php
lib/private/TemplateLayout.php
tests/lib/Template/SCSSCacherTest.php

index 4f5c66f240958b34585a166d845f18be7abcafdf..c12d87715131fc034cc438ace6a4f39083671734 100644 (file)
@@ -30,6 +30,7 @@ use OCP\Files\IAppData;
 use OCP\Files\NotFoundException;
 use OCP\Files\NotPermittedException;
 use OCP\Files\SimpleFS\ISimpleFolder;
+use OCP\ICache;
 use OCP\IConfig;
 use OCP\ILogger;
 use OCP\IURLGenerator;
@@ -51,23 +52,29 @@ class SCSSCacher {
        /** @var string */
        protected $serverRoot;
 
+       /** @var ICache */
+       protected $depsCache;
+
        /**
         * @param ILogger $logger
         * @param IAppData $appData
         * @param IURLGenerator $urlGenerator
-        * @param SystemConfig $systemConfig
+        * @param IConfig $config
         * @param string $serverRoot
+        * @param ICache $depsCache
         */
        public function __construct(ILogger $logger,
                                                                IAppData $appData,
                                                                IURLGenerator $urlGenerator,
                                                                IConfig $config,
-                                                               $serverRoot) {
+                                                               $serverRoot,
+                                                               ICache $depsCache) {
                $this->logger = $logger;
                $this->appData = $appData;
                $this->urlGenerator = $urlGenerator;
                $this->config = $config;
                $this->serverRoot = $serverRoot;
+               $this->depsCache = $depsCache;
        }
 
        /**
@@ -94,7 +101,7 @@ class SCSSCacher {
                        $folder = $this->appData->newFolder($app);
                }
 
-               if($this->isCached($fileNameCSS, $fileNameSCSS, $folder, $path)) {
+               if($this->isCached($fileNameCSS, $folder)) {
                        return true;
                }
                return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
@@ -103,17 +110,22 @@ class SCSSCacher {
        /**
         * Check if the file is cached or not
         * @param string $fileNameCSS
-        * @param string $fileNameSCSS
         * @param ISimpleFolder $folder
-        * @param string $path
         * @return boolean
         */
-       private function isCached($fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $path) {
+       private function isCached($fileNameCSS, ISimpleFolder $folder) {
                try {
                        $cachedFile = $folder->getFile($fileNameCSS);
                        if ($cachedFile->getSize() > 0) {
-                               $depFile = $folder->getFile($fileNameCSS . '.deps');
-                               $deps = json_decode($depFile->getContent(), true);
+                               $depFileName = $fileNameCSS . '.deps';
+                               $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName);
+                               if ($deps === null) {
+                                       $depFile = $folder->getFile($depFileName);
+                                       $deps = $depFile->getContent();
+                                       //Set to memcache for next run
+                                       $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
+                               }
+                               $deps = json_decode($deps, true);
 
                                foreach ($deps as $file=>$mtime) {
                                        if (!file_exists($file) || filemtime($file) > $mtime) {
index 33f61e3c3ac3a43b373ec151af83296105fc3abd..3206a1d3ba8819b297bb9a930c3ffd6d98cb4e5f 100644 (file)
@@ -213,12 +213,15 @@ class TemplateLayout extends \OC_Template {
                $theme = \OC_Util::getTheme();
 
                if($compileScss) {
+                       /** @var \OC\Memcache\Factory $cache */
+                       $cache = \OC::$server->query('MemCacheFactory');
                        $SCSSCacher = new SCSSCacher(
                                \OC::$server->getLogger(),
                                \OC::$server->getAppDataDir('css'),
                                \OC::$server->getURLGenerator(),
                                \OC::$server->getConfig(),
-                               \OC::$SERVERROOT
+                               \OC::$SERVERROOT,
+                               $cache->createLocal('SCSS')
                        );
                } else {
                        $SCSSCacher = null;
index d49f0cdc6a14e77aa96be1426cfd2183e62df93c..b345c67387193679100245b351ce2544b006d8e2 100644 (file)
@@ -28,6 +28,7 @@ use OCP\Files\IAppData;
 use OCP\Files\NotFoundException;
 use OCP\Files\SimpleFS\ISimpleFile;
 use OCP\Files\SimpleFS\ISimpleFolder;
+use OCP\ICache;
 use OCP\IConfig;
 use OCP\ILogger;
 use OCP\IURLGenerator;
@@ -57,7 +58,8 @@ class SCSSCacherTest extends \Test\TestCase {
                        $this->appData,
                        $this->urlGenerator,
                        $this->config,
-                       \OC::$SERVERROOT
+                       \OC::$SERVERROOT,
+                       $this->createMock(ICache::class)
                );
        }
 
@@ -275,4 +277,4 @@ class SCSSCacherTest extends \Test\TestCase {
        }
 
 
-}
\ No newline at end of file
+}