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