REL-0.12 Making devilbox ready for PHP 7.2

This commit is contained in:
cytopia
2017-06-27 13:14:02 +02:00
parent ba498cad2a
commit 4ea1aa2370
10 changed files with 178 additions and 89 deletions

View File

@ -37,47 +37,47 @@ class Memcd extends BaseClass implements BaseInterface
{
parent::__construct($hostname, $data);
$memcd = new \Memcached('_devilbox');
$list = $memcd->getServerList();
if (class_exists('Memcached')) {
$memcd = new \Memcached('_devilbox');
$list = $memcd->getServerList();
// if (!empty($list)) {
// $memcd->resetServerList();
// }
if (empty($list)) {
//$memcd->setOption(\Memcached::OPT_RECV_TIMEOUT, 100);
//$memcd->setOption(\Memcached::OPT_SEND_TIMEOUT, 100);
$memcd->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$memcd->setOption(\Memcached::OPT_BINARY_PROTOCOL, false);
//$memcd->setOption(\Memcached::OPT_SERVER_FAILURE_LIMIT, 50);
//$memcd->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 100);
//$memcd->setOption(\Memcached::OPT_RETRY_TIMEOUT, 100);
//$memcd->setOption(\Memcached::OPT_REMOVE_FAILED_SERVERS, true);
$memcd->addServer($hostname, 11211);
}
if (empty($list)) {
$memcd->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$memcd->setOption(\Memcached::OPT_BINARY_PROTOCOL, false);
$memcd->addServer($hostname, 11211);
}
$err = false;
$stats = $memcd->getStats();
if (!isset($stats[$hostname.':11211'])) {
$memcd->quit();
$this->_connect_error = 'Failed to connect to Memcached host on '.$hostname.' (no connection array)';
$this->_connect_errno = 1;
return;
}
else if (!isset($stats[$hostname.':11211']['pid'])) {
$memcd->quit();
$this->_connect_error = 'Failed to connect to Memcached host on '.$hostname.' (no pid)';
$this->_connect_errno = 2;
return;
}
else if ($stats[$hostname.':11211']['pid'] < 1) {
$memcd->quit();
$this->_connect_error = 'Failed to connect to Memcached host on '.$hostname.' (invalid pid)';
$this->_connect_errno = 3;
return;
}
$err = false;
$stats = $memcd->getStats();
if (!isset($stats[$hostname.':11211'])) {
$memcd->quit();
$this->_connect_error = 'Failed to connect to Memcached host on '.$hostname.' (no connection array)';
$this->_connect_errno = 1;
return;
}
else if (!isset($stats[$hostname.':11211']['pid'])) {
$memcd->quit();
$this->_connect_error = 'Failed to connect to Memcached host on '.$hostname.' (no pid)';
$this->_connect_errno = 2;
return;
}
else if ($stats[$hostname.':11211']['pid'] < 1) {
$memcd->quit();
$this->_connect_error = 'Failed to connect to Memcached host on '.$hostname.' (invalid pid)';
$this->_connect_errno = 3;
return;
}
$memcd->set('devilbox-version', $GLOBALS['DEVILBOX_VERSION'].' ('.$GLOBALS['DEVILBOX_DATE'].')');
$this->_memcached = $memcd;
$memcd->set('devilbox-version', $GLOBALS['DEVILBOX_VERSION'].' ('.$GLOBALS['DEVILBOX_DATE'].')');
$this->_memcached = $memcd;
} else {
$ret = 0;
loadClass('Helper')->exec('echo "stats" | nc 127.0.0.1 11211', $ret);
if ($ret == 0) {
$this->_memcached = true;
}
}
}
/**
@ -85,8 +85,10 @@ class Memcd extends BaseClass implements BaseInterface
*/
public function __destruct()
{
if ($this->_memcached) {
$this->_memcached->quit();
if (class_exists('Memcached')) {
if ($this->_memcached) {
$this->_memcached->quit();
}
}
}
@ -102,12 +104,14 @@ class Memcd extends BaseClass implements BaseInterface
public function getKeys()
{
$store = array();
if ($this->_memcached) {
if (!($keys = $this->_memcached->getAllKeys())) {
$keys = array();
if (class_exists('Memcached')) {
if ($this->_memcached) {
if (!($keys = $this->_memcached->getAllKeys())) {
$keys = array();
}
$this->_memcached->getDelayed($keys);
$store = $this->_memcached->fetchAll();
}
$this->_memcached->getDelayed($keys);
$store = $this->_memcached->fetchAll();
}
return $store;
}
@ -115,11 +119,24 @@ class Memcd extends BaseClass implements BaseInterface
public function getInfo()
{
$stats = array();
if ($this->_memcached) {
$stats = $this->_memcached->getStats();
if (class_exists('Memcached')) {
if ($this->_memcached) {
$stats = $this->_memcached->getStats();
}
} else {
$ret = 0;
$output = loadClass('Helper')->exec('echo "stats" | nc 127.0.0.1 11211 | sed "s/^STAT[[:space:]]*//g" | grep -v "END"', $ret);
if ($ret == 0) {
$output = explode("\n", $output);
foreach ($output as $line) {
$tmp = explode(' ', $line);
$key = isset($tmp[0]) ? $tmp[0] : '';
$val = isset($tmp[1]) ? $tmp[1] : '';
$stats['127.0.0.1'][$key] = $val;
}
}
}
return $stats;
}
@ -147,40 +164,53 @@ class Memcd extends BaseClass implements BaseInterface
return $this->_can_connect[$hostname];
}
// Silence errors and try to connect
//error_reporting(-1);
$memcd = new \Memcached();
$memcd->resetServerList();
if (class_exists('Memcached')) {
// Silence errors and try to connect
//error_reporting(-1);
$memcd = new \Memcached();
$memcd->resetServerList();
if (!$memcd->addServer($hostname, 11211)) {
if (!$memcd->addServer($hostname, 11211)) {
$memcd->quit();
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
$this->_can_connect_err[$hostname] = $err;
return false;
}
$stats = $memcd->getStats();
if (!isset($stats[$hostname.':11211'])) {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
else if (!isset($stats[$hostname.':11211']['pid'])) {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
else if ($stats[$hostname.':11211']['pid'] < 1) {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
else {
$this->_can_connect[$hostname] = true;
}
$memcd->quit();
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
$this->_can_connect_err[$hostname] = $err;
return false;
} else {
$ret = 0;
loadClass('Helper')->exec('echo "stats" | nc '.$hostname.' 11211', $ret);
if ($ret == 0) {
$this->_can_connect[$hostname] = true;
} else {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
}
$stats = $memcd->getStats();
if (!isset($stats[$hostname.':11211'])) {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
else if (!isset($stats[$hostname.':11211']['pid'])) {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
else if ($stats[$hostname.':11211']['pid'] < 1) {
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
else {
$this->_can_connect[$hostname] = true;
}
$memcd->quit();
$this->_can_connect_err[$hostname] = $err;
return $this->_can_connect[$hostname];
}
@ -203,15 +233,21 @@ class Memcd extends BaseClass implements BaseInterface
return $this->_version;
}
if ($this->_memcached) {
$info = $this->_memcached->getVersion();
$info = array_values($info);
if (!isset($info[0])) {
loadClass('Logger')->error('Could not get Memcached version');
$this->_version = '';
} else {
$this->_version = $info[0];
if (class_exists('Memcached')) {
if ($this->_memcached) {
$info = $this->_memcached->getVersion();
$info = array_values($info);
if (!isset($info[0])) {
loadClass('Logger')->error('Could not get Memcached version');
$this->_version = '';
} else {
$this->_version = $info[0];
}
}
} else {
$version = loadClass('Helper')->exec('echo "version" | nc 127.0.0.1 11211 | grep -oE "[0-9.-]+"', $ret);
$this->_version = $version;
}
return $this->_version;
}