From 79479dd3524c0ff27919749bfbe04455b534484b Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Willms Date: Wed, 6 Nov 2024 09:47:33 +0000 Subject: allow cache settings to be configured even if caching is disabled, fixes #4806 Closes #4806 Merge request studip/studip!3597 --- app/controllers/admin/cache.php | 20 ++++---- lib/bootstrap-autoload.php | 1 + lib/classes/cache/Factory.php | 60 ++++++++++++++---------- resources/vue/components/CacheAdministration.vue | 8 ++-- tests/functional/_bootstrap.php | 1 + tests/jsonapi/_bootstrap.php | 4 +- 6 files changed, 53 insertions(+), 41 deletions(-) diff --git a/app/controllers/admin/cache.php b/app/controllers/admin/cache.php index 7bd70c6..e2248b8 100644 --- a/app/controllers/admin/cache.php +++ b/app/controllers/admin/cache.php @@ -47,16 +47,14 @@ class Admin_CacheController extends AuthenticatedController $this->sidebar->addWidget($views); - if ($this->enabled) { - $actions = new ActionsWidget(); - $actions->addLink( - _('Cache leeren'), - $this->url_for('admin/cache/flush'), - Icon::create('decline'), - ['data-confirm' => _('Soll der gesamte Inhalt des Caches wirklich gelöscht werden?')] - ); - $this->sidebar->addWidget($actions); - } + $actions = new ActionsWidget(); + $actions->addLink( + _('Cache leeren'), + $this->url_for('admin/cache/flush'), + Icon::create('decline'), + ['data-confirm' => _('Soll der gesamte Inhalt des Caches wirklich gelöscht werden?')] + ); + $this->sidebar->addWidget($actions); } /** @@ -123,7 +121,7 @@ class Admin_CacheController extends AuthenticatedController */ public function flush_action() { - $cache = \Studip\Cache\Factory::getCache(); + $cache = \Studip\Cache\Factory::loadSystemCache(true); $cache->flush(); PageLayout::postSuccess(_('Die Inhalte des Caches wurden gelöscht.')); diff --git a/lib/bootstrap-autoload.php b/lib/bootstrap-autoload.php index 7d99f86..fb881df 100644 --- a/lib/bootstrap-autoload.php +++ b/lib/bootstrap-autoload.php @@ -5,6 +5,7 @@ StudipAutoloader::register(); class_alias(\Studip\Cache\Factory::class, 'StudipCacheFactory'); class_alias(\Studip\Cache\Cache::class, 'StudipCache'); +class_alias(\Studip\Cache\DbCache::class, 'StudipDbCache'); class_alias(Flexi\PhpTemplate::class, 'Flexi_PhpTemplate'); class_alias(Flexi\Template::class, 'Flexi_Template'); class_alias(Flexi\Factory::class, 'Flexi_TemplateFactory'); diff --git a/lib/classes/cache/Factory.php b/lib/classes/cache/Factory.php index b5c8359..0def769 100644 --- a/lib/classes/cache/Factory.php +++ b/lib/classes/cache/Factory.php @@ -92,33 +92,15 @@ class Factory public static function getCache(bool $apply_proxied_operations = true): Cache { if (self::$cache === null) { - $proxied = false; + $proxied = !$GLOBALS['CACHING_ENABLE'] + && isset($GLOBALS['GLOBAL_CACHING_ENABLE']) + && $GLOBALS['GLOBAL_CACHING_ENABLE']; - if (!$GLOBALS['CACHING_ENABLE']) { - self::$cache = new MemoryCache(); + self::$cache = self::loadSystemCache(); - // Proxy cache operations if CACHING_ENABLE is different from the globally set - // caching value. This should only be the case in cli mode. - if (isset($GLOBALS['GLOBAL_CACHING_ENABLE']) && $GLOBALS['GLOBAL_CACHING_ENABLE']) { - $proxied = true; - } - } else { - try { - $class = self::loadCacheClass(); - $args = self::retrieveConstructorArguments(); - - self::$cache = self::instantiateCache($class, $args); - } catch (\Exception $e) { - error_log(__METHOD__ . ': ' . $e->getMessage()); - PageLayout::addBodyElements(MessageBox::error(__METHOD__ . ': ' . $e->getMessage())); - $class = self::DEFAULT_CACHE_CLASS; - self::$cache = new $class(); - } - } - - // If proxy should be used, inject it. Otherwise apply pending - // operations, if any. if ($proxied) { + // If proxy should be used, inject it. Otherwise apply pending + // operations, if any. self::$cache = new Proxy(self::$cache); } elseif ($GLOBALS['CACHING_ENABLE'] && $apply_proxied_operations) { // Even if the above condition will try to eliminate most @@ -136,6 +118,34 @@ class Factory return self::$cache; } + /** + * Loads the system's configured cache. + * + * @param bool $enforce_configured_cache Define whether the cache should + * be loaded regardless of global + * activation + */ + public static function loadSystemCache(bool $enforce_configured_cache = false): Cache + { + if (!$GLOBALS['CACHING_ENABLE'] && !$enforce_configured_cache) { + return new MemoryCache(); + } + + try { + $class = self::loadCacheClass(); + $args = self::retrieveConstructorArguments(); + + $cache = self::instantiateCache($class, $args); + } catch (\Exception $e) { + error_log(__METHOD__ . ': ' . $e->getMessage()); + PageLayout::addBodyElements(MessageBox::error(__METHOD__ . ': ' . $e->getMessage())); + + $class = self::DEFAULT_CACHE_CLASS; + $cache = new $class(); + } + + return $cache; + } /** * Load configured cache class and return its name. @@ -153,7 +163,7 @@ class Factory $version = new DBSchemaVersion(); if ($version->get(1) < 224) { // db cache is not yet available, use StudipMemoryCache - return 'StudipMemoryCache'; + return MemoryCache::class; } return self::DEFAULT_CACHE_CLASS; diff --git a/resources/vue/components/CacheAdministration.vue b/resources/vue/components/CacheAdministration.vue index a7a1792..c322075 100644 --- a/resources/vue/components/CacheAdministration.vue +++ b/resources/vue/components/CacheAdministration.vue @@ -1,8 +1,8 @@