summaryrefslogtreecommitdiffstats
path: root/system/libraries/Cache/drivers/Cache_redis.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/Cache/drivers/Cache_redis.php')
-rw-r--r--system/libraries/Cache/drivers/Cache_redis.php102
1 files changed, 51 insertions, 51 deletions
diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php
index bfd620436..1629031ce 100644
--- a/system/libraries/Cache/drivers/Cache_redis.php
+++ b/system/libraries/Cache/drivers/Cache_redis.php
@@ -55,11 +55,11 @@ class CI_Cache_redis extends CI_Driver
* @var array
*/
protected static $_default_config = array(
- 'socket_type' => 'tcp',
'host' => '127.0.0.1',
'password' => NULL,
'port' => 6379,
- 'timeout' => 0
+ 'timeout' => 0,
+ 'database' => 0
);
/**
@@ -69,13 +69,6 @@ class CI_Cache_redis extends CI_Driver
*/
protected $_redis;
- /**
- * An internal cache for storing keys of serialized values.
- *
- * @var array
- */
- protected $_serialized = array();
-
// ------------------------------------------------------------------------
/**
@@ -112,16 +105,7 @@ class CI_Cache_redis extends CI_Driver
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)
+ if ( ! $this->_redis->connect($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout']))
{
log_message('error', 'Cache: Redis connection failed. Check your configuration.');
}
@@ -130,15 +114,16 @@ class CI_Cache_redis extends CI_Driver
{
log_message('error', 'Cache: Redis authentication failed.');
}
+
+ if (isset($config['database']) && $config['database'] > 0 && ! $this->_redis->select($config['database']))
+ {
+ log_message('error', 'Cache: Redis select database failed.');
+ }
}
catch (RedisException $e)
{
log_message('error', 'Cache: Redis connection refused ('.$e->getMessage().')');
}
-
- // Initialize the index of serialized values.
- $serialized = $this->_redis->sMembers('_ci_redis_serialized');
- empty($serialized) OR $this->_serialized = array_flip($serialized);
}
// ------------------------------------------------------------------------
@@ -151,14 +136,30 @@ class CI_Cache_redis extends CI_Driver
*/
public function get($key)
{
- $value = $this->_redis->get($key);
+ $data = $this->_redis->hMGet($key, array('__ci_type', '__ci_value'));
- if ($value !== FALSE && isset($this->_serialized[$key]))
+ if ( ! isset($data['__ci_type'], $data['__ci_value']) OR $data['__ci_value'] === FALSE)
{
- return unserialize($value);
+ return FALSE;
}
- return $value;
+ switch ($data['__ci_type'])
+ {
+ case 'array':
+ case 'object':
+ return unserialize($data['__ci_value']);
+ case 'boolean':
+ case 'integer':
+ case 'double': // Yes, 'double' is returned and NOT 'float'
+ case 'string':
+ case 'NULL':
+ return settype($data['__ci_value'], $data['__ci_type'])
+ ? $data['__ci_value']
+ : FALSE;
+ case 'resource':
+ default:
+ return FALSE;
+ }
}
// ------------------------------------------------------------------------
@@ -174,23 +175,33 @@ class CI_Cache_redis extends CI_Driver
*/
public function save($id, $data, $ttl = 60, $raw = FALSE)
{
- if (is_array($data) OR is_object($data))
+ switch ($data_type = gettype($data))
{
- if ( ! $this->_redis->sIsMember('_ci_redis_serialized', $id) && ! $this->_redis->sAdd('_ci_redis_serialized', $id))
- {
+ case 'array':
+ case 'object':
+ $data = serialize($data);
+ break;
+ case 'boolean':
+ case 'integer':
+ case 'double': // Yes, 'double' is returned and NOT 'float'
+ case 'string':
+ case 'NULL':
+ break;
+ case 'resource':
+ default:
return FALSE;
- }
+ }
- isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE;
- $data = serialize($data);
+ if ( ! $this->_redis->hMSet($id, array('__ci_type' => $data_type, '__ci_value' => $data)))
+ {
+ return FALSE;
}
- elseif (isset($this->_serialized[$id]))
+ elseif ($ttl)
{
- $this->_serialized[$id] = NULL;
- $this->_redis->sRemove('_ci_redis_serialized', $id);
+ $this->_redis->expireAt($id, time() + $ttl);
}
- return $this->_redis->set($id, $data, $ttl);
+ return TRUE;
}
// ------------------------------------------------------------------------
@@ -203,18 +214,7 @@ class CI_Cache_redis extends CI_Driver
*/
public function delete($key)
{
- if ($this->_redis->delete($key) !== 1)
- {
- return FALSE;
- }
-
- if (isset($this->_serialized[$key]))
- {
- $this->_serialized[$key] = NULL;
- $this->_redis->sRemove('_ci_redis_serialized', $key);
- }
-
- return TRUE;
+ return ($this->_redis->delete($key) === 1);
}
// ------------------------------------------------------------------------
@@ -228,7 +228,7 @@ class CI_Cache_redis extends CI_Driver
*/
public function increment($id, $offset = 1)
{
- return $this->_redis->incr($id, $offset);
+ return $this->_redis->hIncrBy($id, 'data', $offset);
}
// ------------------------------------------------------------------------
@@ -242,7 +242,7 @@ class CI_Cache_redis extends CI_Driver
*/
public function decrement($id, $offset = 1)
{
- return $this->_redis->decr($id, $offset);
+ return $this->_redis->hIncrBy($id, 'data', -$offset);
}
// ------------------------------------------------------------------------