Improve caching

Signed-off-by: Julius Haertl <jus@bitgrid.net>
This commit is contained in:
Julius Haertl 2016-10-14 14:57:58 +02:00
parent 492d0b9f9b
commit 2e8dd21815
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
6 changed files with 71 additions and 32 deletions

View File

@ -96,7 +96,10 @@ class IconController extends Controller {
} }
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
$response->cacheFor(86400); $response->cacheFor(86400);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$response->addHeader('Pragma', 'cache'); $response->addHeader('Pragma', 'cache');
return $response; return $response;
} }
@ -111,19 +114,24 @@ class IconController extends Controller {
* @return FileDisplayResponse|DataDisplayResponse * @return FileDisplayResponse|DataDisplayResponse
*/ */
public function getFavicon($app = "core") { public function getFavicon($app = "core") {
if ($this->themingDefaults->shouldReplaceIcons()) {
$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app); $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
if($iconFile === null && $this->themingDefaults->shouldReplaceIcons()) { if($iconFile === null) {
$icon = $this->iconBuilder->getFavicon($app); $icon = $this->iconBuilder->getFavicon($app);
$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon); $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
} }
if ($this->themingDefaults->shouldReplaceIcons()) {
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']); $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
$response->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$response->addHeader('Pragma', 'cache');
} else { } else {
$response = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND); $response = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND);
$response->cacheFor(0);
$response->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
} }
$response->cacheFor(86400);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Pragma', 'cache');
return $response; return $response;
} }
@ -137,19 +145,24 @@ class IconController extends Controller {
* @return FileDisplayResponse|DataDisplayResponse * @return FileDisplayResponse|DataDisplayResponse
*/ */
public function getTouchIcon($app = "core") { public function getTouchIcon($app = "core") {
if ($this->themingDefaults->shouldReplaceIcons()) {
$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app); $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
if ($iconFile === null && $this->themingDefaults->shouldReplaceIcons()) { if ($iconFile === null) {
$icon = $this->iconBuilder->getTouchIcon($app); $icon = $this->iconBuilder->getTouchIcon($app);
$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon); $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
} }
if ($this->themingDefaults->shouldReplaceIcons()) {
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']); $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
$response->cacheFor(86400);
$expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$response->addHeader('Pragma', 'cache');
} else { } else {
$response = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND); $response = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND);
$response->cacheFor(0);
$response->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
} }
$response->cacheFor(86400);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Pragma', 'cache');
return $response; return $response;
} }
} }

View File

