2018-07-04 23:37:22 +02:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
2020-01-18 15:41:19 +01:00
|
|
|
use Friendica\Core\BaseCache;
|
|
|
|
|
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
|
|
|
*/
|
2020-01-18 15:41:19 +01:00
|
|
|
class ArrayCache extends BaseCache implements IMemoryCache
|
2018-07-04 23:37:22 +02:00
|
|
|
{
|
|
|
|
use TraitCompareDelete;
|
|
|
|
|
|
|
|
/** @var array Array with the cached data */
|
|
|
|
protected $cachedData = array();
|
|
|
|
|
2018-09-26 04:52:32 +02:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-10-07 00:27:54 +02:00
|
|
|
public function getAllKeys($prefix = null)
|
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)
|
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
if (isset($this->cachedData[$key])) {
|
|
|
|
return $this->cachedData[$key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 15:41:19 +01:00
|
|
|
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-07-04 23:37:22 +02:00
|
|
|
{
|
|
|
|
$this->cachedData[$key] = $value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
unset($this->cachedData[$key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-07-07 19:46:16 +02:00
|
|
|
public function clear($outdated = true)
|
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)
|
|
|
|
*/
|
2020-01-18 15:41:19 +01:00
|
|
|
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-07-04 23:37:22 +02:00
|
|
|
{
|
|
|
|
if (isset($this->cachedData[$key])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $this->set($key, $value, $ttl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 15:41:19 +01:00
|
|
|
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
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}
|
|
|
|
*/
|
|
|
|
public function getName()
|
2019-08-04 15:42:39 +02:00
|
|
|
{
|
2020-01-18 15:41:19 +01:00
|
|
|
return Type::ARRAY;
|
2019-08-04 15:42:39 +02:00
|
|
|
}
|
2018-07-05 07:59:56 +02:00
|
|
|
}
|