]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Cache.php
Merge pull request #7754 from annando/aria
[friendica.git] / src / Core / Cache / Cache.php
1 <?php
2
3 namespace Friendica\Core\Cache;
4
5 /**
6  * Abstract class for common used functions
7  *
8  * Class AbstractCache
9  *
10  * @package Friendica\Core\Cache
11  */
12 abstract class Cache implements ICache
13 {
14         const TYPE_APCU      = 'apcu';
15         const TYPE_ARRAY     = 'array';
16         const TYPE_DATABASE  = 'database';
17         const TYPE_MEMCACHE  = 'memcache';
18         const TYPE_MEMCACHED = 'memcached';
19         const TYPE_REDIS     = 'redis';
20
21         const MONTH        = 2592000;
22         const WEEK         = 604800;
23         const DAY          = 86400;
24         const HOUR         = 3600;
25         const HALF_HOUR    = 1800;
26         const QUARTER_HOUR = 900;
27         const FIVE_MINUTES = 300;
28         const MINUTE       = 60;
29         const INFINITE     = 0;
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 }