@ -94,6 +94,8 @@ class IconBuilder {
if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") { if($mime === "image/svg+xml" || substr($appIconContent, 0, 4) === "<svg") {
if(substr($appIconContent, 0, 5) !== "<?xml") { if(substr($appIconContent, 0, 5) !== "<?xml") {
$svg = "<?xml version=\"1.0\"?>".$appIconContent; $svg = "<?xml version=\"1.0\"?>".$appIconContent;
} else {
$svg = $appIconContent;
} }
$tmp = new Imagick(); $tmp = new Imagick();
$tmp->readImageBlob($svg); $tmp->readImageBlob($svg);

View File

@ -23,6 +23,7 @@
namespace OCA\Theming; namespace OCA\Theming;
use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -38,6 +39,8 @@ class ThemingDefaults extends \OC_Defaults {
private $urlGenerator; private $urlGenerator;
/** @var IRootFolder */ /** @var IRootFolder */
private $rootFolder; private $rootFolder;
/** @var ICacheFactory */
private $cacheFactory;
/** @var string */ /** @var string */
private $name; private $name;
/** @var string */ /** @var string */
@ -60,13 +63,15 @@ class ThemingDefaults extends \OC_Defaults {
IL10N $l, IL10N $l,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
\OC_Defaults $defaults, \OC_Defaults $defaults,
IRootFolder $rootFolder IRootFolder $rootFolder,
ICacheFactory $cacheFactory
) { ) {
parent::__construct(); parent::__construct();
$this->config = $config; $this->config = $config;
$this->l = $l; $this->l = $l;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->cacheFactory = $cacheFactory;
$this->name = $defaults->getName(); $this->name = $defaults->getName();
$this->url = $defaults->getBaseUrl(); $this->url = $defaults->getBaseUrl();
@ -151,14 +156,20 @@ class ThemingDefaults extends \OC_Defaults {
* @return bool * @return bool
*/ */
public function shouldReplaceIcons() { public function shouldReplaceIcons() {
$cache = $this->cacheFactory->create('theming');
if($cache->hasKey('shouldReplaceIcons')) {
return (bool)$cache->get('shouldReplaceIcons');
}
$value = false;
if(extension_loaded('imagick')) { if(extension_loaded('imagick')) {
$checkImagick = new \Imagick(); $checkImagick = new \Imagick();
if (count($checkImagick->queryFormats('SVG')) >= 1) { if (count($checkImagick->queryFormats('SVG')) >= 1) {
return true; $value = true;
} }
$checkImagick->clear(); $checkImagick->clear();
} }
return false; $cache->set('shouldReplaceIcons', $value);
return $value;
} }
/** /**

View File

@ -103,7 +103,10 @@ class IconControllerTest extends TestCase {
->willReturn($file); ->willReturn($file);
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
$expected->cacheFor(86400); $expected->cacheFor(86400);
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$expected->addHeader('Pragma', 'cache'); $expected->addHeader('Pragma', 'cache');
@$this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg')); @$this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg'));
@ -133,7 +136,10 @@ class IconControllerTest extends TestCase {
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']); $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
$expected->cacheFor(86400); $expected->cacheFor(86400);
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$expected->addHeader('Pragma', 'cache'); $expected->addHeader('Pragma', 'cache');
$this->assertEquals($expected, $this->iconController->getFavicon()); $this->assertEquals($expected, $this->iconController->getFavicon());
} }
@ -143,9 +149,8 @@ class IconControllerTest extends TestCase {
->method('shouldReplaceIcons') ->method('shouldReplaceIcons')
->willReturn(false); ->willReturn(false);
$expected = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND); $expected = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND);
$expected->cacheFor(86400); $expected->cacheFor(0);
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expected->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
$expected->addHeader('Pragma', 'cache');
$this->assertEquals($expected, $this->iconController->getFavicon()); $this->assertEquals($expected, $this->iconController->getFavicon());
} }
@ -172,7 +177,10 @@ class IconControllerTest extends TestCase {
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/png']); $expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/png']);
$expected->cacheFor(86400); $expected->cacheFor(86400);
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$expected->addHeader('Pragma', 'cache'); $expected->addHeader('Pragma', 'cache');
$this->assertEquals($expected, $this->iconController->getTouchIcon()); $this->assertEquals($expected, $this->iconController->getTouchIcon());
} }
@ -182,9 +190,8 @@ class IconControllerTest extends TestCase {
->method('shouldReplaceIcons') ->method('shouldReplaceIcons')
->willReturn(false); ->willReturn(false);
$expected = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND); $expected = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND);
$expected->cacheFor(86400); $expected->cacheFor(0);
$expected->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); $expected->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
$expected->addHeader('Pragma', 'cache');
$this->assertEquals($expected, $this->iconController->getTouchIcon()); $this->assertEquals($expected, $this->iconController->getTouchIcon());
} }

View File

@ -24,6 +24,7 @@
namespace OCA\Theming\Tests; namespace OCA\Theming\Tests;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
@ -43,6 +44,8 @@ class ThemingDefaultsTest extends TestCase {
private $template; private $template;
/** @var IRootFolder */ /** @var IRootFolder */
private $rootFolder; private $rootFolder;
/** @var ICacheFactory */
private $cacheFactory;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -52,6 +55,7 @@ class ThemingDefaultsTest extends TestCase {
$this->rootFolder = $this->getMockBuilder(IRootFolder::class) $this->rootFolder = $this->getMockBuilder(IRootFolder::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->cacheFactory = $this->getMockBuilder(ICacheFactory::class)->getMock();
$this->defaults = $this->getMockBuilder(\OC_Defaults::class) $this->defaults = $this->getMockBuilder(\OC_Defaults::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -76,7 +80,8 @@ class ThemingDefaultsTest extends TestCase {
$this->l10n, $this->l10n,
$this->urlGenerator, $this->urlGenerator,
$this->defaults, $this->defaults,
$this->rootFolder $this->rootFolder,
$this->cacheFactory
); );
} }

View File

@ -703,7 +703,8 @@ class Server extends ServerContainer implements IServerContainer {
$c->getL10N('theming'), $c->getL10N('theming'),
$c->getURLGenerator(), $c->getURLGenerator(),
new \OC_Defaults(), new \OC_Defaults(),
$c->getLazyRootFolder() $c->getLazyRootFolder(),
$c->getMemCacheFactory()
); );
} }
return new \OC_Defaults(); return new \OC_Defaults();