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 * @var string The hostname
36 public function __construct(string $hostName)
38 $this->hostName = $hostName;
42 * Returns the prefix (to avoid namespace conflicts)
47 protected function getPrefix()
49 // We fetch with the hostname as key to avoid problems with other applications
50 return $this->hostName;
54 * @param string $key The original key
55 * @return string The cache key used for the cache
58 protected function getCacheKey($key)
60 return $this->getPrefix() . ":" . $key;
64 * @param array $keys A list of cached keys
65 * @return array A list of original keys
67 protected function getOriginalKeys($keys)
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);
84 * Filters the keys of an array with a given prefix
85 * Returns the filtered keys as an new array
87 * @param array $keys The keys, which should get filtered
88 * @param string|null $prefix The prefix (if null, all keys will get returned)
90 * @return array The filtered array with just the keys
92 protected function filterArrayKeysByPrefix(array $keys, string $prefix = null)
99 foreach ($keys as $key) {
100 if (strpos($key, $prefix) === 0) {
101 array_push($result, $key);