aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/cache/KeyTrait.php
diff options
context:
space:
mode:
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";
+ }
+}