From 15eb98a01bacd77d8643c20372f7ef2e41241866 Mon Sep 17 00:00:00 2001 From: Shmuel Krakower Date: Tue, 3 Feb 2015 15:21:21 +0200 Subject: [PATCH 1/3] falling back to internal cache Seems like in case of no available memcached server, the behavior is to not have any caching. Instead - the behavior I suggest is to fallback to internal case. --- object-cache.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/object-cache.php b/object-cache.php index 51f3e91..364d017 100644 --- a/object-cache.php +++ b/object-cache.php @@ -899,9 +899,8 @@ public function add( $key, $value, $group = 'default', $expiration = 0, $server_ else $result = $this->m->add( $derived_key, $value, $expiration ); - // Store in runtime cache if add was successful - if ( Memcached::RES_SUCCESS === $this->getResultCode() ) - $this->add_to_internal_cache( $derived_key, $value ); + // Store in runtime cache anyway + $this->add_to_internal_cache( $derived_key, $value ); return $result; } From ddda74bb600984b08000c16139ae102f033d4e65 Mon Sep 17 00:00:00 2001 From: Shmuel Krakower Date: Tue, 3 Feb 2015 18:18:25 +0200 Subject: [PATCH 2/3] fixing due to failing unit tests --- object-cache.php | 70 +++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/object-cache.php b/object-cache.php index 364d017..dc494a8 100644 --- a/object-cache.php +++ b/object-cache.php @@ -867,43 +867,39 @@ public function __construct( $persistent_id = NULL ) { * @param bool $byKey True to store in internal cache by key; false to not store by key * @return bool Returns TRUE on success or FALSE on failure. */ - public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) { - /* - * Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php - * file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add - * is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because - * wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded. - */ - if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) { - return false; - } - - $derived_key = $this->buildKey( $key, $group ); - $expiration = $this->sanitize_expiration( $expiration ); - - // If group is a non-Memcached group, save to runtime cache, not Memcached - if ( in_array( $group, $this->no_mc_groups ) ) { - - // Add does not set the value if the key exists; mimic that here - if ( isset( $this->cache[$derived_key] ) ) - return false; - - $this->add_to_internal_cache( $derived_key, $value ); - - return true; - } - - // Save to Memcached - if ( $byKey ) - $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration ); - else - $result = $this->m->add( $derived_key, $value, $expiration ); - - // Store in runtime cache anyway - $this->add_to_internal_cache( $derived_key, $value ); - - return $result; - } + public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) { + /* + * Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php + * file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add + * is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because + * wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded. + */ + if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) { + return false; + } + + $derived_key = $this->buildKey( $key, $group ); + $expiration = $this->sanitize_expiration( $expiration ); + + // Skip saving to Memcached if group is a non-Memcached group + if ( ! in_array( $group, $this->no_mc_groups ) ) { + // Save to Memcached + if ( $byKey ) + $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration ); + else + $result = $this->m->add( $derived_key, $value, $expiration ); + } + + // Add does not set the value if the key exists; mimic that here + if ( isset( $this->cache[$derived_key] ) ) + $result=false; + else{ + $this->add_to_internal_cache( $derived_key, $value ); + $result=true; + } + + return $result; + } /** * Adds a value to cache on a specific server. From 1a9199a935e2dd5568712f1dc2dc2805ba08eb69 Mon Sep 17 00:00:00 2001 From: Shmuel Krakower Date: Tue, 3 Feb 2015 18:24:50 +0200 Subject: [PATCH 3/3] fixing indentation to tabs --- object-cache.php | 66 ++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/object-cache.php b/object-cache.php index dc494a8..69daede 100644 --- a/object-cache.php +++ b/object-cache.php @@ -867,39 +867,39 @@ public function __construct( $persistent_id = NULL ) { * @param bool $byKey True to store in internal cache by key; false to not store by key * @return bool Returns TRUE on success or FALSE on failure. */ - public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) { - /* - * Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php - * file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add - * is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because - * wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded. - */ - if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) { - return false; - } - - $derived_key = $this->buildKey( $key, $group ); - $expiration = $this->sanitize_expiration( $expiration ); - - // Skip saving to Memcached if group is a non-Memcached group - if ( ! in_array( $group, $this->no_mc_groups ) ) { - // Save to Memcached - if ( $byKey ) - $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration ); - else - $result = $this->m->add( $derived_key, $value, $expiration ); - } - - // Add does not set the value if the key exists; mimic that here - if ( isset( $this->cache[$derived_key] ) ) - $result=false; - else{ - $this->add_to_internal_cache( $derived_key, $value ); - $result=true; - } - - return $result; - } + public function add( $key, $value, $group = 'default', $expiration = 0, $server_key = '', $byKey = false ) { + /* + * Ensuring that wp_suspend_cache_addition is defined before calling, because sometimes an advanced-cache.php + * file will load object-cache.php before wp-includes/functions.php is loaded. In those cases, if wp_cache_add + * is called in advanced-cache.php before any more of WordPress is loaded, we get a fatal error because + * wp_suspend_cache_addition will not be defined until wp-includes/functions.php is loaded. + */ + if ( function_exists( 'wp_suspend_cache_addition' ) && wp_suspend_cache_addition() ) { + return false; + } + + $derived_key = $this->buildKey( $key, $group ); + $expiration = $this->sanitize_expiration( $expiration ); + + // Skip saving to Memcached if group is a non-Memcached group + if ( ! in_array( $group, $this->no_mc_groups ) ) { + // Save to Memcached + if ( $byKey ) + $result = $this->m->addByKey( $server_key, $derived_key, $value, $expiration ); + else + $result = $this->m->add( $derived_key, $value, $expiration ); + } + + // Add does not set the value if the key exists; mimic that here + if ( isset( $this->cache[$derived_key] ) ) + $result=false; + else{ + $this->add_to_internal_cache( $derived_key, $value ); + $result=true; + } + + return $result; + } /** * Adds a value to cache on a specific server.