Skip to content

Commit

Permalink
Update found value to true after set() in memcache.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Jenkins committed Dec 21, 2019
1 parent 6ff2698 commit 6cd5d95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
3 changes: 3 additions & 0 deletions object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
35 changes: 28 additions & 7 deletions tests/test-object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ] );
Expand All @@ -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.

Expand Down

0 comments on commit 6cd5d95

Please sign in to comment.