diff options
author | Andrey Andreev <narf@devilix.net> | 2014-10-05 23:04:19 +0200 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-10-05 23:04:19 +0200 |
commit | 4a485a73d64a8bebc7625aabc5fdc361d5e7dc56 (patch) | |
tree | cb18690c4de1c6ca008227260f9dd0cd2b9279d2 /system/libraries/Cache | |
parent | 39ec29585b7cdca7edc1a0757c913a13a2ee4f85 (diff) | |
parent | d444d445ed0458a352ecb9ff79ffd158677ee805 (diff) |
Merge branch 'develop' into feature/session
Diffstat (limited to 'system/libraries/Cache')
-rw-r--r-- | system/libraries/Cache/drivers/Cache_file.php | 14 | ||||
-rw-r--r-- | system/libraries/Cache/drivers/Cache_memcached.php | 4 | ||||
-rw-r--r-- | system/libraries/Cache/drivers/Cache_redis.php | 61 |
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. |