2018-03-24 14:39:13 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
|
|
|
use Friendica\Core\Cache;
|
|
|
|
|
2018-07-10 18:55:01 -04:00
|
|
|
use Exception;
|
|
|
|
use Memcache;
|
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
/**
|
|
|
|
* Memcache Cache Driver
|
|
|
|
*
|
2018-09-15 19:28:38 -04:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-03-24 14:39:13 -04:00
|
|
|
*/
|
2018-07-05 15:54:20 -04:00
|
|
|
class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
|
2018-03-24 14:39:13 -04:00
|
|
|
{
|
2018-07-04 17:37:22 -04:00
|
|
|
use TraitCompareSet;
|
|
|
|
use TraitCompareDelete;
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param string $memcache_host
|
|
|
|
* @param int $memcache_port
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
public function __construct($memcache_host, $memcache_port)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-10 18:55:01 -04:00
|
|
|
$this->memcache = new Memcache();
|
2018-03-24 14:39:13 -04:00
|
|
|
|
|
|
|
if (!$this->memcache->connect($memcache_host, $memcache_port)) {
|
2018-07-10 18:55:01 -04:00
|
|
|
throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 22:52:32 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function getAllKeys()
|
|
|
|
{
|
|
|
|
$list = [];
|
|
|
|
$allSlabs = $this->memcache->getExtendedStats('slabs');
|
|
|
|
foreach($allSlabs as $slabs) {
|
|
|
|
foreach(array_keys($slabs) as $slabId) {
|
|
|
|
$cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId);
|
|
|
|
foreach($cachedump as $keys => $arrVal) {
|
|
|
|
if (!is_array($arrVal)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$list = array_merge($list, array_keys($arrVal));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
*/
|
|
|
|
public function set($key, $value, $ttl = Cache::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)
|
|
|
|
*/
|
|
|
|
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|