]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/AbstractCacheDriver.php
Improve redis configuration
[friendica.git] / src / Core / Cache / AbstractCacheDriver.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4 use Friendica\BaseObject;
5
6
7 /**
8  * Abstract class for common used functions
9  *
10  * Class AbstractCacheDriver
11  *
12  * @package Friendica\Core\Cache
13  */
14 abstract class AbstractCacheDriver extends BaseObject
15 {
16         /**
17          * @param string $key The original key
18          * @return string        The cache key used for the cache
19          * @throws \Exception
20          */
21         protected function getCacheKey($key)
22         {
23                 // We fetch with the hostname as key to avoid problems with other applications
24                 return self::getApp()->getHostName() . ":" . $key;
25         }
26
27         /**
28          * @param array $keys   A list of cached keys
29          * @return array        A list of original keys
30          */
31         protected function getOriginalKeys($keys)
32         {
33                 if (empty($keys)) {
34                         return [];
35                 } else {
36                         // Keys are prefixed with the node hostname, let's remove it
37                         array_walk($keys, function (&$value) {
38                                 $value = preg_replace('/^' . self::getApp()->getHostName() . ':/', '', $value);
39                         });
40
41                         sort($keys);
42
43                         return $keys;
44                 }
45         }
46
47         /**
48          * Filters the keys of an array with a given prefix
49          * Returns the filtered keys as an new array
50          *
51          * @param array $array The array, which should get filtered
52          * @param string|null $prefix The prefix (if null, all keys will get returned)
53          *
54          * @return array The filtered array with just the keys
55          */
56         protected function filterArrayKeysByPrefix($array, $prefix = null)
57         {
58                 if (empty($prefix)) {
59                         return array_keys($array);
60                 } else {
61                         $result = [];
62
63                         foreach (array_keys($array) as $key) {
64                                 if (strpos($key, $prefix) === 0) {
65                                         array_push($result, $key);
66                                 }
67                         }
68
69                         return $result;
70                 }
71
72         }
73 }