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 4d38af9..0f6681e 100644 --- a/tests/test-object-cache.php +++ b/tests/test-object-cache.php @@ -828,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 = []; @@ -844,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 {