Default keys for Redis and Memcached

This commit is contained in:
cytopia 2018-12-10 20:09:09 +01:00
parent 8e038d403c
commit c143eac015
No known key found for this signature in database
GPG Key ID: 6D56EDB8695128A2
3 changed files with 36 additions and 57 deletions

View File

@ -25,6 +25,8 @@
<thead class="thead-inverse ">
<tr>
<th>Key</th>
<th>Size</th>
<th>TTL</th>
<th>Value</th>
</th>
</thead>
@ -32,7 +34,9 @@
<?php foreach (loadClass('Memcd')->getKeys() as $data): ?>
<tr>
<td><?php print_r($data['key']);?></td>
<td><?php print_r($data['value']);?></td>
<td><?php print_r($data['size']);?></td>
<td><?php print_r($data['ttl']);?></td>
<td><?php print_r($data['val']);?></td>
</tr>
<?php endforeach; ?>
</tbody>

View File

@ -43,7 +43,7 @@ class Memcd extends BaseClass implements BaseInterface
if (empty($list)) {
$memcd->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$memcd->setOption(\Memcached::OPT_BINARY_PROTOCOL, false);
$memcd->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
$memcd->addServer($hostname, 11211);
}
@ -67,11 +67,15 @@ class Memcd extends BaseClass implements BaseInterface
$this->_connect_errno = 3;
return;
}
$memcd->getDelayed(array('devilbox-version'));
if (!$memcd->fetchAll()) {
$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);
loadClass('Helper')->exec('echo "stats" | nc '.$hostname.' 11211', $ret);
if ($ret == 0) {
$this->_memcached = true;
}
@ -104,14 +108,22 @@ class Memcd extends BaseClass implements BaseInterface
$store = array();
if (class_exists('Memcached')) {
if ($this->_memcached) {
if (!($keys = $this->_memcached->getAllKeys())) {
$keys = array();
}
$this->_memcached->getDelayed($keys);
$store = $this->_memcached->fetchAll();
if (!is_array($store)) {
$store = array();
$this->_memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
$output = array();
exec('printf "stats cachedump 1 0\nquit\n" | nc memcd 11211 | grep -E \'^ITEM\'', $output);
foreach ($output as $line) {
$matches = array();
preg_match('/(^ITEM)\s*(.+?)\s*\[([0-9]+\s*b);\s*([0-9]+\s*s)\s*\]/', $line, $matches);
$key = $matches[2];
$store[] = array(
'key' => $key,
'size' => $matches[3],
'ttl' => $matches[4],
'val' => $this->_memcached->get($key)
);
}
return $store;
}
}
return $store;
@ -126,14 +138,14 @@ class Memcd extends BaseClass implements BaseInterface
}
} else {
$ret = 0;
$output = loadClass('Helper')->exec('echo "stats" | nc 127.0.0.1 11211 | sed "s/^STAT[[:space:]]*//g" | grep -v "END"', $ret);
$output = loadClass('Helper')->exec('echo "stats" | nc memcd 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;
$stats['memcd'][$key] = $val;
}
}
}
@ -165,51 +177,13 @@ class Memcd extends BaseClass implements BaseInterface
return $this->_can_connect[$hostname];
}
if (class_exists('Memcached')) {
// Silence errors and try to connect
//error_reporting(-1);
$memcd = new \Memcached();
$memcd->resetServerList();
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();
$this->_can_connect_err[$hostname] = $err;
$ret = 0;
loadClass('Helper')->exec('echo "stats" | nc '.$hostname.' 11211', $ret);
if ($ret == 0) {
$this->_can_connect[$hostname] = true;
} 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;
}
$err = 'Failed to connect to Memcached host on '.$hostname;
$this->_can_connect[$hostname] = false;
}
return $this->_can_connect[$hostname];
@ -247,7 +221,7 @@ class Memcd extends BaseClass implements BaseInterface
}
}
} else {
$version = loadClass('Helper')->exec('echo "version" | nc 127.0.0.1 11211 | grep -oE "[0-9.-]+"', $ret);
$version = loadClass('Helper')->exec('echo "version" | nc memcd 11211 | grep -oE "[0-9.-]+"', $ret);
$this->_version = $version;
}
return $this->_version;

View File

@ -47,6 +47,7 @@ class Redis extends BaseClass implements BaseInterface
if (array_key_exists('pass', $data)) {
$redis->auth($data['pass']);
}
$redis->set('devilbox-version', $GLOBALS['DEVILBOX_VERSION'].' ('.$GLOBALS['DEVILBOX_DATE'].')');
$this->_redis = $redis;
}
}