2018-03-24 14:39:13 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Core\Cache;
|
|
|
|
|
|
|
|
use Friendica\BaseObject;
|
|
|
|
use Friendica\Core\Cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Memcache Cache Driver
|
|
|
|
*
|
|
|
|
* @author Hypolite Petovan <mrpetovan@gmail.com>
|
|
|
|
*/
|
2018-07-04 17:37:22 -04:00
|
|
|
class MemcacheCacheDriver extends BaseObject 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-04 17:37:22 -04:00
|
|
|
* @var \Memcache
|
2018-03-24 14:39:13 -04:00
|
|
|
*/
|
|
|
|
private $memcache;
|
|
|
|
|
|
|
|
public function __construct($memcache_host, $memcache_port)
|
|
|
|
{
|
|
|
|
if (!class_exists('Memcache', false)) {
|
|
|
|
throw new \Exception('Memcache class isn\'t available');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->memcache = new \Memcache();
|
|
|
|
|
|
|
|
if (!$this->memcache->connect($memcache_host, $memcache_port)) {
|
|
|
|
throw new \Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:37:22 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
public function get($key)
|
|
|
|
{
|
|
|
|
$return = null;
|
|
|
|
|
|
|
|
// We fetch with the hostname as key to avoid problems with other applications
|
|
|
|
$cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
|
|
|
|
|
|
|
|
// @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
|
|
|
{
|
|
|
|
// 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(
|
|
|
|
self::getApp()->get_hostname() . ":" . $key,
|
|
|
|
serialize($value),
|
|
|
|
MEMCACHE_COMPRESSED,
|
|
|
|
time() + $ttl
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->memcache->set(
|
|
|
|
self::getApp()->get_hostname() . ":" . $key,
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return $this->memcache->delete($key);
|
|
|
|
}
|
|
|
|
|
2018-07-05 01:59:56 -04:00
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
public function clear()
|
|
|
|
{
|
2018-07-04 17:37:22 -04:00
|
|
|
return $this->memcache->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
|
|
|
|
{
|
|
|
|
return $this->memcache->add(self::getApp()->get_hostname() . ":" . $key, $value, $ttl);
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
}
|