aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2024-11-06 09:47:33 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2024-11-06 09:47:33 +0000
commit79479dd3524c0ff27919749bfbe04455b534484b (patch)
tree61cad62e61be06145a8ed89fce8a2f35d76f6b96
parent807bc5928408345284f0ce602d1682e3d1f4be76 (diff)
allow cache settings to be configured even if caching is disabled, fixes #4806
Closes #4806 Merge request studip/studip!3597
-rw-r--r--app/controllers/admin/cache.php20
-rw-r--r--lib/bootstrap-autoload.php1
-rw-r--r--lib/classes/cache/Factory.php60
-rw-r--r--resources/vue/components/CacheAdministration.vue8
-rw-r--r--tests/functional/_bootstrap.php1
-rw-r--r--tests/jsonapi/_bootstrap.php4
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 @@
<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 e8a911d..cdc9db1 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 468d050..01538aa 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");