cache isCached state for scss resources

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2018-07-24 17:30:39 +02:00
parent 7da815bb04
commit 6686ca9b2a
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB
4 changed files with 51 additions and 14 deletions

View file

@ -965,7 +965,8 @@ class Server extends ServerContainer implements IServerContainer {
$c->getThemingDefaults(), $c->getThemingDefaults(),
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->getMemCacheFactory(), $this->getMemCacheFactory(),
$c->query(IconsCacher::class) $c->query(IconsCacher::class),
new TimeFactory()
); );
}); });
$this->registerService(JSCombiner::class, function (Server $c) { $this->registerService(JSCombiner::class, function (Server $c) {

View file

@ -32,6 +32,7 @@ use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Exception\ParserException; use Leafo\ScssPhp\Exception\ParserException;
use Leafo\ScssPhp\Formatter\Crunched; use Leafo\ScssPhp\Formatter\Crunched;
use Leafo\ScssPhp\Formatter\Expanded; use Leafo\ScssPhp\Formatter\Expanded;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
@ -77,6 +78,12 @@ class SCSSCacher {
/** @var IconsCacher */ /** @var IconsCacher */
private $iconsCacher; private $iconsCacher;
/** @var ICache */
private $isCachedCache;
/** @var ITimeFactory */
private $timeFactory;
/** /**
* @param ILogger $logger * @param ILogger $logger
* @param Factory $appDataFactory * @param Factory $appDataFactory
@ -86,6 +93,7 @@ class SCSSCacher {
* @param string $serverRoot * @param string $serverRoot
* @param ICacheFactory $cacheFactory * @param ICacheFactory $cacheFactory
* @param IconsCacher $iconsCacher * @param IconsCacher $iconsCacher
* @param ITimeFactory $timeFactory
*/ */
public function __construct(ILogger $logger, public function __construct(ILogger $logger,
Factory $appDataFactory, Factory $appDataFactory,
@ -94,7 +102,8 @@ class SCSSCacher {
\OC_Defaults $defaults, \OC_Defaults $defaults,
$serverRoot, $serverRoot,
ICacheFactory $cacheFactory, ICacheFactory $cacheFactory,
IconsCacher $iconsCacher) { IconsCacher $iconsCacher,
ITimeFactory $timeFactory) {
$this->logger = $logger; $this->logger = $logger;
$this->appData = $appDataFactory->get('css'); $this->appData = $appDataFactory->get('css');
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
@ -103,7 +112,9 @@ class SCSSCacher {
$this->serverRoot = $serverRoot; $this->serverRoot = $serverRoot;
$this->cacheFactory = $cacheFactory; $this->cacheFactory = $cacheFactory;
$this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl())); $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl()));
$this->isCachedCache = $cacheFactory->createLocal('SCSS-cached-' . md5($this->urlGenerator->getBaseUrl()));
$this->iconsCacher = $iconsCacher; $this->iconsCacher = $iconsCacher;
$this->timeFactory = $timeFactory;
} }
/** /**
@ -124,14 +135,7 @@ class SCSSCacher {
$path = implode('/', $path); $path = implode('/', $path);
$webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT); $webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT);
try { if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $app)) {
$folder = $this->appData->getFolder($app);
} catch (NotFoundException $e) {
// creating css appdata folder
$folder = $this->appData->newFolder($app);
}
if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) {
// Inject icons vars css if any // Inject icons vars css if any
if ($this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) { if ($this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) {
$this->iconsCacher->injectCss(); $this->iconsCacher->injectCss();
@ -139,6 +143,13 @@ class SCSSCacher {
return true; return true;
} }
try {
$folder = $this->appData->getFolder($app);
} catch (NotFoundException $e) {
// creating css appdata folder
$folder = $this->appData->newFolder($app);
}
$cached = $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); $cached = $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
// Inject icons vars css if any // Inject icons vars css if any
@ -164,10 +175,24 @@ class SCSSCacher {
/** /**
* Check if the file is cached or not * Check if the file is cached or not
* @param string $fileNameCSS * @param string $fileNameCSS
* @param ISimpleFolder $folder * @param string $app
* @return boolean * @return boolean
*/ */
private function isCached(string $fileNameCSS, ISimpleFolder $folder) { private function isCached(string $fileNameCSS, string $app) {
$key = $this->config->getSystemValue('version') . '/' . $app . '/' . $fileNameCSS;
if (!$this->config->getSystemValue('debug') && $cacheValue = $this->isCachedCache->get($key)) {
if ($cacheValue > $this->timeFactory->getTime()) {
return true;
}
}
try {
$folder = $this->appData->getFolder($app);
} catch (NotFoundException $e) {
// creating css appdata folder
$folder = $this->appData->newFolder($app);
}
try { try {
$cachedFile = $folder->getFile($fileNameCSS); $cachedFile = $folder->getFile($fileNameCSS);
if ($cachedFile->getSize() > 0) { if ($cachedFile->getSize() > 0) {
@ -187,6 +212,7 @@ class SCSSCacher {
} }
} }
$this->isCachedCache->set($key, $this->timeFactory->getTime() + 5 * 60);
return true; return true;
} }

View file

@ -25,6 +25,7 @@ namespace Test\Template;
use OC\Files\AppData\AppData; use OC\Files\AppData\AppData;
use OC\Files\AppData\Factory; use OC\Files\AppData\Factory;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\ILogger; use OCP\ILogger;
@ -50,6 +51,8 @@ class CSSResourceLocatorTest extends \Test\TestCase {
protected $logger; protected $logger;
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */ /** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
protected $iconsCacher; protected $iconsCacher;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
private $timeFactory;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -61,6 +64,7 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->themingDefaults = $this->createMock(ThemingDefaults::class); $this->themingDefaults = $this->createMock(ThemingDefaults::class);
$this->iconsCacher = $this->createMock(IconsCacher::class); $this->iconsCacher = $this->createMock(IconsCacher::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
} }
private function cssResourceLocator() { private function cssResourceLocator() {
@ -75,7 +79,8 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->themingDefaults, $this->themingDefaults,
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->cacheFactory, $this->cacheFactory,
$this->iconsCacher $this->iconsCacher,
$this->timeFactory
); );
return new CSSResourceLocator( return new CSSResourceLocator(
$this->logger, $this->logger,

View file

@ -28,6 +28,7 @@ use OC\Files\AppData\Factory;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OC\Template\IconsCacher; use OC\Template\IconsCacher;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
@ -58,12 +59,15 @@ class SCSSCacherTest extends \Test\TestCase {
protected $cacheFactory; protected $cacheFactory;
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */ /** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
protected $iconsCacher; protected $iconsCacher;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $timeFactory;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->appData = $this->createMock(AppData::class); $this->appData = $this->createMock(AppData::class);
$this->iconsCacher = $this->createMock(IconsCacher::class); $this->iconsCacher = $this->createMock(IconsCacher::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
$factory = $this->createMock(Factory::class); $factory = $this->createMock(Factory::class);
@ -97,7 +101,8 @@ class SCSSCacherTest extends \Test\TestCase {
$this->themingDefaults, $this->themingDefaults,
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->cacheFactory, $this->cacheFactory,
$this->iconsCacher $this->iconsCacher,
$this->timeFactory
); );
} }