2018-07-05 21:47:52 +02:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-07-05 21:47:52 +02:00
|
|
|
|
2020-01-18 15:41:19 +01:00
|
|
|
namespace Friendica\Core;
|
|
|
|
|
|
|
|
use Friendica\Core\Cache\ICache;
|
2018-07-05 21:47:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class for common used functions
|
|
|
|
*/
|
2020-01-18 15:41:19 +01:00
|
|
|
abstract class BaseCache implements ICache
|
2018-07-05 21:47:52 +02:00
|
|
|
{
|
2019-08-03 20:48:56 +02:00
|
|
|
/**
|
|
|
|
* @var string The hostname
|
|
|
|
*/
|
|
|
|
private $hostName;
|
|
|
|
|
|
|
|
public function __construct(string $hostName)
|
|
|
|
{
|
|
|
|
$this->hostName = $hostName;
|
|
|
|
}
|
|
|
|
|
2019-04-20 17:37:57 +02: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 20:48:56 +02:00
|
|
|
return $this->hostName;
|
2019-04-20 17:37:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 21:47:52 +02:00
|
|
|
/**
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param string $key The original key
|
|
|
|
* @return string The cache key used for the cache
|
|
|
|
* @throws \Exception
|
2018-07-05 21:47:52 +02:00
|
|
|
*/
|
2018-10-07 00:27:54 +02:00
|
|
|
protected function getCacheKey($key)
|
|
|
|
{
|
2019-04-21 12:24:48 +02:00
|
|
|
return $this->getPrefix() . ":" . $key;
|
2018-07-05 21:47:52 +02:00
|
|
|
}
|
2018-10-07 00:27:54 +02: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 20:48:56 +02:00
|
|
|
$value = preg_replace('/^' . $this->hostName . ':/', '', $value);
|
2018-10-07 00:27:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
sort($keys);
|
|
|
|
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-07 10:35:37 +02:00
|
|
|
* Filters the keys of an array with a given prefix
|
|
|
|
* Returns the filtered keys as an new array
|
2018-10-07 00:27:54 +02:00
|
|
|
*
|
2019-08-15 13:58:01 +02:00
|
|
|
* @param array $keys The keys, which should get filtered
|
2018-10-07 10:35:37 +02:00
|
|
|
* @param string|null $prefix The prefix (if null, all keys will get returned)
|
2018-10-07 00:27:54 +02:00
|
|
|
*
|
2018-10-07 10:35:37 +02:00
|
|
|
* @return array The filtered array with just the keys
|
2018-10-07 00:27:54 +02:00
|
|
|
*/
|
2019-08-15 13:58:01 +02:00
|
|
|
protected function filterArrayKeysByPrefix(array $keys, string $prefix = null)
|
2018-10-07 00:27:54 +02:00
|
|
|
{
|
|
|
|
if (empty($prefix)) {
|
2019-08-15 13:58:01 +02:00
|
|
|
return $keys;
|
2018-10-07 00:27:54 +02:00
|
|
|
} else {
|
|
|
|
$result = [];
|
|
|
|
|
2019-08-15 13:58:01 +02:00
|
|
|
foreach ($keys as $key) {
|
2018-10-07 00:27:54 +02:00
|
|
|
if (strpos($key, $prefix) === 0) {
|
|
|
|
array_push($result, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 22:01:33 +02:00
|
|
|
}
|