aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/cache/KeyTrait.php
diff options
context:
space:
mode:
authorMoritz Strohm <strohm@data-quest.de>2024-05-14 07:22:47 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2024-05-14 07:22:47 +0000
commit40bdfaf4415ff9f46e63435fc5e61049649802f2 (patch)
treef3ae85d9edaef23db555a829571ff968c21f9af5 /lib/classes/cache/KeyTrait.php
parent636039e25ccc6c33ae758bdb3ddfca4f68327948 (diff)
made Stud.IP cache compatible with PSR-6, re #3701
Merge request studip/studip!2570
Diffstat (limited to 'lib/classes/cache/KeyTrait.php')
-rw-r--r--lib/classes/cache/KeyTrait.php31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/classes/cache/KeyTrait.php b/lib/classes/cache/KeyTrait.php
new file mode 100644
index 0000000..021aaba
--- /dev/null
+++ b/lib/classes/cache/KeyTrait.php
@@ -0,0 +1,31 @@
+<?php
+namespace Studip\Cache;
+
+/**
+ * Trait for unique cache hashes per key for each system based on db configuration which should
+ * be sufficient to eliminate cache mishaps.
+ *
+ * @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
+ * @license GPL2 or any later version
+ * @package studip
+ * @subpackage cache
+ * @since Stud.IP 5.0
+ */
+trait KeyTrait
+{
+ protected ?string $cache_prefix = null;
+
+ /**
+ * Returns a prefix cache key based on db configuration.
+ *
+ * @param string $offset
+ * @return string
+ */
+ protected function getCacheKey(string $offset): string
+ {
+ if ($this->cache_prefix === null) {
+ $this->cache_prefix = md5("{$GLOBALS['DB_STUDIP_HOST']}|{$GLOBALS['DB_STUDIP_DATABASE']}");
+ }
+ return "$this->cache_prefix/$offset";
+ }
+}