]> git.mxchange.org Git - friendica.git/blob - src/Core/BaseCache.php
Remove confirm template obsolete uses (except for contacts)
[friendica.git] / src / Core / BaseCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\Core\Cache\ICache;
25
26 /**
27  * Abstract class for common used functions
28  */
29 abstract class BaseCache implements ICache
30 {
31         /**
32          * @var string The hostname
33          */
34         private $hostName;
35
36         public function __construct(string $hostName)
37         {
38                 $this->hostName = $hostName;
39         }
40
41         /**
42          * Returns the prefix (to avoid namespace conflicts)
43          *
44          * @return string
45          * @throws \Exception
46          */
47         protected function getPrefix()
48         {
49                 // We fetch with the hostname as key to avoid problems with other applications
50                 return $this->hostName;
51         }
52
53         /**
54          * @param string $key The original key
55          * @return string        The cache key used for the cache
56          * @throws \Exception
57          */
58         protected function getCacheKey($key)
59         {
60                 return $this->getPrefix() . ":" . $key;
61         }
62
63         /**
64          * @param array $keys   A list of cached keys
65          * @return array        A list of original keys
66          */
67         protected function getOriginalKeys($keys)
68         {
69                 if (empty($keys)) {
70                         return [];
71                 } else {
72                         // Keys are prefixed with the node hostname, let's remove it
73                         array_walk($keys, function (&$value) {
74                                 $value = preg_replace('/^' . $this->hostName . ':/', '', $value);
75                         });
76
77                         sort($keys);
78
79                         return $keys;
80                 }
81         }
82
83         /**
84          * Filters the keys of an array with a given prefix
85          * Returns the filtered keys as an new array
86          *
87          * @param array $keys The keys, which should get filtered
88          * @param string|null $prefix The prefix (if null, all keys will get returned)
89          *
90          * @return array The filtered array with just the keys
91          */
92         protected function filterArrayKeysByPrefix(array $keys, string $prefix = null)
93         {
94                 if (empty($prefix)) {
95                         return $keys;
96                 } else {
97                         $result = [];
98
99                         foreach ($keys as $key) {
100                                 if (strpos($key, $prefix) === 0) {
101                                         array_push($result, $key);
102                                 }
103                         }
104
105                         return $result;
106                 }
107         }
108 }