]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/AbstractCacheDriver.php
Merge branch '2019.06-rc'
[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          * Returns the prefix (to avoid namespace conflicts)
18          *
19          * @return string
20          * @throws \Exception
21          */
22         protected function getPrefix()
23         {
24                 // We fetch with the hostname as key to avoid problems with other applications
25                 return self::getApp()->getHostName();
26         }
27
28         /**
29          * @param string $key The original key
30          * @return string        The cache key used for the cache
31          * @throws \Exception
32          */
33         protected function getCacheKey($key)
34         {
35                 return $this->getPrefix() . ":" . $key;
36         }
37
38         /**
39          * @param array $keys   A list of cached keys
40          * @return array        A list of original keys
41          */
42         protected function getOriginalKeys($keys)
43         {
44                 if (empty($keys)) {
45                         return [];
46                 } else {
47                         // Keys are prefixed with the node hostname, let's remove it
48                         array_walk($keys, function (&$value) {
49                                 $value = preg_replace('/^' . self::getApp()->getHostName() . ':/', '', $value);
50                         });
51
52                         sort($keys);
53
54                         return $keys;
55                 }
56         }
57
58         /**
59          * Filters the keys of an array with a given prefix
60          * Returns the filtered keys as an new array
61          *
62          * @param array $array The array, which should get filtered
63          * @param string|null $prefix The prefix (if null, all keys will get returned)
64          *
65          * @return array The filtered array with just the keys
66          */
67         protected function filterArrayKeysByPrefix($array, $prefix = null)
68         {
69                 if (empty($prefix)) {
70                         return array_keys($array);
71                 } else {
72                         $result = [];
73
74                         foreach (array_keys($array) as $key) {
75                                 if (strpos($key, $prefix) === 0) {
76                                         array_push($result, $key);
77                                 }
78                         }
79
80                         return $result;
81                 }
82
83         }
84 }