]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Type/AbstractCache.php
Move Cache to strategies
[friendica.git] / src / Core / Cache / Type / AbstractCache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Cache\Type;
23
24 use Friendica\Core\Cache\Capability\ICanCache;
25
26 /**
27  * Abstract class for common used functions
28  */
29 abstract class AbstractCache implements ICanCache
30 {
31         public static $NAME = '';
32
33         /**
34          * @var string The hostname
35          */
36         private $hostName;
37
38         public function __construct(string $hostName)
39         {
40                 $this->hostName = $hostName;
41         }
42
43         /**
44          * Returns the prefix (to avoid namespace conflicts)
45          *
46          * @return string
47          */
48         protected function getPrefix(): string
49         {
50                 // We fetch with the hostname as key to avoid problems with other applications
51                 return $this->hostName;
52         }
53
54         /**
55          * @param string $key The original key
56          *
57          * @return string        The cache key used for the cache
58          */
59         protected function getCacheKey(string $key): string
60         {
61                 return $this->getPrefix() . ":" . $key;
62         }
63
64         /**
65          * @param string[] $keys A list of cached keys
66          *
67          * @return string[] A list of original keys
68          */
69         protected function getOriginalKeys(array $keys): array
70         {
71                 if (empty($keys)) {
72                         return [];
73                 } else {
74                         // Keys are prefixed with the node hostname, let's remove it
75                         array_walk($keys, function (&$value) {
76                                 $value = preg_replace('/^' . $this->hostName . ':/', '', $value);
77                         });
78
79                         sort($keys);
80
81                         return $keys;
82                 }
83         }
84
85         /**
86          * Filters the keys of an array with a given prefix
87          * Returns the filtered keys as an new array
88          *
89          * @param string[]    $keys   The keys, which should get filtered
90          * @param string|null $prefix The prefix (if null, all keys will get returned)
91          *
92          * @return string[] The filtered array with just the keys
93          */
94         protected function filterArrayKeysByPrefix(array $keys, string $prefix = null): array
95         {
96                 if (empty($prefix)) {
97                         return $keys;
98                 } else {
99                         $result = [];
100
101                         foreach ($keys as $key) {
102                                 if (strpos($key, $prefix) === 0) {
103                                         array_push($result, $key);
104                                 }
105                         }
106
107                         return $result;
108                 }
109         }
110
111         /** {@inheritDoc} */
112         public function getName(): string
113         {
114                 return static::$NAME;
115         }
116 }