3 namespace Friendica\Core\Cache;
6 * Abstract class for common used functions
10 * @package Friendica\Core\Cache
12 abstract class Cache implements ICache
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';
21 const MONTH = 2592000;
25 const HALF_HOUR = 1800;
26 const QUARTER_HOUR = 900;
27 const FIVE_MINUTES = 300;
32 * Force each Cache implementation to define the ToString method
36 abstract function __toString();
39 * @var string The hostname
43 public function __construct(string $hostName)
45 $this->hostName = $hostName;
49 * Returns the prefix (to avoid namespace conflicts)
54 protected function getPrefix()
56 // We fetch with the hostname as key to avoid problems with other applications
57 return $this->hostName;
61 * @param string $key The original key
62 * @return string The cache key used for the cache
65 protected function getCacheKey($key)
67 return $this->getPrefix() . ":" . $key;
71 * @param array $keys A list of cached keys
72 * @return array A list of original keys
74 protected function getOriginalKeys($keys)
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);
91 * Filters the keys of an array with a given prefix
92 * Returns the filtered keys as an new array
94 * @param array $array The array, which should get filtered
95 * @param string|null $prefix The prefix (if null, all keys will get returned)
97 * @return array The filtered array with just the keys
99 protected function filterArrayKeysByPrefix($array, $prefix = null)
101 if (empty($prefix)) {
102 return array_keys($array);
106 foreach (array_keys($array) as $key) {
107 if (strpos($key, $prefix) === 0) {
108 array_push($result, $key);