From 01b7283c06c13067678e1fcf196667130f95c87d Mon Sep 17 00:00:00 2001 From: Erick Hitter <ehitter@gmail.com> Date: Thu, 27 Feb 2014 11:19:25 -0800 Subject: [PATCH] Serialize data when necessary as Redis only accepts strings. Also, properly store non-expiring values. --- object-cache.php | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/object-cache.php b/object-cache.php index ecd0d8d..d63bc0b 100644 --- a/object-cache.php +++ b/object-cache.php @@ -853,7 +853,12 @@ class WP_Object_Cache { } // Save to Redis - $result = $this->redis->setex( $derived_key, $value, $expiration ); + $expiration = absint( $expiration ); + if ( $expiration ) { + $result = $this->redis->setex( $derived_key, $this->prepare_value_for_redis( $value ), $expiration ); + } else { + $result = $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ); + } return $result; } @@ -924,14 +929,14 @@ class WP_Object_Cache { $derived_key = $this->build_key( $key, $group ); if ( ! in_array( $group, $this->no_redis_groups ) ) { - $value = $this->redis->get( $derived_key ); + $value = $this->redis->get( $this->restore_value_from_redis( $derived_key ) ); } else { if ( isset( $this->cache[$derived_key] ) ) { return is_object( $this->cache[$derived_key] ) ? clone $this->cache[$derived_key] : $this->cache[$derived_key]; } elseif ( in_array( $group, $this->no_redis_groups ) ) { return false; } else { - $value = $this->redis->get( $derived_key ); + $value = $this->redis->get( $this->restore_value_from_redis( $derived_key ) ); } } @@ -967,7 +972,12 @@ class WP_Object_Cache { } // Save to Redis - $result = $this->redis->setex( $derived_key, $value, absint( $expiration ) ); + $expiration = absint( $expiration ); + if ( $expiration ) { + $result = $this->redis->setex( $derived_key, $this->prepare_value_for_redis( $value ), $expiration ); + } else { + $result = $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ); + } $this->add_to_internal_cache( $derived_key, $value ); @@ -999,6 +1009,26 @@ class WP_Object_Cache { return preg_replace( '/\s+/', '', WP_CACHE_KEY_SALT . "$prefix$group:$key" ); } + /** + * Prepare a value for storage in Redis, which only accepts strings + * + * @param mixed $value + * @return string + */ + protected function prepare_value_for_redis( $value ) { + $value = maybe_serialize( $value ); + } + + /** + * Restore a value stored in Redis to its original data type + * + * @param string $value + * @return mixed + */ + protected function restore_value_from_redis( $value ) { + $value = maybe_unserialize( $value ); + } + /** * Simple wrapper for saving object to the internal cache. * -- GitLab