2019-04-20 15:37:57 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2023-01-01 14:36:24 +00:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-09 14:45:36 +00:00
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-04-20 15:37:57 +00:00
|
|
|
|
2021-10-23 08:49:27 +00:00
|
|
|
namespace Friendica\Core\Cache\Type;
|
2019-04-20 15:37:57 +00:00
|
|
|
|
2021-10-23 08:49:27 +00:00
|
|
|
use Friendica\Core\Cache\Enum\Duration;
|
2021-10-26 19:44:29 +00:00
|
|
|
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
|
|
|
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
2019-04-20 15:37:57 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-04 08:26:53 +00:00
|
|
|
* APCu Cache.
|
2019-04-20 15:37:57 +00:00
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
class APCuCache extends AbstractCache implements ICanCacheInMemory
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
2023-07-26 20:42:40 +00:00
|
|
|
const NAME = 'apcu';
|
2023-07-22 21:57:38 +00:00
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
use CompareSetTrait;
|
|
|
|
use CompareDeleteTrait;
|
2019-04-20 15:37:57 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-26 19:44:29 +00:00
|
|
|
* @throws InvalidCacheDriverException
|
2019-04-20 15:37:57 +00:00
|
|
|
*/
|
2019-08-03 18:48:56 +00:00
|
|
|
public function __construct(string $hostname)
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
|
|
|
if (!self::isAvailable()) {
|
2021-10-26 19:44:29 +00:00
|
|
|
throw new InvalidCacheDriverException('APCu is not available.');
|
2019-04-20 15:37:57 +00:00
|
|
|
}
|
2019-08-03 18:48:56 +00:00
|
|
|
|
|
|
|
parent::__construct($hostname);
|
2019-04-20 15:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function getAllKeys(?string $prefix = null): array
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$ns = $this->getCacheKey($prefix ?? '');
|
2019-04-20 15:37:57 +00:00
|
|
|
$ns = preg_quote($ns, '/');
|
|
|
|
|
|
|
|
if (class_exists('\APCIterator')) {
|
|
|
|
$iterator = new \APCIterator('user', '/^' . $ns. '/', APC_ITER_KEY);
|
|
|
|
} else {
|
|
|
|
$iterator = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = [];
|
|
|
|
foreach ($iterator as $item) {
|
|
|
|
array_push($keys, $item['key']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getOriginalKeys($keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function get(string $key)
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
2019-04-20 15:37:57 +00:00
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
$cached = apcu_fetch($cacheKey, $success);
|
2019-04-20 15:37:57 +00:00
|
|
|
if (!$success) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = unserialize($cached);
|
|
|
|
|
|
|
|
// Only return a value if the serialized value is valid.
|
|
|
|
// We also check if the db entry is a serialized
|
|
|
|
// boolean 'false' value (which we want to return).
|
|
|
|
if ($cached === serialize(false) || $value !== false) {
|
2021-10-26 19:44:29 +00:00
|
|
|
return $value;
|
2019-04-20 15:37:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
return null;
|
2019-04-20 15:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
2019-04-20 15:37:57 +00:00
|
|
|
|
|
|
|
$cached = serialize($value);
|
|
|
|
|
|
|
|
if ($ttl > 0) {
|
|
|
|
return apcu_store(
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey,
|
2019-04-20 15:37:57 +00:00
|
|
|
$cached,
|
|
|
|
$ttl
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return apcu_store(
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey,
|
2019-04-20 15:37:57 +00:00
|
|
|
$cached
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function delete(string $key): bool
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
|
|
|
return apcu_delete($cacheKey);
|
2019-04-20 15:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function clear(bool $outdated = true): bool
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
|
|
|
if ($outdated) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$prefix = $this->getPrefix();
|
|
|
|
$prefix = preg_quote($prefix, '/');
|
|
|
|
|
|
|
|
if (class_exists('\APCIterator')) {
|
|
|
|
$iterator = new \APCIterator('user', '/^' . $prefix . '/', APC_ITER_KEY);
|
|
|
|
} else {
|
|
|
|
$iterator = new \APCUIterator('/^' . $prefix . '/', APC_ITER_KEY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return apcu_delete($iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
|
|
|
$cached = serialize($value);
|
2019-04-20 15:37:57 +00:00
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
return apcu_add($cacheKey, $cached);
|
2019-04-20 15:37:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
public static function isAvailable(): bool
|
2019-04-20 15:37:57 +00:00
|
|
|
{
|
|
|
|
if (!extension_loaded('apcu')) {
|
|
|
|
return false;
|
|
|
|
} elseif (!ini_get('apc.enabled') && !ini_get('apc.enable_cli')) {
|
|
|
|
return false;
|
|
|
|
} elseif (
|
|
|
|
version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 &&
|
|
|
|
version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|