diff options
Diffstat (limited to 'system/libraries/Session/drivers/Session_redis_driver.php')
-rw-r--r-- | system/libraries/Session/drivers/Session_redis_driver.php | 77 |
1 files changed, 56 insertions, 21 deletions
diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index 1215b3afd..3ee59a4a2 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -37,8 +37,6 @@ */ defined('BASEPATH') OR exit('No direct script access allowed'); -define('PHPREDIS_VERSION', phpversion('redis')); - /** * CodeIgniter Session Redis Driver * @@ -78,6 +76,33 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle */ protected $_key_exists = FALSE; + /** + * Name of setTimeout() method in phpRedis + * + * Due to some deprecated methods in phpRedis, we need to call the + * specific methods depending on the version of phpRedis. + * + * @var string + */ + protected $_setTimeout_name; + + /** + * Name of delete() method in phpRedis + * + * Due to some deprecated methods in phpRedis, we need to call the + * specific methods depending on the version of phpRedis. + * + * @var string + */ + protected $_delete_name; + + /** + * Response of ping() method in phpRedis + * + * @var mixed + */ + protected $_ping_response; + // ------------------------------------------------------------------------ /** @@ -90,6 +115,20 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle { parent::__construct($params); + // Detect the names of some methods in phpRedis instance + if (version_compare(phpversion('redis'), '5', '>=')) + { + $this->_setTimeout_name = 'expire'; + $this->_delete_name = 'del'; + $this->_ping_response = TRUE; + } + else + { + $this->_setTimeout_name = 'setTimeout'; + $this->_delete_name = 'delete'; + $this->_ping_response = '+PONG'; + } + if (empty($this->_config['save_path'])) { log_message('error', 'Session: No Redis save path configured.'); @@ -170,8 +209,8 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle } else { - $this->php5_validate_id(); $this->_redis = $redis; + $this->php5_validate_id(); return $this->_success; } } @@ -242,7 +281,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle $this->_session_id = $session_id; } - $this->_expire_key($this->_lock_key, 300); + $this->_setTimeout($this->_lock_key, 300); if ($this->_fingerprint !== ($fingerprint = md5($session_data)) OR $this->_key_exists === FALSE) { if ($this->_redis->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration'])) @@ -255,7 +294,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle return $this->_fail(); } - return ($this->_expire_key($this->_key_prefix.$session_id, $this->_config['expiration'])) + return ($this->_setTimeout($this->_key_prefix.$session_id, $this->_config['expiration'])) ? $this->_success : $this->_fail(); } @@ -275,7 +314,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle { try { $ping = $this->_redis->ping(); - if ($ping === '+PONG' OR $ping === TRUE) + if ($ping === $this->_ping_response) { $this->_release_lock(); if ($this->_redis->close() === FALSE) @@ -310,9 +349,9 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle { if (isset($this->_redis, $this->_lock_key)) { - if (($result = $this->_delete_key($this->_key_prefix.$session_id)) !== 1) + if (($result = $this->_delete($this->_key_prefix.$session_id)) !== 1) { - log_message('debug', 'Session: Redis::del() expected to return 1, got '.var_export($result, TRUE).' instead.'); + log_message('debug', 'Session: Redis::'.$this->_delete_name.'() expected to return 1, got '.var_export($result, TRUE).' instead.'); } $this->_cookie_destroy(); @@ -371,7 +410,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle // correct session ID. if ($this->_lock_key === $this->_key_prefix.$session_id.':lock') { - return $this->_expire_key($this->_lock_key, 300); + return $this->_setTimeout($this->_lock_key, 300); } // 30 attempts to obtain a lock, in case another request already has it @@ -429,7 +468,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle { if (isset($this->_redis, $this->_lock_key) && $this->_lock) { - if ( ! $this->_redis->delete($this->_lock_key)) + if ( ! $this->_delete($this->_lock_key)) { log_message('error', 'Session: Error while trying to free lock for '.$this->_lock_key); return FALSE; @@ -445,7 +484,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle // ------------------------------------------------------------------------ /** - * Expire key + * Set timeout * * Sets expiration for a key * @@ -453,28 +492,24 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle * @param int $ttl The key's remaining Time To Live, in seconds. * @return bool */ - protected function _expire_key($key, $ttl) + protected function _setTimeout($key, $ttl) { - if (version_compare(PHPREDIS_VERSION, '5', '>=')) - return $this->_redis->expire($key, $ttl); - return $this->_redis->setTimeout($key, $ttl); + return $this->_redis->{$this->_setTimeout_name}($key, $ttl); } // ------------------------------------------------------------------------ /** - * Delete key + * Delete * * Deletes a key * - * @param string $key Redis key + * @param string $key Redis key * @return bool */ - protected function _delete_key($key) + protected function _delete($key) { - if (version_compare(PHPREDIS_VERSION, '5', '>=')) - return $this->_redis->del($key); - return $this->_redis->delete($key); + return $this->_redis->{$this->_delete_name}($key); } } |