summaryrefslogtreecommitdiffstats
path: root/system/libraries/Cache/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/Cache/drivers')
-rw-r--r--system/libraries/Cache/drivers/Cache_file.php14
-rw-r--r--system/libraries/Cache/drivers/Cache_memcached.php4
-rw-r--r--system/libraries/Cache/drivers/Cache_redis.php61
3 files changed, 65 insertions, 14 deletions
diff --git a/system/libraries/Cache/drivers/Cache_file.php b/system/libraries/Cache/drivers/Cache_file.php
index c6aa848fe..29898040a 100644
--- a/system/libraries/Cache/drivers/Cache_file.php
+++ b/system/libraries/Cache/drivers/Cache_file.php
@@ -92,7 +92,7 @@ class CI_Cache_file extends CI_Driver {
if (write_file($this->_cache_path.$id, serialize($contents)))
{
- @chmod($this->_cache_path.$id, 0660);
+ chmod($this->_cache_path.$id, 0640);
return TRUE;
}
@@ -125,7 +125,11 @@ class CI_Cache_file extends CI_Driver {
{
$data = $this->_get($id);
- if ($data === FALSE OR ! is_int($data['data']))
+ if ($data === FALSE)
+ {
+ $data = array('data' => 0, 'ttl' => 60);
+ }
+ elseif ( ! is_int($data['data']))
{
return FALSE;
}
@@ -149,7 +153,11 @@ class CI_Cache_file extends CI_Driver {
{
$data = $this->_get($id);
- if ($data === FALSE OR ! is_int($data['data']))
+ if ($data === FALSE)
+ {
+ $data = array('data' => 0, 'ttl' => 60);
+ }
+ elseif ( ! is_int($data['data']))
{
return FALSE;
}
diff --git a/system/libraries/Cache/drivers/Cache_memcached.php b/system/libraries/Cache/drivers/Cache_memcached.php
index bed606afb..55b769424 100644
--- a/system/libraries/Cache/drivers/Cache_memcached.php
+++ b/system/libraries/Cache/drivers/Cache_memcached.php
@@ -49,7 +49,7 @@ class CI_Cache_memcached extends CI_Driver {
*
* @var array
*/
- protected $_memcache_conf = array(
+ protected $_memcache_conf = array(
'default' => array(
'host' => '127.0.0.1',
'port' => 11211,
@@ -202,12 +202,12 @@ class CI_Cache_memcached extends CI_Driver {
{
// 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']))
{
- $defaults = $this->_memcache_conf['default'];
$this->_memcache_conf = array();
foreach ($CI->config->config['memcached'] as $name => $conf)
diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php
index 1c76426c5..7c9da3d2e 100644
--- a/system/libraries/Cache/drivers/Cache_redis.php
+++ b/system/libraries/Cache/drivers/Cache_redis.php
@@ -58,6 +58,13 @@ class CI_Cache_redis extends CI_Driver
*/
protected $_redis;
+ /**
+ * An internal cache for storing keys of serialized values.
+ *
+ * @var array
+ */
+ protected $_serialized = array();
+
// ------------------------------------------------------------------------
/**
@@ -68,7 +75,14 @@ class CI_Cache_redis extends CI_Driver
*/
public function get($key)
{
- return $this->_redis->get($key);
+ $value = $this->_redis->get($key);
+
+ if ($value !== FALSE && isset($this->_serialized[$key]))
+ {
+ return unserialize($value);
+ }
+
+ return $value;
}
// ------------------------------------------------------------------------
@@ -84,6 +98,22 @@ class CI_Cache_redis extends CI_Driver
*/
public function save($id, $data, $ttl = 60, $raw = FALSE)
{
+ if (is_array($data) OR is_object($data))
+ {
+ if ( ! $this->_redis->sAdd('_ci_redis_serialized', $id))
+ {
+ return FALSE;
+ }
+
+ isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE;
+ $data = serialize($data);
+ }
+ elseif (isset($this->_serialized[$id]))
+ {
+ $this->_serialized[$id] = NULL;
+ $this->_redis->sRemove('_ci_redis_serialized', $id);
+ }
+
return ($ttl)
? $this->_redis->setex($id, $ttl, $data)
: $this->_redis->set($id, $data);
@@ -99,7 +129,18 @@ class CI_Cache_redis extends CI_Driver
*/
public function delete($key)
{
- return ($this->_redis->delete($key) === 1);
+ 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;
}
// ------------------------------------------------------------------------
@@ -113,9 +154,7 @@ class CI_Cache_redis extends CI_Driver
*/
public function increment($id, $offset = 1)
{
- return $this->_redis->exists($id)
- ? $this->_redis->incr($id, $offset)
- : FALSE;
+ return $this->_redis->incr($id, $offset);
}
// ------------------------------------------------------------------------
@@ -129,9 +168,7 @@ class CI_Cache_redis extends CI_Driver
*/
public function decrement($id, $offset = 1)
{
- return $this->_redis->exists($id)
- ? $this->_redis->decr($id, $offset)
- : FALSE;
+ return $this->_redis->decr($id, $offset);
}
// ------------------------------------------------------------------------
@@ -259,13 +296,19 @@ class CI_Cache_redis extends CI_Driver
$this->_redis->auth($config['password']);
}
+ // Initialize the index of serialized values.
+ $serialized = $this->_redis->sMembers('_ci_redis_serialized');
+ if ( ! empty($serialized))
+ {
+ $this->_serialized = array_flip($serialized);
+ }
+
return TRUE;
}
// ------------------------------------------------------------------------
/**
-
* Class destructor
*
* Closes the connection to Redis if present.