From 6cd5d9551934c8b0a65dd6dd4ca5534cfdcb5006 Mon Sep 17 00:00:00 2001 From: Ian Jenkins Date: Sat, 21 Dec 2019 14:45:22 +0000 Subject: [PATCH] Update found value to true after set() in memcache. --- object-cache.php | 3 +++ tests/test-object-cache.php | 35 ++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/object-cache.php b/object-cache.php index 3e6e5bb..74b46d4 100644 --- a/object-cache.php +++ b/object-cache.php @@ -491,6 +491,9 @@ function set( $id, $data, $group = 'default', $expire = 0 ) { $mc =& $this->get_mc( $group ); $result = $mc->set( $key, $data, false, $expire ); + // Update the found cache value with the result of the set in memcache. + $this->cache[ $key ][ 'found' ] = $result; + ++$this->stats[ 'set' ]; $this->group_ops[$group][] = "set $id"; diff --git a/tests/test-object-cache.php b/tests/test-object-cache.php index ce82c4e..4f4e346 100644 --- a/tests/test-object-cache.php +++ b/tests/test-object-cache.php @@ -769,20 +769,20 @@ public function test_replace_can_be_performed_per_group(): void { public function test_set_returns_true_if_added_successfully(): void { // String. - $added_string = $this->object_cache->set( 'foo1', 'data' ); + $set_string = $this->object_cache->set( 'foo1', 'data' ); - $this->assertTrue( $added_string ); + $this->assertTrue( $set_string ); // Array. - $added_arr = $this->object_cache->set( 'foo2', [ 'data' ] ); + $set_arr = $this->object_cache->set( 'foo2', [ 'data' ] ); - $this->assertTrue( $added_arr ); + $this->assertTrue( $set_arr ); // Object. $data = new StdClass(); - $added_obj = $this->object_cache->set( 'foo3', $data ); + $set_obj = $this->object_cache->set( 'foo3', $data ); - $this->assertTrue( $added_obj ); + $this->assertTrue( $set_obj ); } public function test_set_returns_false_if_cached_value_of_checkthedatabaseplease(): void { @@ -804,7 +804,7 @@ public function test_set_caches_data_locally(): void { $expected = [ 'value' => 'data', - 'found' => false, + 'found' => true, ]; $this->assertEquals( $expected, $this->object_cache->cache[ $key ] ); @@ -826,6 +826,27 @@ public function test_set_can_be_performed_per_group(): void { $this->object_cache->set( 'foo1', 'data2', 'group2' ); $this->assertEquals( 'data2', $this->object_cache->get( 'foo1', 'group2' ) ); } + + public function test_set_updated_found_status_with_memcache_result(): void { + // Need this to ensure cache is set witout going to memcache. + $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' ] ); + + // Remove non-persistent group. + $this->object_cache->no_mc_groups = []; + + // Re-set. + $this->object_cache->set( 'foo', 'data', $group ); + + // Cached found value should now be true. + $this->assertTrue( $this->object_cache->cache[ $key ][ 'found' ] ); + } // Tests for switch_to_blog.