]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/AbstractCacheDriver.php
friendica-5847 Console Cache List command doesn't work
[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          */
20         protected function getCacheKey($key)
21         {
22                 // We fetch with the hostname as key to avoid problems with other applications
23                 return self::getApp()->get_hostname() . ":" . $key;
24         }
25
26         /**
27          * @param array $keys   A list of cached keys
28          * @return array        A list of original keys
29          */
30         protected function getOriginalKeys($keys)
31         {
32                 if (empty($keys)) {
33                         return [];
34                 } else {
35                         // Keys are prefixed with the node hostname, let's remove it
36                         array_walk($keys, function (&$value) {
37                                 $value = preg_replace('/^' . self::getApp()->get_hostname() . ':/', '', $value);
38                         });
39
40                         sort($keys);
41
42                         return $keys;
43                 }
44         }
45
46         /**
47          * Filters a list for a given prefix
48          *
49          * @param array $list the list
50          * @param string|null $prefix the prefix
51          *
52          * @return array the filtered list
53          */
54         protected function filterPrefix($list, $prefix = null)
55         {
56                 if (empty($prefix)) {
57                         return array_keys($list);
58                 } else {
59                         $result = [];
60
61                         foreach (array_keys($list) as $key) {
62                                 if (strpos($key, $prefix) === 0) {
63                                         array_push($result, $key);
64                                 }
65                         }
66
67                         return $result;
68                 }
69
70         }
71 }