2018-03-24 18:39:13 +00:00
|
|
|
<?php
|
2020-02-09 14:45:36 +00:00
|
|
|
/**
|
2023-01-01 14:36:24 +00:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-09 14:45:36 +00: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-03-24 18:39:13 +00:00
|
|
|
|
2021-10-23 08:49:27 +00:00
|
|
|
namespace Friendica\Core\Cache\Type;
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2021-10-23 08:49:27 +00:00
|
|
|
use Friendica\Core\Cache\Enum\Duration;
|
2021-10-26 19:44:29 +00:00
|
|
|
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
|
|
|
use Friendica\Core\Cache\Exception\CachePersistenceException;
|
|
|
|
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
|
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2018-07-10 22:55:01 +00:00
|
|
|
use Memcache;
|
|
|
|
|
2018-03-24 18:39:13 +00:00
|
|
|
/**
|
2019-08-04 08:26:53 +00:00
|
|
|
* Memcache Cache
|
2018-03-24 18:39:13 +00:00
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
class MemcacheCache extends AbstractCache implements ICanCacheInMemory
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2023-07-26 20:42:40 +00:00
|
|
|
const NAME = 'memcached';
|
2023-07-22 21:57:38 +00:00
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
use CompareSetTrait;
|
|
|
|
use CompareDeleteTrait;
|
|
|
|
use MemcacheCommandTrait;
|
2018-07-04 21:37:22 +00:00
|
|
|
|
2018-03-24 18:39:13 +00:00
|
|
|
/**
|
2018-07-10 22:55:01 +00:00
|
|
|
* @var Memcache
|
2018-03-24 18:39:13 +00:00
|
|
|
*/
|
|
|
|
private $memcache;
|
|
|
|
|
2018-09-26 02:51:41 +00:00
|
|
|
/**
|
2021-10-26 19:44:29 +00:00
|
|
|
* @param string $hostname
|
|
|
|
* @param IManageConfigValues $config
|
|
|
|
*
|
|
|
|
* @throws InvalidCacheDriverException
|
|
|
|
* @throws CachePersistenceException
|
2018-09-26 02:51:41 +00:00
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function __construct(string $hostname, IManageConfigValues $config)
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
|
|
|
if (!class_exists('Memcache', false)) {
|
2021-10-26 19:44:29 +00:00
|
|
|
throw new InvalidCacheDriverException('Memcache class isn\'t available');
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 18:48:56 +00:00
|
|
|
parent::__construct($hostname);
|
|
|
|
|
2018-07-10 22:55:01 +00:00
|
|
|
$this->memcache = new Memcache();
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
$this->server = $config->get('system', 'memcache_host');
|
|
|
|
$this->port = $config->get('system', 'memcache_port');
|
2019-08-03 18:48:56 +00:00
|
|
|
|
2019-09-24 15:52:38 +00:00
|
|
|
if (!@$this->memcache->connect($this->server, $this->port)) {
|
2021-10-26 19:44:29 +00:00
|
|
|
throw new CachePersistenceException('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 09:56:13 +00:00
|
|
|
/**
|
|
|
|
* Memcache doesn't allow spaces in keys
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getCacheKey(string $key): string
|
|
|
|
{
|
|
|
|
return str_replace(' ', '_', parent::getCacheKey($key));
|
|
|
|
}
|
|
|
|
|
2018-09-26 02:52:32 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function getAllKeys(?string $prefix = null): array
|
2018-09-26 02:52:32 +00:00
|
|
|
{
|
2019-09-24 15:52:38 +00:00
|
|
|
$keys = $this->getOriginalKeys($this->getMemcacheKeys());
|
2018-10-06 22:27:54 +00:00
|
|
|
|
2018-10-07 08:35:37 +00:00
|
|
|
return $this->filterArrayKeysByPrefix($keys, $prefix);
|
2018-09-26 02:52:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function get(string $key)
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
// We fetch with the hostname as key to avoid problems with other applications
|
2021-10-26 19:44:29 +00:00
|
|
|
$cached = $this->memcache->get($cacheKey);
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
// @see http://php.net/manual/en/memcache.get.php#84275
|
|
|
|
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
|
2021-10-26 19:44:29 +00:00
|
|
|
return null;
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = @unserialize($cached);
|
|
|
|
|
|
|
|
// Only return a value if the serialized value is valid.
|
|
|
|
// We also check if the db entry is a serialized
|
|
|
|
// boolean 'false' value (which we want to return).
|
|
|
|
if ($cached === serialize(false) || $value !== false) {
|
2021-10-26 19:44:29 +00:00
|
|
|
return $value;
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 19:44:29 +00:00
|
|
|
return null;
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
2018-07-05 19:47:52 +00:00
|
|
|
|
2018-03-24 18:39:13 +00:00
|
|
|
// We store with the hostname as key to avoid problems with other applications
|
2018-07-04 21:37:22 +00:00
|
|
|
if ($ttl > 0) {
|
|
|
|
return $this->memcache->set(
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey,
|
2018-07-04 21:37:22 +00:00
|
|
|
serialize($value),
|
|
|
|
MEMCACHE_COMPRESSED,
|
|
|
|
time() + $ttl
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->memcache->set(
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey,
|
2018-07-04 21:37:22 +00:00
|
|
|
serialize($value),
|
|
|
|
MEMCACHE_COMPRESSED
|
|
|
|
);
|
|
|
|
}
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 21:37:22 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function delete(string $key): bool
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
|
|
|
return $this->memcache->delete($cacheKey);
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 05:59:56 +00:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function clear(bool $outdated = true): bool
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
2018-07-07 17:46:16 +00:00
|
|
|
if ($outdated) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $this->memcache->flush();
|
|
|
|
}
|
2018-07-04 21:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2021-10-26 19:44:29 +00:00
|
|
|
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
|
2018-07-04 21:37:22 +00:00
|
|
|
{
|
2021-10-26 19:44:29 +00:00
|
|
|
$cacheKey = $this->getCacheKey($key);
|
|
|
|
return $this->memcache->add($cacheKey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
}
|