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