aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/cache/KeyTrait.php
blob: 021aaba3966574f13fad4428e6cf71a68fcd0efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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";
    }
}