summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2014-08-18 14:57:40 +0200
committerAndrey Andreev <narf@devilix.net>2014-08-18 14:57:40 +0200
commit5e9b936390f0b72ee899985bd21204b50a7c917e (patch)
tree899d7c109c297a56cc90a4b324dfcdd27522ae65
parent6c52096f4f9147244e9631b8040088025ae6e79d (diff)
parent2dcdd060fe8b4abdbb77d0535b9fc9ebf9f9f311 (diff)
Merge pull request #3186 from ivantcholakov/feature/cache-redis-serialization
Cache_redis: Adding serialization support.
-rw-r--r--system/libraries/Cache/drivers/Cache_redis.php51
1 files changed, 48 insertions, 3 deletions
diff --git a/system/libraries/Cache/drivers/Cache_redis.php b/system/libraries/Cache/drivers/Cache_redis.php
index 7a2b70382..b8f3f5d9a 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;
+
// ------------------------------------------------------------------------
/**
@@ -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 && in_array($key, $this->_serialized, TRUE))
+ {
+ return unserialize($value);
+ }
+
+ return $value;
}
// ------------------------------------------------------------------------
@@ -84,6 +98,23 @@ class CI_Cache_redis extends CI_Driver
*/
public function save($id, $data, $ttl = 60, $raw = FALSE)
{
+ if (is_array($data) OR is_object($data))
+ {
+ $data = serialize($data);
+
+ if ( ! in_array($id, $this->_serialized, TRUE))
+ {
+ $this->_serialized[] = $id;
+ }
+
+ $this->_redis->sAdd('_ci_redis_serialized', $id);
+ }
+ elseif (($index_key = array_search($id, $this->_serialized, TRUE)) !== FALSE)
+ {
+ unset($this->_serialized[$index_key]);
+ $this->_redis->sRemove('_ci_redis_serialized', $id);
+ }
+
return ($ttl)
? $this->_redis->setex($id, $ttl, $data)
: $this->_redis->set($id, $data);
@@ -99,7 +130,13 @@ class CI_Cache_redis extends CI_Driver
*/
public function delete($key)
{
- return ($this->_redis->delete($key) === 1);
+ if ($this->_redis->delete($key) === 1 && in_array($key, $this->_serialized, TRUE))
+ {
+ $this->_redis->sRemove('_ci_redis_serialized', $key);
+ return TRUE;
+ }
+
+ return FALSE;
}
// ------------------------------------------------------------------------
@@ -255,13 +292,21 @@ class CI_Cache_redis extends CI_Driver
$this->_redis->auth($config['password']);
}
+ // Initialize the index of serialized values.
+ $this->_serialized = $this->_redis->sMembers('_ci_redis_serialized');
+
+ if (empty($this->_serialized))
+ {
+ // On error FALSE is returned, ensure array type for empty index.
+ $this->_serialized = array();
+ }
+
return TRUE;
}
// ------------------------------------------------------------------------
/**
-
* Class destructor
*
* Closes the connection to Redis if present.