2018-07-04 21:37:22 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00: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 21:37:22 +00:00
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
2020-01-18 14:41:19 +00:00
|
|
|
use Friendica\Core\BaseCache;
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
/**
|
2019-08-04 08:26:53 +00:00
|
|
|
* Implementation of the IMemoryCache mainly for testing purpose
|
2018-07-04 21:37:22 +00:00
|
|
|
*/
|
2020-01-18 14:41:19 +00:00
|
|
|
class ArrayCache extends BaseCache implements IMemoryCache
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
|
|
|
use TraitCompareDelete;
|
|
|
|
|
|
|
|
/** @var array Array with the cached data */
|
|
|
|
protected $cachedData = array();
|
|
|
|
|
2018-09-26 02:52:32 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-10-06 22:27:54 +00:00
|
|
|
public function getAllKeys($prefix = null)
|
2018-09-26 02:52:32 +00:00
|
|
|
{
|
2019-08-15 11:58:01 +00:00
|
|
|
return $this->filterArrayKeysByPrefix(array_keys($this->cachedData), $prefix);
|
2018-09-26 02:52:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
if (isset($this->cachedData[$key])) {
|
|
|
|
return $this->cachedData[$key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 14:41:19 +00:00
|
|
|
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
|
|
|
$this->cachedData[$key] = $value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
unset($this->cachedData[$key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-07-07 17:46:16 +00:00
|
|
|
public function clear($outdated = true)
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
2018-09-06 06:11:18 +00:00
|
|
|
// Array doesn't support TTL so just don't delete something
|
|
|
|
if ($outdated) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
$this->cachedData = [];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 14:41:19 +00:00
|
|
|
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
|
|
|
if (isset($this->cachedData[$key])) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return $this->set($key, $value, $ttl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 14:41:19 +00:00
|
|
|
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
|
|
|
if ($this->get($key) === $oldValue) {
|
|
|
|
return $this->set($key, $newValue);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 13:42:39 +00:00
|
|
|
|
2019-08-04 14:13:53 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
2019-08-04 13:42:39 +00:00
|
|
|
{
|
2020-01-18 14:41:19 +00:00
|
|
|
return Type::ARRAY;
|
2019-08-04 13:42:39 +00:00
|
|
|
}
|
2018-07-05 05:59:56 +00:00
|
|
|
}
|