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