From 94ebea018b9910fff98e57ea43680a7f72cc442b Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Thu, 18 Jun 2020 11:46:50 +0100 Subject: [PATCH 1/2] Add test for Issue #61 --- tests/test-object-cache.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test-object-cache.php b/tests/test-object-cache.php index 4d38af9..7d72d16 100644 --- a/tests/test-object-cache.php +++ b/tests/test-object-cache.php @@ -145,6 +145,26 @@ public function test_add_non_persistent_groups_does_not_allow_duplicate_groups() $this->assertCount( 1, array_keys( $this->object_cache->no_mc_groups, 'group-1' ) ); } + public function test_found_is_set_when_using_non_persistent_groups(): void { + /** + * wp> wp_cache_add_non_persistent_groups( [ 'example' ] ); + * NULL + * wp> wp_cache_set( 'example', 'example', 'example' ); + * bool(true) + * wp> wp_cache_get( 'example', 'example', false, $found ); + * string(7) "example" + * wp> $found + * bool(false) + */ + + $groups = [ 'example' ]; + $this->object_cache->add_non_persistent_groups( $groups ); + $this->object_cache->set( 'example', 'example', 'example' ); + $this->object_cache->get( 'example', 'example', false, $found ); + + $this->assertTrue( $found ); + } + // Tests for increment. public function test_incr_increments_a_numeric_value(): void { From cfc4970b89cd2ca2f912e1f6f010e2579b14f837 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Fri, 19 Jun 2020 08:30:15 +0100 Subject: [PATCH 2/2] Set found to true when using non persistent group. Fixes #61. --- object-cache.php | 4 ++- tests/test-object-cache.php | 51 ++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/object-cache.php b/object-cache.php index cdf1e5a..b9e6926 100644 --- a/object-cache.php +++ b/object-cache.php @@ -508,7 +508,9 @@ function set( $id, $data, $group = 'default', $expire = 0 ) { ]; if ( in_array( $group, $this->no_mc_groups ) ) { - $this->group_ops_stats( 'set_local', $key, $group, null, null ); + $this->group_ops_stats( 'set_local', $key, $group, null, null ); + // If using a non persistent group it will never be in memcache but will be found in our local cache so set found to true. + $this->cache[ $key ]['found'] = true; return true; } diff --git a/tests/test-object-cache.php b/tests/test-object-cache.php index 7d72d16..0f6681e 100644 --- a/tests/test-object-cache.php +++ b/tests/test-object-cache.php @@ -145,26 +145,6 @@ public function test_add_non_persistent_groups_does_not_allow_duplicate_groups() $this->assertCount( 1, array_keys( $this->object_cache->no_mc_groups, 'group-1' ) ); } - public function test_found_is_set_when_using_non_persistent_groups(): void { - /** - * wp> wp_cache_add_non_persistent_groups( [ 'example' ] ); - * NULL - * wp> wp_cache_set( 'example', 'example', 'example' ); - * bool(true) - * wp> wp_cache_get( 'example', 'example', false, $found ); - * string(7) "example" - * wp> $found - * bool(false) - */ - - $groups = [ 'example' ]; - $this->object_cache->add_non_persistent_groups( $groups ); - $this->object_cache->set( 'example', 'example', 'example' ); - $this->object_cache->get( 'example', 'example', false, $found ); - - $this->assertTrue( $found ); - } - // Tests for increment. public function test_incr_increments_a_numeric_value(): void { @@ -848,15 +828,15 @@ public function test_set_can_be_performed_per_group(): void { } public function test_set_updated_found_status_with_memcache_result(): void { - // Need this to ensure cache is set witout going to memcache. + // Need this to ensure cache is set witout going to memcache and using non persistent groups. $group = 'do-not-persist-me'; $this->object_cache->add_non_persistent_groups( [ $group ] ); $this->object_cache->set( 'foo', 'data', $group ); $key = $this->object_cache->key( 'foo', $group ); - // Check it's false until we use memcache. - $this->assertFalse( $this->object_cache->cache[ $key ][ 'found' ] ); + // Check it's true even though we're not using memcache. + $this->assertTrue( $this->object_cache->cache[ $key ][ 'found' ] ); // Remove non-persistent group. $this->object_cache->no_mc_groups = []; @@ -864,10 +844,33 @@ public function test_set_updated_found_status_with_memcache_result(): void { // Re-set. $this->object_cache->set( 'foo', 'data', $group ); - // Cached found value should now be true. + // Cached found value should still be true. $this->assertTrue( $this->object_cache->cache[ $key ][ 'found' ] ); } + /** + * @see https://github.com/Automattic/wp-memcached/issues/61 + */ + public function test_found_is_set_when_using_non_persistent_groups(): void { + /** + * wp> wp_cache_add_non_persistent_groups( [ 'example' ] ); + * NULL + * wp> wp_cache_set( 'example', 'example', 'example' ); + * bool(true) + * wp> wp_cache_get( 'example', 'example', false, $found ); + * string(7) "example" + * wp> $found + * bool(false) + */ + + $groups = [ 'example' ]; + $this->object_cache->add_non_persistent_groups( $groups ); + $this->object_cache->set( 'example', 'example', 'example' ); + $this->object_cache->get( 'example', 'example', false, $found ); + + $this->assertTrue( $found ); + } + // Tests for switch_to_blog. public function test_switch_to_blog_sets_blog_prefix_depending_on_multi_site_status(): void {