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

120 lines
2.6 KiB
PHP
Raw Normal View History

<?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/>.
*
*/
namespace Friendica\Core\Cache\Type;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
use Friendica\Core\Cache\Enum;
/**
* Implementation of the IMemoryCache mainly for testing purpose
*/
2021-10-26 21:44:29 +02:00
class ArrayCache extends AbstractCache implements ICanCacheInMemory
{
2021-10-26 21:44:29 +02:00
use CompareDeleteTrait;
/** @var array Array with the cached data */
2021-10-26 21:44:29 +02:00
protected $cachedData = [];
2018-09-26 04:52:32 +02:00
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function getAllKeys(?string $prefix = null): array
2018-09-26 04:52:32 +02:00
{
return $this->filterArrayKeysByPrefix(array_keys($this->cachedData), $prefix);
2018-09-26 04:52:32 +02:00
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function get(string $key)
{
if (isset($this->cachedData[$key])) {
return $this->cachedData[$key];
}
return null;
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function set(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
{
$this->cachedData[$key] = $value;
return true;
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function delete(string $key): bool
{
unset($this->cachedData[$key]);
return true;
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function clear(bool $outdated = true): bool
{
// Array doesn't support TTL so just don't delete something
if ($outdated) {
return true;
}
$this->cachedData = [];
return true;
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function add(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
{
if (isset($this->cachedData[$key])) {
return false;
} else {
return $this->set($key, $value, $ttl);
}
}
/**
* (@inheritdoc)
*/
2021-10-26 21:44:29 +02:00
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Enum\Duration::FIVE_MINUTES): bool
{
if ($this->get($key) === $oldValue) {
return $this->set($key, $newValue);
} else {
return false;
}
}
2019-08-04 16:13:53 +02:00
/**
* {@inheritDoc}
*/
2021-10-26 21:44:29 +02:00
public function getName(): string
{
2021-10-26 21:44:29 +02:00
return Enum\Type::ARRAY;
}
2018-07-05 07:59:56 +02:00
}