Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiserver #62

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 64 additions & 32 deletions Cm/Cache/Backend/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,24 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
*/
public function __construct($options = array())
{
if ( empty($options['server']) ) {
Zend_Cache::throwException('Redis \'server\' not specified.');
}

if ( empty($options['port']) && substr($options['server'],0,1) != '/' ) {
Zend_Cache::throwException('Redis \'port\' not specified.');
}

$port = isset($options['port']) ? $options['port'] : NULL;
$timeout = isset($options['timeout']) ? $options['timeout'] : self::DEFAULT_CONNECT_TIMEOUT;
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
$this->_redis = new Credis_Client($options['server'], $port, $timeout, $persistent);

if ( isset($options['force_standalone']) && $options['force_standalone']) {
$this->_redis->forceStandalone();
}

$connectRetries = isset($options['connect_retries']) ? (int)$options['connect_retries'] : self::DEFAULT_CONNECT_RETRIES;
$this->_redis->setMaxConnectRetries($connectRetries);

if ( ! empty($options['read_timeout']) && $options['read_timeout'] > 0) {
$this->_redis->setReadTimeout((float) $options['read_timeout']);
}

if ( ! empty($options['password'])) {
$this->_redis->auth($options['password']) or Zend_Cache::throwException('Unable to authenticate with the redis server.');
}

// Always select database on startup in case persistent connection is re-used by other code
if (empty($options['database'])) {
$options['database'] = 0;
if( isset($options['servers']) ) {
if( !is_array($options['servers']) ) {
Zend_Cache::throwException('Redis \'servers\' config should be an array. Got \''.gettype($options['servers']).'\'');
}
$servers = array();
foreach( $options['servers'] as $server) {
$servers[] = $this->_initBackendConfig($server);
}
$this->_redis = new Credis_Cluster($servers);
foreach($this->_redis->clients() as $client) {
$this->_initRedisConfig($client,$options['servers']);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the second argument to _initRedisConfig not be just $options?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should indeed be the case. I do believe that this is not sufficient. The "_initRedisConfig" method is a way of compensating for the limited amount of options you can pass to the constructor of the Credis_Client or Credis_Cluster.

As you mentioned most of the work should be done in the Credis library. I'll try to figure out how we can do that and send the right pull requests.

}
} else {
$backendConfig = $this->_initBackendConfig($options);
$persistent = isset($options['persistent']) ? $options['persistent'] : '';
$this->_redis = new Credis_Client($backendConfig['host'], $backendConfig['port'], $backendConfig['timeout'], $persistent);
$this->_initRedisConfig($this->_redis, $options);
}
$this->_redis->select( (int) $options['database']) or Zend_Cache::throwException('The redis database could not be selected.');

if ( isset($options['notMatchingTags']) ) {
$this->_notMatchingTags = (bool) $options['notMatchingTags'];
Expand Down Expand Up @@ -172,6 +157,53 @@ public function __construct($options = array())
}
}

/**
* Initialize Credis_Cluster backend config
* @param array $options
* @return \Cm_Cache_Backend_Redis
*/
protected function _initBackendConfig($options)
{
if ( empty($options['server']) ) {
Zend_Cache::throwException('Redis \'server\' not specified.');
}

if ( empty($options['port']) && substr($options['server'],0,1) != '/' ) {
Zend_Cache::throwException('Redis \'port\' not specified.');
}
$port = isset($options['port']) ? $options['port'] : NULL;
$timeout = isset($options['timeout']) ? $options['timeout'] : self::DEFAULT_CONNECT_TIMEOUT;
$config = array('host'=>$options['server'], 'port'=>$port, 'timeout'=> $timeout);
return $config;
}
/**
* Configure Credits_Client options
* @param Credis_Client $redis
* @param array $options
*/
protected function _initRedisConfig(\Credis_Client $redis, $options)
{
if ( isset($options['force_standalone']) && $options['force_standalone']) {
$redis->forceStandalone();
}

$connectRetries = isset($options['connect_retries']) ? (int)$options['connect_retries'] : self::DEFAULT_CONNECT_RETRIES;
$redis->setMaxConnectRetries($connectRetries);

if ( ! empty($options['read_timeout']) && $options['read_timeout'] > 0) {
$redis->setReadTimeout((float) $options['read_timeout']);
}

if ( ! empty($options['password'])) {
$redis->auth($options['password']) or Zend_Cache::throwException('Unable to authenticate with the redis server.');
}

// Always select database on startup in case persistent connection is re-used by other code
if (empty($options['database'])) {
$options['database'] = 0;
}
$redis->select( (int) $options['database']) or Zend_Cache::throwException('The redis database could not be selected.');
}
/**
* Load value with given id from cache
*
Expand Down