Skip to content
Snippets Groups Projects
Commit f6634dfe authored by Erick Hitter's avatar Erick Hitter
Browse files

Add handling for the various responses Predis returns after performing an...

Add handling for the various responses Predis returns after performing an action, ensuring a boolean always results.
parent 277e59e6
No related branches found
No related tags found
No related merge requests found
...@@ -372,9 +372,9 @@ class WP_Object_Cache { ...@@ -372,9 +372,9 @@ class WP_Object_Cache {
// Save to Redis // Save to Redis
$expiration = absint( $expiration ); $expiration = absint( $expiration );
if ( $expiration ) { if ( $expiration ) {
$result = $this->redis->setex( $derived_key, $expiration, $this->prepare_value_for_redis( $value ) ); $result = $this->parse_predis_response( $this->redis->setex( $derived_key, $expiration, $this->prepare_value_for_redis( $value ) ) );
} else { } else {
$result = $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ); $result = $this->parse_predis_response( $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ) );
} }
return $result; return $result;
...@@ -399,7 +399,7 @@ class WP_Object_Cache { ...@@ -399,7 +399,7 @@ class WP_Object_Cache {
} }
} }
$result = $this->redis->del( $derived_key ); $result = $this->parse_predis_response( $this->redis->del( $derived_key ) );
unset( $this->cache[$derived_key] ); unset( $this->cache[$derived_key] );
...@@ -420,7 +420,7 @@ class WP_Object_Cache { ...@@ -420,7 +420,7 @@ class WP_Object_Cache {
} }
$this->cache = array(); $this->cache = array();
$result = $this->redis->flushall(); $result = $this->parse_predis_response( $this->redis->flushall() );
return $result; return $result;
} }
...@@ -486,9 +486,9 @@ class WP_Object_Cache { ...@@ -486,9 +486,9 @@ class WP_Object_Cache {
// Save to Redis // Save to Redis
$expiration = absint( $expiration ); $expiration = absint( $expiration );
if ( $expiration ) { if ( $expiration ) {
$result = $this->redis->setex( $derived_key, $expiration, $this->prepare_value_for_redis( $value ) ); $result = $this->parse_predis_response( $this->redis->setex( $derived_key, $expiration, $this->prepare_value_for_redis( $value ) ) );
} else { } else {
$result = $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ); $result = $this->parse_predis_response( $this->redis->set( $derived_key, $this->prepare_value_for_redis( $value ) ) );
} }
return $result; return $result;
...@@ -516,7 +516,7 @@ class WP_Object_Cache { ...@@ -516,7 +516,7 @@ class WP_Object_Cache {
} }
// Save to Redis // Save to Redis
$result = $this->redis->incrBy( $derived_key, $offset ); $result = $this->parse_predis_response( $this->redis->incrBy( $derived_key, $offset ) );
$this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) ); $this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) );
...@@ -545,7 +545,7 @@ class WP_Object_Cache { ...@@ -545,7 +545,7 @@ class WP_Object_Cache {
} }
// Save to Redis // Save to Redis
$result = $this->redis->decrBy( $derived_key, $offset ); $result = $this->parse_predis_response( $this->redis->decrBy( $derived_key, $offset ) );
$this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) ); $this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) );
...@@ -621,6 +621,28 @@ class WP_Object_Cache { ...@@ -621,6 +621,28 @@ class WP_Object_Cache {
return $value; return $value;
} }
/**
* Convert the response fro Predis into something meaningful
*
* @param mixed $response
* @return mixed
*/
protected function parse_predis_response( $response ) {
if ( is_bool( $response ) ) {
return $response;
}
if ( is_numeric( $response ) ) {
return (bool) $response;
}
if ( is_object( $response ) && method_exists( $response, 'getPayload' ) ) {
return 'OK' === $response->getPayload();
}
return false;
}
/** /**
* Simple wrapper for saving object to the internal cache. * Simple wrapper for saving object to the internal cache.
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment