friendica/src/Core/Cache/Type/APCuCache.php

185 lines
4.0 KiB
PHP
Raw Normal View History

2019-04-20 17:37:57 +02:00
<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01: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 17:37:57 +02:00
namespace Friendica\Core\Cache\Type;
2019-04-20 17:37:57 +02:00
use Friendica\Core\Cache\Enum\Duration;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
use Friendica\Core\Cache\Enum\Type;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
2019-04-20 17:37:57 +02:00
/**
* APCu Cache.
2019-04-20 17:37:57 +02:00
*/
2021-10-26 21:44:29 +02:00
class APCuCache extends AbstractCache implements ICanCacheInMemory
2019-04-20 17:37:57 +02:00
{
2021-10-26 21:44:29 +02:00
use CompareSetTrait;
use CompareDeleteTrait;
2019-04-20 17:37:57 +02:00
/**
2021-10-26 21:44:29 +02:00
* @param string $hostname
*
* @throws InvalidCacheDriverException
2019-04-20 17:37:57 +02:00
*/
public function __construct(string $hostname)
2019-04-20 17:37:57 +02:00
{
if (!self::isAvailable()) {
2021-10-26 21:44:29 +02:00
throw new InvalidCacheDriverException('APCu is not available.');
2019-04-20 17:37:57 +02:00
}
parent::__construct($hostname);
2019-04-20 17:37:57 +02:00
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function getAllKeys(?string $prefix = null): array
2019-04-20 17:37:57 +02:00
{
2021-10-26 21:44:29 +02:00
$ns = $this->getCacheKey($prefix ?? '');
2019-04-20 17:37:57 +02: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 21:44:29 +02:00
public function get(string $key)
2019-04-20 17:37:57 +02:00
{
2021-10-26 21:44:29 +02:00
$cacheKey = $this->getCacheKey($key);
2019-04-20 17:37:57 +02:00
2021-10-26 21:44:29 +02:00
$cached = apcu_fetch($cacheKey, $success);
2019-04-20 17:37:57 +02: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 21:44:29 +02:00
return $value;
2019-04-20 17:37:57 +02:00
}
2021-10-26 21:44:29 +02:00
return null;
2019-04-20 17:37:57 +02:00
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
2019-04-20 17:37:57 +02:00
{
2021-10-26 21:44:29 +02:00
$cacheKey = $this->getCacheKey($key);
2019-04-20 17:37:57 +02:00
$cached = serialize($value);
if ($ttl > 0) {
return apcu_store(
2021-10-26 21:44:29 +02:00
$cacheKey,
2019-04-20 17:37:57 +02:00
$cached,
$ttl
);
} else {
return apcu_store(
2021-10-26 21:44:29 +02:00
$cacheKey,
2019-04-20 17:37:57 +02:00
$cached
);
}
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function delete(string $key): bool
2019-04-20 17:37:57 +02:00
{
2021-10-26 21:44:29 +02:00
$cacheKey = $this->getCacheKey($key);
return apcu_delete($cacheKey);
2019-04-20 17:37:57 +02:00
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function clear(bool $outdated = true): bool
2019-04-20 17:37:57 +02: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 21:44:29 +02:00
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
2019-04-20 17:37:57 +02:00
{
2021-10-26 21:44:29 +02:00
$cacheKey = $this->getCacheKey($key);
$cached = serialize($value);
2019-04-20 17:37:57 +02:00
2021-10-26 21:44:29 +02:00
return apcu_add($cacheKey, $cached);
2019-04-20 17:37:57 +02:00
}
2021-10-26 21:44:29 +02:00
public static function isAvailable(): bool
2019-04-20 17:37:57 +02: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;
}
2019-08-04 16:13:53 +02:00
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function getName(): string
{
return Type::APCU;
}
2019-04-20 17:37:57 +02:00
}