]> git.mxchange.org Git - friendica.git/blob - src/Core/Cache/Cache.php
4e24246e886e584f4c2d24b16b1c5aea2a5012ad
[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          * Force each Cache implementation to define the ToString method
33          *
34          * @return string
35          */
36         abstract function __toString();
37
38         /**
39          * @var string The hostname
40          */
41         private $hostName;
42
43         public function __construct(string $hostName)
44         {
45                 $this->hostName = $hostName;
46         }
47
48         /**
49          * Returns the prefix (to avoid namespace conflicts)
50          *
51          * @return string
52          * @throws \Exception
53          */
54         protected function getPrefix()
55         {
56                 // We fetch with the hostname as key to avoid problems with other applications
57                 return $this->hostName;
58         }
59
60         /**
61          * @param string $key The original key
62          * @return string        The cache key used for the cache
63          * @throws \Exception
64          */
65         protected function getCacheKey($key)
66         {
67                 return $this->getPrefix() . ":" . $key;
68         }
69
70         /**
71          * @param array $keys   A list of cached keys
72          * @return array        A list of original keys
73          */
74         protected function getOriginalKeys($keys)
75         {
76                 if (empty($keys)) {
77                         return [];
78                 } else {
79                         // Keys are prefixed with the node hostname, let's remove it
80                         array_walk($keys, function (&$value) {
81                                 $value = preg_replace('/^' . $this->hostName . ':/', '', $value);
82                         });
83
84                         sort($keys);
85
86                         return $keys;
87                 }
88         }
89
90         /**
91          * Filters the keys of an array with a given prefix
92          * Returns the filtered keys as an new array
93          *
94          * @param array $array The array, which should get filtered
95          * @param string|null $prefix The prefix (if null, all keys will get returned)
96          *
97          * @return array The filtered array with just the keys
98          */
99         protected function filterArrayKeysByPrefix($array, $prefix = null)
100         {
101                 if (empty($prefix)) {
102                         return array_keys($array);
103                 } else {
104                         $result = [];
105
106                         foreach (array_keys($array) as $key) {
107                                 if (strpos($key, $prefix) === 0) {
108                                         array_push($result, $key);
109                                 }
110                         }
111
112                         return $result;
113                 }
114         }
115 }