<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
namespace Friendica\Core\Cache\Type;
-use Exception;
use Friendica\Core\Cache\Enum\Duration;
-use Friendica\Core\Cache\IMemoryCache;
-use Friendica\Core\Cache\Type\TraitCompareDelete;
-use Friendica\Core\Cache\Type\TraitCompareSet;
+use Friendica\Core\Cache\Capability\ICanCacheInMemory;
use Friendica\Core\Cache\Enum\Type;
+use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
/**
* APCu Cache.
*/
-class APCuCache extends BaseCache implements IMemoryCache
+class APCuCache extends AbstractCache implements ICanCacheInMemory
{
- use TraitCompareSet;
- use TraitCompareDelete;
+ use CompareSetTrait;
+ use CompareDeleteTrait;
/**
- * @throws Exception
+ * @param string $hostname
+ *
+ * @throws InvalidCacheDriverException
*/
public function __construct(string $hostname)
{
if (!self::isAvailable()) {
- throw new Exception('APCu is not available.');
+ throw new InvalidCacheDriverException('APCu is not available.');
}
parent::__construct($hostname);
/**
* (@inheritdoc)
*/
- public function getAllKeys($prefix = null)
+ public function getAllKeys(?string $prefix = null): array
{
- $ns = $this->getCacheKey($prefix);
+ $ns = $this->getCacheKey($prefix ?? '');
$ns = preg_quote($ns, '/');
if (class_exists('\APCIterator')) {
/**
* (@inheritdoc)
*/
- public function get($key)
+ public function get(string $key)
{
- $return = null;
- $cachekey = $this->getCacheKey($key);
+ $cacheKey = $this->getCacheKey($key);
- $cached = apcu_fetch($cachekey, $success);
+ $cached = apcu_fetch($cacheKey, $success);
if (!$success) {
return null;
}
// We also check if the db entry is a serialized
// boolean 'false' value (which we want to return).
if ($cached === serialize(false) || $value !== false) {
- $return = $value;
+ return $value;
}
- return $return;
+ return null;
}
/**
* (@inheritdoc)
*/
- public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
+ public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
{
- $cachekey = $this->getCacheKey($key);
+ $cacheKey = $this->getCacheKey($key);
$cached = serialize($value);
if ($ttl > 0) {
return apcu_store(
- $cachekey,
+ $cacheKey,
$cached,
$ttl
);
} else {
return apcu_store(
- $cachekey,
+ $cacheKey,
$cached
);
}
/**
* (@inheritdoc)
*/
- public function delete($key)
+ public function delete(string $key): bool
{
- $cachekey = $this->getCacheKey($key);
- return apcu_delete($cachekey);
+ $cacheKey = $this->getCacheKey($key);
+ return apcu_delete($cacheKey);
}
/**
* (@inheritdoc)
*/
- public function clear($outdated = true)
+ public function clear(bool $outdated = true): bool
{
if ($outdated) {
return true;
/**
* (@inheritdoc)
*/
- public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
+ public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
{
- $cachekey = $this->getCacheKey($key);
- $cached = serialize($value);
+ $cacheKey = $this->getCacheKey($key);
+ $cached = serialize($value);
- return apcu_add($cachekey, $cached);
+ return apcu_add($cacheKey, $cached);
}
- public static function isAvailable()
+ public static function isAvailable(): bool
{
if (!extension_loaded('apcu')) {
return false;
/**
* {@inheritDoc}
*/
- public function getName()
+ public function getName(): string
{
return Type::APCU;
}