mirror of
https://github.com/cytopia/devilbox.git
synced 2025-06-22 00:57:06 +00:00
REL-0.9 Intranet refactoring
This commit is contained in:
165
.devilbox/www/include/lib/container/Redis.php
Normal file
165
.devilbox/www/include/lib/container/Redis.php
Normal file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
namespace devilbox;
|
||||
|
||||
/**
|
||||
* @requires devilbox::Logger
|
||||
*/
|
||||
class Redis extends BaseClass implements BaseInterface
|
||||
{
|
||||
/*********************************************************************************
|
||||
*
|
||||
* Private Variables
|
||||
*
|
||||
*********************************************************************************/
|
||||
|
||||
/**
|
||||
* Redis instance
|
||||
* @var object|null
|
||||
*/
|
||||
private $_redis = null;
|
||||
|
||||
|
||||
|
||||
/*********************************************************************************
|
||||
*
|
||||
* Constructor Overwrite
|
||||
*
|
||||
*********************************************************************************/
|
||||
|
||||
/**
|
||||
* Use singleton getInstance() instead.
|
||||
*
|
||||
* @param string $user Username
|
||||
* @param string $pass Password
|
||||
* @param string $host Host
|
||||
*/
|
||||
public function __construct($hostname, $data = array())
|
||||
{
|
||||
parent::__construct($hostname, $data);
|
||||
|
||||
// Silence errors and try to connect
|
||||
error_reporting(0);
|
||||
$redis = new \Redis();
|
||||
|
||||
if (!$redis->connect($hostname, 6379)) {
|
||||
$this->setConnectError('Failed to connect to Redis host on '.$hostname);
|
||||
$this->setConnectErrno(1);
|
||||
//loadClass('Logger')->error($this->_connect_error);
|
||||
} else {
|
||||
$redis->set('devilbox-version', $GLOBALS['DEVILBOX_VERSION'].' ('.$GLOBALS['DEVILBOX_DATE'].')');
|
||||
$this->_redis = $redis;
|
||||
}
|
||||
error_reporting(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->_redis) {
|
||||
$this->_redis->close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************************
|
||||
*
|
||||
* Select functions
|
||||
*
|
||||
*********************************************************************************/
|
||||
|
||||
public function getInfo()
|
||||
{
|
||||
if ($this->_redis) {
|
||||
return $this->_redis->info('all');
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
public function getKeys()
|
||||
{
|
||||
$store = array();
|
||||
if ($this->_redis) {
|
||||
$keys = $this->_redis->keys('*');
|
||||
foreach ($keys as $key) {
|
||||
$store[$key] = $this->_redis->get($key);
|
||||
}
|
||||
}
|
||||
return $store;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************************
|
||||
*
|
||||
* Interface required functions
|
||||
*
|
||||
*********************************************************************************/
|
||||
|
||||
private $_can_connect = array();
|
||||
private $_can_connect_err = array();
|
||||
|
||||
private $_name = null;
|
||||
private $_version = null;
|
||||
|
||||
public function canConnect(&$err, $hostname, $data = array())
|
||||
{
|
||||
$err = false;
|
||||
|
||||
// Return if already cached
|
||||
if (isset($this->_can_connect[$hostname])) {
|
||||
// Assume error for unset error message
|
||||
$err = isset($this->_can_connect_err[$hostname]) ? $this->_can_connect_err[$hostname] : true;
|
||||
return $this->_can_connect[$hostname];
|
||||
}
|
||||
|
||||
// Silence errors and try to connect
|
||||
//error_reporting(0);
|
||||
$redis = new \Redis();
|
||||
|
||||
if (!$redis->connect($hostname, 6379)) {
|
||||
$err = 'Failed to connect to Redis host on '.$hostname;
|
||||
$this->_can_connect[$hostname] = false;
|
||||
} else {
|
||||
$this->_can_connect[$hostname] = true;
|
||||
}
|
||||
//error_reporting(-1);
|
||||
|
||||
$redis->close();
|
||||
|
||||
$this->_can_connect_err[$hostname] = $err;
|
||||
return $this->_can_connect[$hostname];
|
||||
}
|
||||
|
||||
public function getName($default = 'Redis')
|
||||
{
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function getVersion()
|
||||
{
|
||||
// Return if already cached
|
||||
if ($this->_version !== null) {
|
||||
return $this->_version;
|
||||
}
|
||||
|
||||
// Return empty if not available
|
||||
if (!$this->isAvailable()) {
|
||||
$this->_version = '';
|
||||
return $this->_version;
|
||||
}
|
||||
|
||||
$info = $this->getInfo();
|
||||
if (!isset($info['redis_version'])) {
|
||||
loadClass('Logger')->error('Could not get Redis version');
|
||||
$this->_version = '';
|
||||
} else {
|
||||
$this->_version = $info['redis_version'];
|
||||
}
|
||||
|
||||
return $this->_version;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user