devilbox/.devilbox/www/include/lib/Redis.php

176 lines
3.7 KiB
PHP
Raw Normal View History

2017-04-23 11:20:32 +00:00
<?php
namespace devilbox;
/**
* @requires devilbox::Logger
*/
2017-05-06 09:13:33 +00:00
class Redis extends _Base implements _iBase
2017-04-23 11:20:32 +00:00
{
/*********************************************************************************
*
* Statics
*
*********************************************************************************/
/**
* Redis instance
* @var Redis|null
*/
protected static $instance = null;
/**
* Singleton Instance getter.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
* @return object|null
*/
2017-05-06 09:13:33 +00:00
public static function getInstance($host, $user = null, $pass = null)
2017-04-23 11:20:32 +00:00
{
if (!isset(static::$instance)) {
static::$instance = new static($host);
}
// If current Redis instance was unable to connect
if (!static::$instance) {
2017-05-06 09:13:33 +00:00
//loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
2017-04-23 11:20:32 +00:00
}
return static::$instance;
}
/**
* Connect to Redis
*
* @param string $err Reference to error message
* @param string $host Redis hostname
* @return boolean
*/
2017-05-06 09:13:33 +00:00
public static function testConnection(&$err, $host, $user = '', $pass = '')
2017-04-23 11:20:32 +00:00
{
$err = false;
// Silence errors and try to connect
error_reporting(0);
$redis = new \Redis();
if (!$redis->connect($host, 6379)) {
2017-05-06 09:13:33 +00:00
$err = 'Failed to connect to Redis host on '.$host;
2017-04-23 11:20:32 +00:00
error_reporting(-1);
return false;
}
error_reporting(-1);
$redis->close();
return true;
}
/*********************************************************************************
*
* Private Variables
*
*********************************************************************************/
/**
* Redis instance
* @var object|null
*/
private $_redis = null;
/*********************************************************************************
*
* Construct/Destructor
*
*********************************************************************************/
/**
* Use singleton getInstance() instead.
*
* @param string $user Username
* @param string $pass Password
* @param string $host Host
*/
public function __construct($host)
{
// Silence errors and try to connect
error_reporting(0);
$redis = new \Redis();
if (!$redis->connect($host, 6379)) {
2017-05-06 09:13:33 +00:00
$this->setConnectError('Failed to connect to Redis host on '.$host);
$this->setConnectErrno(1);
//loadClass('Logger')->error($this->_connect_error);
2017-04-23 11:20:32 +00:00
} else {
2017-05-07 16:49:38 +00:00
$redis->set('devilbox-version', $GLOBALS['DEVILBOX_VERSION'].' ('.$GLOBALS['DEVILBOX_DATE'].')');
2017-04-23 11:20:32 +00:00
$this->_redis = $redis;
}
error_reporting(-1);
}
/**
* Destructor
*/
public function __destruct()
{
if ($this->_redis) {
$this->_redis->close();
}
}
2017-05-06 09:13:33 +00:00
2017-04-23 11:20:32 +00:00
/*********************************************************************************
*
* Redis Select functions
*
*********************************************************************************/
2017-04-23 11:34:14 +00:00
public function getInfo()
{
2017-05-06 09:13:33 +00:00
if ($this->_redis) {
return $this->_redis->info('all');
} else {
return array();
}
2017-04-23 11:34:14 +00:00
}
2017-04-23 11:20:32 +00:00
2017-04-23 12:08:54 +00:00
public function getKeys()
{
2017-05-07 16:49:38 +00:00
$store = array();
2017-05-06 09:13:33 +00:00
if ($this->_redis) {
2017-05-07 16:49:38 +00:00
$keys = $this->_redis->keys('*');
foreach ($keys as $key) {
$store[$key] = $this->_redis->get($key);
}
2017-05-06 09:13:33 +00:00
}
2017-05-07 16:49:38 +00:00
return $store;
2017-04-23 12:08:54 +00:00
}
2017-04-23 11:20:32 +00:00
2017-05-06 09:13:33 +00:00
2017-04-23 11:20:32 +00:00
/*********************************************************************************
*
2017-05-06 09:13:33 +00:00
* Interface required functions
2017-04-23 11:20:32 +00:00
*
*********************************************************************************/
2017-05-06 09:13:33 +00:00
public function getName($default = 'Redis')
2017-04-23 11:20:32 +00:00
{
2017-05-06 09:13:33 +00:00
return $default;
2017-04-23 11:20:32 +00:00
}
2017-05-06 09:13:33 +00:00
public function getVersion()
2017-04-23 11:20:32 +00:00
{
2017-05-06 09:13:33 +00:00
$info = $this->getInfo();
if (!isset($info['redis_version'])) {
loadClass('Logger')->error('Could not get Redis version');
return '';
}
return $info['redis_version'];
2017-04-23 11:20:32 +00:00
}
}