Merge pull request #8716 from nextcloud/fix-cache-prefixing

Remove base url from global cache prefix
This commit is contained in:
Roeland Jago Douma 2018-03-08 21:12:54 +01:00 committed by GitHub
commit 7a7c350a2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 36 deletions

View File

@ -231,7 +231,7 @@ class ThemingDefaults extends \OC_Defaults {
* @return array scss variables to overwrite * @return array scss variables to overwrite
*/ */
public function getScssVariables() { public function getScssVariables() {
$cache = $this->cacheFactory->createDistributed('theming'); $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
if ($value = $cache->get('getScssVariables')) { if ($value = $cache->get('getScssVariables')) {
return $value; return $value;
} }
@ -298,7 +298,7 @@ class ThemingDefaults extends \OC_Defaults {
* @return bool * @return bool
*/ */
public function shouldReplaceIcons() { public function shouldReplaceIcons() {
$cache = $this->cacheFactory->createDistributed('theming'); $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
if($value = $cache->get('shouldReplaceIcons')) { if($value = $cache->get('shouldReplaceIcons')) {
return (bool)$value; return (bool)$value;
} }
@ -320,7 +320,7 @@ class ThemingDefaults extends \OC_Defaults {
private function increaseCacheBuster() { private function increaseCacheBuster() {
$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0'); $cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1); $this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
$this->cacheFactory->createDistributed('theming')->clear('getScssVariables'); $this->cacheFactory->createDistributed('theming-')->clear('getScssVariables');
} }
/** /**

View File

@ -79,7 +79,7 @@ class ThemingDefaultsTest extends TestCase {
$this->cacheFactory $this->cacheFactory
->expects($this->any()) ->expects($this->any())
->method('createDistributed') ->method('createDistributed')
->with('theming') ->with('theming-')
->willReturn($this->cache); ->willReturn($this->cache);
$this->template = new ThemingDefaults( $this->template = new ThemingDefaults(
$this->config, $this->config,

View File

@ -498,7 +498,7 @@ class Server extends ServerContainer implements IServerContainer {
$version = implode(',', $v); $version = implode(',', $v);
$instanceId = \OC_Util::getInstanceId(); $instanceId = \OC_Util::getInstanceId();
$path = \OC::$SERVERROOT; $path = \OC::$SERVERROOT;
$prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . $urlGenerator->getBaseUrl()); $prefix = md5($instanceId . '-' . $version . '-' . $path);
return new \OC\Memcache\Factory($prefix, $c->getLogger(), return new \OC\Memcache\Factory($prefix, $c->getLogger(),
$config->getSystemValue('memcache.local', null), $config->getSystemValue('memcache.local', null),
$config->getSystemValue('memcache.distributed', null), $config->getSystemValue('memcache.distributed', null),
@ -965,7 +965,7 @@ class Server extends ServerContainer implements IServerContainer {
$c->getConfig(), $c->getConfig(),
$c->getThemingDefaults(), $c->getThemingDefaults(),
\OC::$SERVERROOT, \OC::$SERVERROOT,
$cacheFactory->createDistributed('SCSS') $this->getMemCacheFactory()
); );
}); });
$this->registerService(JSCombiner::class, function (Server $c) { $this->registerService(JSCombiner::class, function (Server $c) {
@ -974,7 +974,7 @@ class Server extends ServerContainer implements IServerContainer {
return new JSCombiner( return new JSCombiner(
$c->getAppDataDir('js'), $c->getAppDataDir('js'),
$c->getURLGenerator(), $c->getURLGenerator(),
$cacheFactory->createDistributed('JS'), $this->getMemCacheFactory(),
$c->getSystemConfig(), $c->getSystemConfig(),
$c->getLogger() $c->getLogger()
); );

View File

@ -30,6 +30,7 @@ use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICacheFactory;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -50,21 +51,25 @@ class JSCombiner {
/** @var ILogger */ /** @var ILogger */
protected $logger; protected $logger;
/** @var ICacheFactory */
private $cacheFactory;
/** /**
* @param IAppData $appData * @param IAppData $appData
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param ICache $depsCache * @param ICacheFactory $cacheFactory
* @param SystemConfig $config * @param SystemConfig $config
* @param ILogger $logger * @param ILogger $logger
*/ */
public function __construct(IAppData $appData, public function __construct(IAppData $appData,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
ICache $depsCache, ICacheFactory $cacheFactory,
SystemConfig $config, SystemConfig $config,
ILogger $logger) { ILogger $logger) {
$this->appData = $appData; $this->appData = $appData;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->depsCache = $depsCache; $this->cacheFactory = $cacheFactory;
$this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl()));
$this->config = $config; $this->config = $config;
$this->logger = $logger; $this->logger = $logger;
} }
@ -243,7 +248,7 @@ class JSCombiner {
* @throws NotFoundException * @throws NotFoundException
*/ */
public function resetCache() { public function resetCache() {
$this->depsCache->clear(); $this->cacheFactory->createDistributed('JS-')->clear();
$appDirectory = $this->appData->getDirectoryListing(); $appDirectory = $this->appData->getDirectoryListing();
foreach ($appDirectory as $folder) { foreach ($appDirectory as $folder) {
foreach ($folder->getDirectoryListing() as $file) { foreach ($folder->getDirectoryListing() as $file) {

View File

@ -39,6 +39,7 @@ use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache; use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -69,6 +70,9 @@ class SCSSCacher {
/** @var null|string */ /** @var null|string */
private $injectedVariables; private $injectedVariables;
/** @var ICacheFactory */
private $cacheFactory;
/** /**
* @param ILogger $logger * @param ILogger $logger
* @param Factory $appDataFactory * @param Factory $appDataFactory
@ -76,7 +80,7 @@ class SCSSCacher {
* @param IConfig $config * @param IConfig $config
* @param \OC_Defaults $defaults * @param \OC_Defaults $defaults
* @param string $serverRoot * @param string $serverRoot
* @param ICache $depsCache * @param ICacheFactory $cacheFactory
*/ */
public function __construct(ILogger $logger, public function __construct(ILogger $logger,
Factory $appDataFactory, Factory $appDataFactory,
@ -84,14 +88,15 @@ class SCSSCacher {
IConfig $config, IConfig $config,
\OC_Defaults $defaults, \OC_Defaults $defaults,
$serverRoot, $serverRoot,
ICache $depsCache) { ICacheFactory $cacheFactory) {
$this->logger = $logger; $this->logger = $logger;
$this->appData = $appDataFactory->get('css'); $this->appData = $appDataFactory->get('css');
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->config = $config; $this->config = $config;
$this->defaults = $defaults; $this->defaults = $defaults;
$this->serverRoot = $serverRoot; $this->serverRoot = $serverRoot;
$this->depsCache = $depsCache; $this->cacheFactory = $cacheFactory;
$this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl()));
} }
/** /**
@ -263,7 +268,7 @@ class SCSSCacher {
*/ */
public function resetCache() { public function resetCache() {
$this->injectedVariables = null; $this->injectedVariables = null;
$this->depsCache->clear(); $this->cacheFactory->createDistributed('SCSS-')->clear();
$appDirectory = $this->appData->getDirectoryListing(); $appDirectory = $this->appData->getDirectoryListing();
foreach ($appDirectory as $folder) { foreach ($appDirectory as $folder) {
foreach ($folder->getDirectoryListing() as $file) { foreach ($folder->getDirectoryListing() as $file) {

View File

@ -306,13 +306,7 @@ class TemplateLayout extends \OC_Template {
$theme, $theme,
array( \OC::$SERVERROOT => \OC::$WEBROOT ), array( \OC::$SERVERROOT => \OC::$WEBROOT ),
array( \OC::$SERVERROOT => \OC::$WEBROOT ), array( \OC::$SERVERROOT => \OC::$WEBROOT ),
new JSCombiner( \OC::$server->query(JSCombiner::class)
\OC::$server->getAppDataDir('js'),
\OC::$server->getURLGenerator(),
\OC::$server->getMemCacheFactory()->createDistributed('JS'),
\OC::$server->getSystemConfig(),
\OC::$server->getLogger()
)
); );
$locator->find($scripts); $locator->find($scripts);
return $locator->getResources(); return $locator->getResources();

View File

@ -25,11 +25,11 @@ namespace Test\Template;
use OC\Files\AppData\Factory; use OC\Files\AppData\Factory;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\ICacheFactory;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IConfig; use OCP\IConfig;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\ICache;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OC\Template\CSSResourceLocator; use OC\Template\CSSResourceLocator;
@ -42,8 +42,8 @@ class CSSResourceLocatorTest extends \Test\TestCase {
protected $config; protected $config;
/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */ /** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
protected $themingDefaults; protected $themingDefaults;
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $depsCache; protected $cacheFactory;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger; protected $logger;
@ -54,7 +54,7 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->depsCache = $this->createMock(ICache::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->themingDefaults = $this->createMock(ThemingDefaults::class); $this->themingDefaults = $this->createMock(ThemingDefaults::class);
} }
@ -69,7 +69,7 @@ class CSSResourceLocatorTest extends \Test\TestCase {
$this->config, $this->config,
$this->themingDefaults, $this->themingDefaults,
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->depsCache $this->cacheFactory
); );
return new CSSResourceLocator( return new CSSResourceLocator(
$this->logger, $this->logger,

View File

@ -31,6 +31,7 @@ use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache; use OCP\ICache;
use OCP\ICacheFactory;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -47,6 +48,8 @@ class JSCombinerTest extends \Test\TestCase {
protected $jsCombiner; protected $jsCombiner;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger; protected $logger;
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $cacheFactory;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -54,15 +57,20 @@ class JSCombinerTest extends \Test\TestCase {
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(SystemConfig::class); $this->config = $this->createMock(SystemConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->depsCache = $this->createMock(ICache::class); $this->depsCache = $this->createMock(ICache::class);
$this->cacheFactory->expects($this->at(0))
->method('createDistributed')
->willReturn($this->depsCache);
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
$this->jsCombiner = new JSCombiner( $this->jsCombiner = new JSCombiner(
$this->appData, $this->appData,
$this->urlGenerator, $this->urlGenerator,
$this->depsCache, $this->cacheFactory,
$this->config, $this->config,
$this->logger $this->logger
); );
} }
public function testProcessDebugMode() { public function testProcessDebugMode() {
@ -539,7 +547,11 @@ var b = \'world\';
->method('getDirectoryListing') ->method('getDirectoryListing')
->willReturn([$file]); ->willReturn([$file]);
$this->depsCache->expects($this->once()) $cache = $this->createMock(ICache::class);
$this->cacheFactory->expects($this->once())
->method('createDistributed')
->willReturn($cache);
$cache->expects($this->once())
->method('clear') ->method('clear')
->with(''); ->with('');
$this->appData->expects($this->once()) $this->appData->expects($this->once())

View File

@ -25,8 +25,8 @@ namespace Test\Template;
use OC\Template\JSCombiner; use OC\Template\JSCombiner;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\ICacheFactory;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\ICache;
use OC\SystemConfig; use OC\SystemConfig;
use OCP\ILogger; use OCP\ILogger;
use OC\Template\JSResourceLocator; use OC\Template\JSResourceLocator;
@ -38,8 +38,8 @@ class JSResourceLocatorTest extends \Test\TestCase {
protected $urlGenerator; protected $urlGenerator;
/** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */ /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config; protected $config;
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $depsCache; protected $cacheFactory;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
protected $logger; protected $logger;
@ -49,7 +49,7 @@ class JSResourceLocatorTest extends \Test\TestCase {
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(SystemConfig::class); $this->config = $this->createMock(SystemConfig::class);
$this->depsCache = $this->createMock(ICache::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
} }
@ -57,7 +57,7 @@ class JSResourceLocatorTest extends \Test\TestCase {
$jsCombiner = new JSCombiner( $jsCombiner = new JSCombiner(
$this->appData, $this->appData,
$this->urlGenerator, $this->urlGenerator,
$this->depsCache, $this->cacheFactory,
$this->config, $this->config,
$this->logger $this->logger
); );

View File

@ -31,6 +31,7 @@ use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache; use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -50,6 +51,8 @@ class SCSSCacherTest extends \Test\TestCase {
protected $scssCacher; protected $scssCacher;
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
protected $depsCache; protected $depsCache;
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $cacheFactory;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -60,7 +63,11 @@ class SCSSCacherTest extends \Test\TestCase {
$factory->method('get')->with('css')->willReturn($this->appData); $factory->method('get')->with('css')->willReturn($this->appData);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->depsCache = $this->createMock(ICache::class); $this->depsCache = $this->createMock(ICache::class);
$this->cacheFactory->expects($this->at(0))
->method('createDistributed')
->willReturn($this->depsCache);
$this->themingDefaults = $this->createMock(ThemingDefaults::class); $this->themingDefaults = $this->createMock(ThemingDefaults::class);
$this->scssCacher = new SCSSCacher( $this->scssCacher = new SCSSCacher(
$this->logger, $this->logger,
@ -69,7 +76,7 @@ class SCSSCacherTest extends \Test\TestCase {
$this->config, $this->config,
$this->themingDefaults, $this->themingDefaults,
\OC::$SERVERROOT, \OC::$SERVERROOT,
$this->depsCache $this->cacheFactory
); );
$this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]); $this->themingDefaults->expects($this->any())->method('getScssVariables')->willReturn([]);
@ -459,7 +466,11 @@ class SCSSCacherTest extends \Test\TestCase {
->method('getDirectoryListing') ->method('getDirectoryListing')
->willReturn([$file]); ->willReturn([$file]);
$this->depsCache->expects($this->once()) $cache = $this->createMock(ICache::class);
$this->cacheFactory->expects($this->once())
->method('createDistributed')
->willReturn($cache);
$cache->expects($this->once())
->method('clear') ->method('clear')
->with(''); ->with('');
$this->appData->expects($this->once()) $this->appData->expects($this->once())