2018-07-05 15:47:52 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class for common used functions
|
|
|
|
*
|
2019-08-04 04:26:53 -04:00
|
|
|
* Class AbstractCache
|
2018-07-05 15:47:52 -04:00
|
|
|
*
|
|
|
|
* @package Friendica\Core\Cache
|
|
|
|
*/
|
2019-08-04 04:26:53 -04:00
|
|
|
abstract class AbstractCache implements ICache
|
2018-07-05 15:47:52 -04:00
|
|
|
{
|
2019-08-04 09:42:39 -04:00
|
|
|
const TYPE_APCU = 'apcu';
|
|
|
|
const TYPE_ARRAY = 'array';
|
|
|
|
const TYPE_DATABASE = 'database';
|
|
|
|
const TYPE_MEMCACHE = 'memcache';
|
|
|
|
const TYPE_MEMCACHED = 'memcached';
|
|
|
|
const TYPE_REDIS = 'redis';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force each Cache implementation to define the ToString method
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract function __toString();
|
|
|
|
|
2019-08-03 14:48:56 -04:00
|
|
|
/**
|
|
|
|
* @var string The hostname
|
|
|
|
*/
|
|
|
|
private $hostName;
|
|
|
|
|
|
|
|
public function __construct(string $hostName)
|
|
|
|
{
|
|
|
|
$this->hostName = $hostName;
|
|
|
|
}
|
|
|
|
|
2019-04-20 11:37:57 -04:00
|
|
|
/**
|
|
|
|
* Returns the prefix (to avoid namespace conflicts)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function getPrefix()
|
|
|
|
{
|
|
|
|
// We fetch with the hostname as key to avoid problems with other applications
|
2019-08-03 14:48:56 -04:00
|
|
|
return $this->hostName;
|
2019-04-20 11:37:57 -04:00
|
|
|
}
|
|
|
|
|
2018-07-05 15:47:52 -04:00
|
|
|
/**
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param string $key The original key
|
|
|
|
* @return string The cache key used for the cache
|
|
|
|
* @throws \Exception
|
2018-07-05 15:47:52 -04:00
|
|
|
*/
|
2018-10-06 18:27:54 -04:00
|
|
|
protected function getCacheKey($key)
|
|
|
|
{
|
2019-04-21 06:24:48 -04:00
|
|
|
return $this->getPrefix() . ":" . $key;
|
2018-07-05 15:47:52 -04:00
|
|
|
}
|
2018-10-06 18:27:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $keys A list of cached keys
|
|
|
|
* @return array A list of original keys
|
|
|
|
*/
|
|
|
|
protected function getOriginalKeys($keys)
|
|
|
|
{
|
|
|
|
if (empty($keys)) {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
// Keys are prefixed with the node hostname, let's remove it
|
|
|
|
array_walk($keys, function (&$value) {
|
2019-08-03 14:48:56 -04:00
|
|
|
$value = preg_replace('/^' . $this->hostName . ':/', '', $value);
|
2018-10-06 18:27:54 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
sort($keys);
|
|
|
|
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 04:35:37 -04:00
|
|
|
* Filters the keys of an array with a given prefix
|
|
|
|
* Returns the filtered keys as an new array
|
2018-10-06 18:27:54 -04:00
|
|
|
*
|
2018-10-07 04:35:37 -04:00
|
|
|
* @param array $array The array, which should get filtered
|
|
|
|
* @param string|null $prefix The prefix (if null, all keys will get returned)
|
2018-10-06 18:27:54 -04:00
|
|
|
*
|
2018-10-07 04:35:37 -04:00
|
|
|
* @return array The filtered array with just the keys
|
2018-10-06 18:27:54 -04:00
|
|
|
*/
|
2018-10-07 04:35:37 -04:00
|
|
|
protected function filterArrayKeysByPrefix($array, $prefix = null)
|
2018-10-06 18:27:54 -04:00
|
|
|
{
|
|
|
|
if (empty($prefix)) {
|
2018-10-07 04:35:37 -04:00
|
|
|
return array_keys($array);
|
2018-10-06 18:27:54 -04:00
|
|
|
} else {
|
|
|
|
$result = [];
|
|
|
|
|
2018-10-07 04:35:37 -04:00
|
|
|
foreach (array_keys($array) as $key) {
|
2018-10-06 18:27:54 -04:00
|
|
|
if (strpos($key, $prefix) === 0) {
|
|
|
|
array_push($result, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 16:01:33 -04:00
|
|
|
}
|