Skip to content

Commit

Permalink
rewrite getConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
hans2103 committed May 31, 2014
1 parent b272025 commit 085fa23
Showing 1 changed file with 124 additions and 29 deletions.
153 changes: 124 additions & 29 deletions libraries/joomla/cache/storage/redis.php
@@ -1,37 +1,37 @@
<?php
/**
* @package Joomla.Platform
* @package Joomla.Platform
* @subpackage Cache
*
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* Redis cache storage handler
*
* @package Joomla.Platform
* @package Joomla.Platform
* @subpackage Cache
* @since 11.1
* @since 13.1
*/
class JCacheStorageRedis extends JCacheStorage
{
/**
* Redis connection object
*
* @var Redis
* @since 11.1
* @var Redis
* @since 13.1
*/
protected static $_redis = null;

/**
* Persistent session flag
*
* @var boolean
* @since 11.1
* @var boolean
* @since 13.1
*/
protected $_persistent = false;

Expand All @@ -40,7 +40,7 @@ class JCacheStorageRedis extends JCacheStorage
*
* @param array $options Optional parameters.
*
* @since 11.1
* @since 13.1
*/
public function __construct($options = array())
{
Expand All @@ -56,61 +56,124 @@ public function __construct($options = array())
*
* @return object redis connection object
*
* @since 11.1
* @since 13.1
* @throws RuntimeException
*/
protected function getConnection()
{
if (static::isSupported() != true)
if (static::isSupported() == false)
{
return false;
}

$app = JFactory::getApplication();
$config = JFactory::getConfig();
$this->_persistent = $config->get('redis_persist', true);

$server = array();
$server['host'] = $config->get('redis_server_host', 'localhost');
$server['port'] = $config->get('redis_server_port', 6379);
$server['auth'] = $config->get('redis_server_auth', NULL);
$server['db'] = (int)$config->get('redis_server_db', NULL);

self::$_redis = new Redis();
if($this->_persistent)
{
$redistest = self::$_redis->pconnect($server['host'], $server['port']) && self::$_redis->auth($server['auth']);
try
{
$connection = self::$_redis->pconnect($server['host'], $server['port']);
$auth = (!empty($server['auth'])) ? self::$_redis->auth($server['auth']) : true;
}

catch(Exception $e)
{

}
}
else
{
$redistest = self::$_redis->connect($server['host'], $server['port']) && self::$_redis->auth($server['auth']);
try
{
$connection = self::$_redis->connect($server['host'], $server['port']);
$auth = (!empty($server['auth'])) ? self::$_redis->auth($server['auth']) : true;
}

catch(Exception $e)
{

}
}

if ($redistest == false)
if ($connection == false)
{
$app = JFactory::getApplication();
self::$_redis = null;
if ($app->isAdmin())
{
JError::raiseWarning(500, JText::_('JLIB_CACHE_ERROR_CACHE_STORAGE_REDIS_NO_CONNECTION'));
JError::raiseWarning(500, 'Redis connection failed');
}

return;
}

return;
if ($auth == false)
{
if ($app->isAdmin())
{
JError::raiseWarning(500, 'Redis authentication failed');
}

return;
}

$select = self::$_redis->select($server['db']);
if($select == false)
{
self::$_redis = null;
if ($app->isAdmin())
{
JError::raiseWarning(500, 'Redis failed to select database');
}

return;
}

try
{
self::$_redis->ping();
}

catch(RedisException $e)
{
self::$_redis = null;
if ($app->isAdmin())
{
JError::raiseWarning(500, 'Redis ping failed');
}

return;
}

return self::$_redis;
}

/**
* Get cached data from redis by id and group
*
* @param string $id The cache data id
* @param string $group The cache data group
* @param string $id The cache data id
* @param string $group The cache data group
* @param boolean $checkTime True to verify cache time expiration threshold
*
* @return mixed Boolean false on failure or a cached data string
*
* @since 11.1
* @since 13.1
*/
public function get($id, $group, $checkTime = true)
{
if (self::isConnected() == false)
{
return false;
}

$cache_id = $this->_getCacheId($id, $group);
$back = self::$_redis->get($cache_id);

Expand All @@ -120,12 +183,17 @@ public function get($id, $group, $checkTime = true)
/**
* Get all cached data
*
* @return array data
* @return array data
*
* @since 11.1
* @since 13.1
*/
public function getAll()
{
if (self::isConnected() == false)
{
return false;
}

parent::getAll();

$allKeys = self::$_redis->keys('*');
Expand Down Expand Up @@ -161,16 +229,21 @@ public function getAll()
/**
* Store the data to redis by id and group
*
* @param string $id The cache data id
* @param string $id The cache data id
* @param string $group The cache data group
* @param string $data The data to store in cache
*
* @return boolean True on success, false otherwise
*
* @since 11.1
* @since 13.1
*/
public function store($id, $group, $data)
{
if (self::isConnected() == false)
{
return false;
}

$cache_id = $this->_getCacheId($id, $group);

$tmparr = new stdClass;
Expand All @@ -194,15 +267,20 @@ public function store($id, $group, $data)
/**
* Remove a cached data entry by id and group
*
* @param string $id The cache data id
* @param string $id The cache data id
* @param string $group The cache data group
*
* @return boolean True on success, false otherwise
*
* @since 11.1
* @since 13.1
*/
public function remove($id, $group)
{
if (self::isConnected() == false)
{
return false;
}

$cache_id = $this->_getCacheId($id, $group);
return self::$_redis->delete($cache_id);
}
Expand All @@ -212,15 +290,20 @@ public function remove($id, $group)
*
* @param string $group The cache data group
* @param string $mode The mode for cleaning cache [group|notgroup]
* group mode : cleans all cache in the group
* group mode : cleans all cache in the group
* notgroup mode : cleans all cache not in the group
*
* @return boolean True on success, false otherwise
*
* @since 11.1
* @since 13.1
*/
public function clean($group, $mode = null)
{
if (self::isConnected() == false)
{
return false;
}

$allKeys = self::$_redis->keys('*');
if ($allKeys === false)
{
Expand Down Expand Up @@ -248,7 +331,7 @@ public function clean($group, $mode = null)
*
* @return boolean True on success, false otherwise.
*
* @since 12.1
* @since 13.1
*/
public static function isSupported()
{
Expand All @@ -259,4 +342,16 @@ public static function isSupported()

return true;
}

/**
* Test to see if Redis connection is up
*
* @return boolean True on success, false otherwise.
*
* @since 13.1
*/
public static function isConnected()
{
return (bool)self::$_redis;
}
}

0 comments on commit 085fa23

Please sign in to comment.