2018-03-24 14:39:13 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
2018-07-10 18:55:01 -04:00
|
|
|
use Exception;
|
2020-01-18 09:41:19 -05:00
|
|
|
use Friendica\Core\BaseCache;
|
2019-12-19 14:11:07 -05:00
|
|
|
use Friendica\Core\Config\IConfiguration;
|
2018-07-10 18:55:01 -04:00
|
|
|
use Memcache;
|
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
/**
|
2019-08-04 04:26:53 -04:00
|
|
|
* Memcache Cache
|
2018-03-24 14:39:13 -04:00
|
|
|
*
|
2018-09-15 19:28:38 -04:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-03-24 14:39:13 -04:00
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
class MemcacheCache extends BaseCache implements IMemoryCache
|
2018-03-24 14:39:13 -04:00
|
|
|
{
|
2018-07-04 17:37:22 -04:00
|
|
|
use TraitCompareSet;
|
|
|
|
use TraitCompareDelete;
|
2019-09-24 11:52:38 -04:00
|
|
|
use TraitMemcacheCommand;
|
2018-07-04 17:37:22 -04:00
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
/**
|
2018-07-10 18:55:01 -04:00
|
|
|
* @var Memcache
|
2018-03-24 14:39:13 -04:00
|
|
|
*/
|
|
|
|
private $memcache;
|
|
|
|
|
2018-09-25 22:51:41 -04:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-12-19 14:11:07 -05:00
|
|
|
public function __construct(string $hostname, IConfiguration $config)
|
2018-03-24 14:39:13 -04:00
|
|
|
{
|
|
|
|
if (!class_exists('Memcache', false)) {
|
2018-07-10 18:55:01 -04:00
|
|
|
throw new Exception('Memcache class isn\'t available');
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
|
2019-08-03 14:48:56 -04:00
|
|
|
parent::__construct($hostname);
|
|
|
|
|
2018-07-10 18:55:01 -04:00
|
|
|
$this->memcache = new Memcache();
|
2018-03-24 14:39:13 -04:00
|
|
|
|
2019-09-24 11:52:38 -04:00
|
|
|
$this->server = $config->get('system', 'memcache_host');;
|
|
|
|
$this->port = $config->get('system', 'memcache_port');
|
2019-08-03 14:48:56 -04:00
|
|
|
|
2019-09-24 11:52:38 -04:00
|
|
|
if (!@$this->memcache->connect($this->server, $this->port)) {
|
|
|
|
throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 22:52:32 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-10-06 18:27:54 -04:00
|
|
|
public function getAllKeys($prefix = null)
|
2018-09-25 22:52:32 -04:00
|
|
|
{
|
2019-09-24 11:52:38 -04:00
|
|
|
$keys = $this->getOriginalKeys($this->getMemcacheKeys());
|
2018-10-06 18:27:54 -04:00
|
|
|
|
2018-10-07 04:35:37 -04:00
|
|
|
return $this->filterArrayKeysByPrefix($keys, $prefix);
|
2018-09-25 22:52:32 -04:00
|
|
|
}
|
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
public function get($key)
|
|
|
|
{
|
2019-08-04 09:51:49 -04:00
|
|
|
$return = null;
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = $this->getCacheKey($key);
|
2018-03-24 14:39:13 -04:00
|
|
|
|
|
|
|
// We fetch with the hostname as key to avoid problems with other applications
|
2018-07-05 15:47:52 -04:00
|
|
|
$cached = $this->memcache->get($cachekey);
|
2018-03-24 14:39:13 -04:00
|
|
|
|
|
|
|
// @see http://php.net/manual/en/memcache.get.php#84275
|
|
|
|
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
$return = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-03-24 14:39:13 -04:00
|
|
|
{
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = $this->getCacheKey($key);
|
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
// We store with the hostname as key to avoid problems with other applications
|
2018-07-04 17:37:22 -04:00
|
|
|
if ($ttl > 0) {
|
|
|
|
return $this->memcache->set(
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey,
|
2018-07-04 17:37:22 -04:00
|
|
|
serialize($value),
|
|
|
|
MEMCACHE_COMPRESSED,
|
|
|
|
time() + $ttl
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->memcache->set(
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey,
|
2018-07-04 17:37:22 -04:00
|
|
|
serialize($value),
|
|
|
|
MEMCACHE_COMPRESSED
|
|
|
|
);
|
|
|
|
}
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
public function delete($key)
|
|
|
|
{
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = $this->getCacheKey($key);
|
|
|
|
return $this->memcache->delete($cachekey);
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
|
2018-07-05 01:59:56 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-07-07 13:46:16 -04:00
|
|
|
public function clear($outdated = true)
|
2018-03-24 14:39:13 -04:00
|
|
|
{
|
2018-07-07 13:46:16 -04:00
|
|
|
if ($outdated) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $this->memcache->flush();
|
|
|
|
}
|
2018-07-04 17:37:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2020-01-18 09:41:19 -05:00
|
|
|
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
|
2018-07-04 17:37:22 -04:00
|
|
|
{
|
2018-07-05 15:47:52 -04:00
|
|
|
$cachekey = $this->getCacheKey($key);
|
2018-07-07 14:07:07 -04:00
|
|
|
return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
2019-08-04 09:42:39 -04:00
|
|
|
|
2019-08-04 10:13:53 -04:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
2019-08-04 09:42:39 -04:00
|
|
|
{
|
2020-01-18 09:41:19 -05:00
|
|
|
return Type::MEMCACHE;
|
2019-08-04 09:42:39 -04:00
|
|
|
}
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|