2018-07-05 19:47:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
use Friendica\BaseObject;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class for common used functions
|
|
|
|
*
|
|
|
|
* Class AbstractCacheDriver
|
|
|
|
*
|
|
|
|
* @package Friendica\Core\Cache
|
|
|
|
*/
|
2018-07-05 19:54:20 +00:00
|
|
|
abstract class AbstractCacheDriver extends BaseObject
|
2018-07-05 19:47:52 +00:00
|
|
|
{
|
2019-04-20 15:37:57 +00: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
|
|
|
|
return self::getApp()->getHostName();
|
|
|
|
}
|
|
|
|
|
2018-07-05 19:47:52 +00:00
|
|
|
/**
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param string $key The original key
|
|
|
|
* @return string The cache key used for the cache
|
|
|
|
* @throws \Exception
|
2018-07-05 19:47:52 +00:00
|
|
|
*/
|
2018-10-06 22:27:54 +00:00
|
|
|
protected function getCacheKey($key)
|
|
|
|
{
|
2019-04-20 15:37:57 +00:00
|
|
|
return $this->getPrefix() . ":" . $key;
|
2018-07-05 19:47:52 +00:00
|
|
|
}
|
2018-10-06 22:27:54 +00: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) {
|
2018-10-09 17:58:58 +00:00
|
|
|
$value = preg_replace('/^' . self::getApp()->getHostName() . ':/', '', $value);
|
2018-10-06 22:27:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
sort($keys);
|
|
|
|
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 08:35:37 +00:00
|
|
|
* Filters the keys of an array with a given prefix
|
|
|
|
* Returns the filtered keys as an new array
|
2018-10-06 22:27:54 +00:00
|
|
|
*
|
2018-10-07 08:35:37 +00: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 22:27:54 +00:00
|
|
|
*
|
2018-10-07 08:35:37 +00:00
|
|
|
* @return array The filtered array with just the keys
|
2018-10-06 22:27:54 +00:00
|
|
|
*/
|
2018-10-07 08:35:37 +00:00
|
|
|
protected function filterArrayKeysByPrefix($array, $prefix = null)
|
2018-10-06 22:27:54 +00:00
|
|
|
{
|
|
|
|
if (empty($prefix)) {
|
2018-10-07 08:35:37 +00:00
|
|
|
return array_keys($array);
|
2018-10-06 22:27:54 +00:00
|
|
|
} else {
|
|
|
|
$result = [];
|
|
|
|
|
2018-10-07 08:35:37 +00:00
|
|
|
foreach (array_keys($array) as $key) {
|
2018-10-06 22:27:54 +00:00
|
|
|
if (strpos($key, $prefix) === 0) {
|
|
|
|
array_push($result, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-05 20:01:33 +00:00
|
|
|
}
|