diff --git a/app/controllers/admin/cache.php b/app/controllers/admin/cache.php index 7bd70c64627a19d35e249a729cc6e00c8be2cac3..e2248b8dbac8922e57dbd710d5ac293b825e6dc0 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 7d99f860415110705a6fe56089734feb049957e9..fb881df3b43790298f989adc0183b16f3effba21 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 b5c835951a6e7add5544f565265bf29fdd30bd9f..0def7694e678dbbed16c6d0d7afefd7954288af7 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 a7a1792357d86d4c1cd0ea809484e0a40186ebec..c3220753bb8b583810cb61ee2d0dc67a34b31bc2 100644 --- a/resources/vue/components/CacheAdministration.vue +++ b/resources/vue/components/CacheAdministration.vue @@ -1,8 +1,8 @@ <template> - <StudipMessageBox v-if="!enabled" type="warning" :hide-close="true"> - {{ $gettext('Caching ist systemweit ausgeschaltet, daher kann hier nichts konfiguriert werden.') }} - </StudipMessageBox> - <form v-else class="default" :action="actionUrl" method="post" ref="configForm"> + <form class="default" :action="actionUrl" method="post" ref="configForm"> + <StudipMessageBox v-if="!enabled" type="warning" :hide-close="true"> + {{ $gettext('Caching ist systemweit ausgeschaltet.') }} + </StudipMessageBox> <fieldset> <legend> <translate>Cachetyp</translate> diff --git a/tests/functional/_bootstrap.php b/tests/functional/_bootstrap.php index e8a911d562a3423d339913f1914b25dd4914032d..cdc9db19668a8d3176957945d90179a40d6e0915 100644 --- a/tests/functional/_bootstrap.php +++ b/tests/functional/_bootstrap.php @@ -38,6 +38,7 @@ foreach($added_configs as $key => $value) { $GLOBALS[$key] = $value; } require 'config/config.inc.php'; +require_once __DIR__ . '/../../lib/bootstrap-autoload.php'; // Do not send mails of any kind during tests require 'vendor/email_message/email_message.php'; diff --git a/tests/jsonapi/_bootstrap.php b/tests/jsonapi/_bootstrap.php index 468d050dc99222b768d2eb6f1adc2af18b18ae0c..01538aae7d794295173f553f3ca8615776d544a8 100644 --- a/tests/jsonapi/_bootstrap.php +++ b/tests/jsonapi/_bootstrap.php @@ -28,6 +28,8 @@ $CACHING_ENABLE = false; date_default_timezone_set('Europe/Berlin'); require 'config.inc.php'; +require_once __DIR__ . '/../../lib/bootstrap-autoload.php'; + require 'lib/helpers.php'; require 'lib/functions.php'; @@ -58,6 +60,6 @@ class DB_Seminar extends DB_Sql } } -require_once __DIR__.'/../../composer/autoload.php'; +require_once __DIR__ . '/../../composer/autoload.php'; session_id("test-session");