From 9aade1cd9f666ac0f70c97c4299063fafe68ada8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 22 Jun 2015 13:19:45 +0300 Subject: Fix #3913 --- system/libraries/Cache/drivers/Cache_memcached.php | 142 ++++++++++----------- system/libraries/Cache/drivers/Cache_redis.php | 125 +++++++++--------- user_guide_src/source/changelog.rst | 2 + 3 files changed, 130 insertions(+), 139 deletions(-) diff --git a/system/libraries/Cache/drivers/Cache_memcached.php b/system/libraries/Cache/drivers/Cache_memcached.php index b90b561c9..111e2109d 100644 --- a/system/libraries/Cache/drivers/Cache_memcached.php +++ b/system/libraries/Cache/drivers/Cache_memcached.php @@ -68,6 +68,76 @@ class CI_Cache_memcached extends CI_Driver { ) ); + // ------------------------------------------------------------------------ + + /** + * Class constructor + * + * Setup Memcache(d) + * + * @return void + */ + public function __construct() + { + // Try to load memcached server info from the config file. + $CI =& get_instance(); + $defaults = $this->_memcache_conf['default']; + + if ($CI->config->load('memcached', TRUE, TRUE)) + { + if (is_array($CI->config->config['memcached'])) + { + $this->_memcache_conf = array(); + + foreach ($CI->config->config['memcached'] as $name => $conf) + { + $this->_memcache_conf[$name] = $conf; + } + } + } + + if (class_exists('Memcached', FALSE)) + { + $this->_memcached = new Memcached(); + } + elseif (class_exists('Memcache', FALSE)) + { + $this->_memcached = new Memcache(); + } + else + { + throw new RuntimeException('Cache: Failed to create Memcache(d) object; extension not loaded?'); + } + + foreach ($this->_memcache_conf as $cache_server) + { + isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host']; + isset($cache_server['port']) OR $cache_server['port'] = $defaults['port']; + isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight']; + + if (get_class($this->_memcached) === 'Memcache') + { + // Third parameter is persistance and defaults to TRUE. + $this->_memcached->addServer( + $cache_server['hostname'], + $cache_server['port'], + TRUE, + $cache_server['weight'] + ); + } + else + { + $this->_memcached->addServer( + $cache_server['hostname'], + $cache_server['port'], + $cache_server['weight'] + ); + } + } + } + + // ------------------------------------------------------------------------ + /** * Fetch from cache * @@ -204,75 +274,6 @@ class CI_Cache_memcached extends CI_Driver { // ------------------------------------------------------------------------ - /** - * Setup memcached. - * - * @return bool - */ - protected function _setup_memcached() - { - // Try to load memcached server info from the config file. - $CI =& get_instance(); - $defaults = $this->_memcache_conf['default']; - - if ($CI->config->load('memcached', TRUE, TRUE)) - { - if (is_array($CI->config->config['memcached'])) - { - $this->_memcache_conf = array(); - - foreach ($CI->config->config['memcached'] as $name => $conf) - { - $this->_memcache_conf[$name] = $conf; - } - } - } - - if (class_exists('Memcached', FALSE)) - { - $this->_memcached = new Memcached(); - } - elseif (class_exists('Memcache', FALSE)) - { - $this->_memcached = new Memcache(); - } - else - { - log_message('error', 'Failed to create object for Memcached Cache; extension not loaded?'); - return FALSE; - } - - foreach ($this->_memcache_conf as $cache_server) - { - isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host']; - isset($cache_server['port']) OR $cache_server['port'] = $defaults['port']; - isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight']; - - if (get_class($this->_memcached) === 'Memcache') - { - // Third parameter is persistance and defaults to TRUE. - $this->_memcached->addServer( - $cache_server['hostname'], - $cache_server['port'], - TRUE, - $cache_server['weight'] - ); - } - else - { - $this->_memcached->addServer( - $cache_server['hostname'], - $cache_server['port'], - $cache_server['weight'] - ); - } - } - - return TRUE; - } - - // ------------------------------------------------------------------------ - /** * Is supported * @@ -289,7 +290,6 @@ class CI_Cache_memcached extends CI_Driver { return FALSE; } - return $this->_setup_memcached(); + return TRUE; } - } diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index 66966ba16..d7dca1973 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -78,6 +78,63 @@ class CI_Cache_redis extends CI_Driver // ------------------------------------------------------------------------ + /** + * Class constructor + * + * Setup Redis + * + * Loads Redis config file if present. Will halt execution + * if a Redis connection can't be established. + * + * @return void + * @see Redis::connect() + */ + public function __construct() + { + $config = array(); + $CI =& get_instance(); + + if ($CI->config->load('redis', TRUE, TRUE)) + { + $config = $CI->config->item('redis'); + } + + $config = array_merge(self::$_default_config, $config); + $this->_redis = new Redis(); + + try + { + if ($config['socket_type'] === 'unix') + { + $success = $this->_redis->connect($config['socket']); + } + else // tcp socket + { + $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); + } + + if ( ! $success) + { + throw new RuntimeException('Cache: Redis connection failed. Check your configuration.'); + } + } + catch (RedisException $e) + { + throw new RuntimeException('Cache: Redis connection refused ('.$e->getMessage().')'); + } + + if (isset($config['password']) && ! $this->_redis->auth($config['password'])) + { + throw new RuntimeException('Cache: Redis authentication failed.'); + } + + // Initialize the index of serialized values. + $serialized = $this->_redis->sMembers('_ci_redis_serialized'); + empty($serialized) OR $this->_serialized = array_flip($serialized); + } + + // ------------------------------------------------------------------------ + /** * Get cache * @@ -247,73 +304,6 @@ class CI_Cache_redis extends CI_Driver return FALSE; } - return $this->_setup_redis(); - } - - // ------------------------------------------------------------------------ - - /** - * Setup Redis config and connection - * - * Loads Redis config file if present. Will halt execution - * if a Redis connection can't be established. - * - * @return bool - * @see Redis::connect() - */ - protected function _setup_redis() - { - $config = array(); - $CI =& get_instance(); - - if ($CI->config->load('redis', TRUE, TRUE)) - { - $config = $CI->config->item('redis'); - } - - $config = array_merge(self::$_default_config, $config); - - $this->_redis = new Redis(); - - try - { - if ($config['socket_type'] === 'unix') - { - $success = $this->_redis->connect($config['socket']); - } - else // tcp socket - { - $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); - } - - if ( ! $success) - { - log_message('debug', 'Cache: Redis connection refused. Check the config.'); - return FALSE; - } - } - catch (RedisException $e) - { - log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')'); - return FALSE; - } - - if (isset($config['password'])) - { - if ( ! $this->_redis->auth($config['password'])) - { - log_message('debug', 'Cache: Redis authentication failed.'); - return FALSE; - } - } - - // Initialize the index of serialized values. - $serialized = $this->_redis->sMembers('_ci_redis_serialized'); - if ( ! empty($serialized)) - { - $this->_serialized = array_flip($serialized); - } - return TRUE; } @@ -333,5 +323,4 @@ class CI_Cache_redis extends CI_Driver $this->_redis->close(); } } - } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 46d19bf2b..211811339 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -43,6 +43,8 @@ Bug fixes for 3.0.1 - Fixed a bug (#3903) - :doc:`Form Validation Library ` ignored "unnamed" closure validation rules. - Fixed a bug (#3904) - :doc:`Form Validation Library ` ignored "named" callback rules when the field is empty and there's no 'required' rule. - Fixed a bug (#3922) - :doc:`Email ` and :doc:`XML-RPC ` libraries could enter an infinite loop due to `PHP bug #39598 `_. +- Fixed a bug (#3913) - :doc:`Cache Library ` didn't work with the direct ``$this->cache->$driver_name->method()`` syntax with Redis and Memcache(d). + Version 3.0.0 ============= -- cgit v1.2.3-24-g4f1b