diff options
author | Andrey Andreev <narf@devilix.net> | 2016-03-12 12:47:12 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2016-03-12 12:47:12 +0100 |
commit | 8de036be27b55cc7587f0dfcb1c0fbcc88c5cf8f (patch) | |
tree | 8ec72594cbe7e57eeada34324f54ed7cb3b4a84b /system/libraries | |
parent | 1c8a6fdbe7e07aba97a4a604053446421f2eed42 (diff) | |
parent | 0abb9777fd45cf87dc3e0e4780ae2292b2822407 (diff) |
Merge pull request #4528 from masterklavi/redis_data_type
Refactor data serialization logic for Cache_redis
Diffstat (limited to 'system/libraries')
-rw-r--r-- | system/libraries/Cache/drivers/Cache_redis.php | 48 |
1 files changed, 18 insertions, 30 deletions
diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php index d4d95ebb1..6da86728c 100644 --- a/system/libraries/Cache/drivers/Cache_redis.php +++ b/system/libraries/Cache/drivers/Cache_redis.php @@ -151,14 +151,22 @@ class CI_Cache_redis extends CI_Driver */ public function get($key) { - $value = $this->_redis->get($key); + $value = $this->_redis->hMGet($key, array('type', 'data')); - if ($value !== FALSE && isset($this->_serialized[$key])) + if (empty($value['type'])) { - return unserialize($value); + return FALSE; } - return $value; + if ($value['type'] === 'array' OR $value['type'] === 'object') + { + return unserialize($value['data']); + } + else + { + settype($value['data'], $value['type']); + return $value['data']; + } } // ------------------------------------------------------------------------ @@ -176,21 +184,12 @@ class CI_Cache_redis extends CI_Driver { if (is_array($data) OR is_object($data)) { - if ( ! $this->_redis->sIsMember('_ci_redis_serialized', $id) && ! $this->_redis->sAdd('_ci_redis_serialized', $id)) - { - return FALSE; - } - - isset($this->_serialized[$id]) OR $this->_serialized[$id] = TRUE; - $data = serialize($data); + return $this->_redis->hMSet($id, array('type' => gettype($data), 'data' => serialize($data))); } - elseif (isset($this->_serialized[$id])) + else { - $this->_serialized[$id] = NULL; - $this->_redis->sRemove('_ci_redis_serialized', $id); + return $this->_redis->hMSet($id, array('type' => gettype($data), 'data' => $data)); } - - return $this->_redis->set($id, $data, $ttl); } // ------------------------------------------------------------------------ @@ -203,18 +202,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 +216,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 +230,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); } // ------------------------------------------------------------------------ |