Add caching for theming inline js

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2019-01-15 21:39:45 +01:00
parent fe59d751b3
commit cc506990e0
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
1 changed files with 10 additions and 1 deletions

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
namespace OCA\Theming;
use OCP\AppFramework\Http\Inline\IInline;
use OCP\ICache;
use OCP\IConfig;
class InlineJS implements IInline {
@ -34,15 +35,21 @@ class InlineJS implements IInline {
private $config;
/** @var Util */
private $util;
/** @var ICache */
private $cache;
public function __construct(IConfig $config, Util $util, ThemingDefaults $themingDefaults) {
public function __construct(IConfig $config, Util $util, ThemingDefaults $themingDefaults, ICache $cache) {
$this->themingDefaults = $themingDefaults;
$this->config = $config;
$this->util = $util;
$this->cache = $cache;
}
function getData(): string {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
if ($this->cache->hasKey('theming-inline-' . $cacheBusterValue)) {
return $this->cache->get('theming-inline-' . $cacheBusterValue);
}
$responseJS = 'document.addEventListener(\'DOMContentLoaded\', function() {
window.OCA.Theming = {
name: ' . json_encode($this->themingDefaults->getName()) . ',
@ -55,6 +62,8 @@ class InlineJS implements IInline {
cacheBuster: ' . json_encode($cacheBusterValue) . '
};
});';
$this->cache->clear('theming-inline-');
$this->cache->set('theming-inline-' . $cacheBusterValue, $responseJS);
return $responseJS;
}
}