Skip to content

Commit

Permalink
cache isCached state for scss resources
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Jul 24, 2018
1 parent 7da815b commit 6686ca9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,8 @@ public function __construct($webRoot, \OC\Config $config) {
$c->getThemingDefaults(),
\OC::$SERVERROOT,
$this->getMemCacheFactory(),
$c->query(IconsCacher::class)
$c->query(IconsCacher::class),
new TimeFactory()
);
});
$this->registerService(JSCombiner::class, function (Server $c) {
Expand Down
48 changes: 37 additions & 11 deletions lib/private/Template/SCSSCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Leafo\ScssPhp\Exception\ParserException;
use Leafo\ScssPhp\Formatter\Crunched;
use Leafo\ScssPhp\Formatter\Expanded;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
Expand Down Expand Up @@ -77,6 +78,12 @@ class SCSSCacher {
/** @var IconsCacher */
private $iconsCacher;

/** @var ICache */
private $isCachedCache;

/** @var ITimeFactory */
private $timeFactory;

/**
* @param ILogger $logger
* @param Factory $appDataFactory
Expand All @@ -86,6 +93,7 @@ class SCSSCacher {
* @param string $serverRoot
* @param ICacheFactory $cacheFactory
* @param IconsCacher $iconsCacher
* @param ITimeFactory $timeFactory
*/
public function __construct(ILogger $logger,
Factory $appDataFactory,
Expand All @@ -94,7 +102,8 @@ public function __construct(ILogger $logger,
\OC_Defaults $defaults,
$serverRoot,
ICacheFactory $cacheFactory,
IconsCacher $iconsCacher) {
IconsCacher $iconsCacher,
ITimeFactory $timeFactory) {
$this->logger = $logger;
$this->appData = $appDataFactory->get('css');
$this->urlGenerator = $urlGenerator;
Expand All @@ -103,7 +112,9 @@ public function __construct(ILogger $logger,
$this->serverRoot = $serverRoot;
$this->cacheFactory = $cacheFactory;
$this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl()));
$this->isCachedCache = $cacheFactory->createLocal('SCSS-cached-' . md5($this->urlGenerator->getBaseUrl()));
$this->iconsCacher = $iconsCacher;
$this->timeFactory = $timeFactory;
}

/**
Expand All @@ -124,21 +135,21 @@ public function process(string $root, string $file, string $app): bool {
$path = implode('/', $path);
$webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT);

try {
$folder = $this->appData->getFolder($app);
} catch (NotFoundException $e) {
// creating css appdata folder
$folder = $this->appData->newFolder($app);
}

if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) {
if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $app)) {
// Inject icons vars css if any
if ($this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) {
$this->iconsCacher->injectCss();
}
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);

// Inject icons vars css if any
Expand All @@ -164,10 +175,24 @@ public function getCachedCSS(string $appName, string $fileName): ISimpleFile {
/**
* Check if the file is cached or not
* @param string $fileNameCSS
* @param ISimpleFolder $folder
* @param string $app
* @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 {
$cachedFile = $folder->getFile($fileNameCSS);
if ($cachedFile->getSize() > 0) {
Expand All @@ -187,6 +212,7 @@ private function isCached(string $fileNameCSS, ISimpleFolder $folder) {
}
}

$this->isCachedCache->set($key, $this->timeFactory->getTime() + 5 * 60);
return true;
}

Expand Down
7 changes: 6 additions & 1 deletion tests/lib/Template/CSSResourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OC\Files\AppData\AppData;
use OC\Files\AppData\Factory;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\ICacheFactory;
use OCP\ILogger;
Expand All @@ -50,6 +51,8 @@ class CSSResourceLocatorTest extends \Test\TestCase {
protected $logger;
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
protected $iconsCacher;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
private $timeFactory;

protected function setUp() {
parent::setUp();
Expand All @@ -61,6 +64,7 @@ protected function setUp() {
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
$this->iconsCacher = $this->createMock(IconsCacher::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
}

private function cssResourceLocator() {
Expand All @@ -75,7 +79,8 @@ private function cssResourceLocator() {
$this->themingDefaults,
\OC::$SERVERROOT,
$this->cacheFactory,
$this->iconsCacher
$this->iconsCacher,
$this->timeFactory
);
return new CSSResourceLocator(
$this->logger,
Expand Down
7 changes: 6 additions & 1 deletion tests/lib/Template/SCSSCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OC\Template\SCSSCacher;
use OC\Template\IconsCacher;
use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
Expand Down Expand Up @@ -58,12 +59,15 @@ class SCSSCacherTest extends \Test\TestCase {
protected $cacheFactory;
/** @var IconsCacher|\PHPUnit_Framework_MockObject_MockObject */
protected $iconsCacher;
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $timeFactory;

protected function setUp() {
parent::setUp();
$this->logger = $this->createMock(ILogger::class);
$this->appData = $this->createMock(AppData::class);
$this->iconsCacher = $this->createMock(IconsCacher::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);

/** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */
$factory = $this->createMock(Factory::class);
Expand Down Expand Up @@ -97,7 +101,8 @@ protected function setUp() {
$this->themingDefaults,
\OC::$SERVERROOT,
$this->cacheFactory,
$this->iconsCacher
$this->iconsCacher,
$this->timeFactory
);
}

Expand Down

0 comments on commit 6686ca9

Please sign in to comment.