Skip to content

Commit

Permalink
[BUGS-7148] Handle duplicate keys in get_multiple function (#448)
Browse files Browse the repository at this point in the history
* fix: Handle duplicate keys in `get_multiple` function

Signed-off-by: Souptik Datta <souptikdatta2001@gmail.com>

* add readme update

* fix whitespace 🤦‍♂️

---------

Signed-off-by: Souptik Datta <souptikdatta2001@gmail.com>
Co-authored-by: Chris Reynolds <chris.reynolds@pantheon.io>
Co-authored-by: Chris Reynolds <chris@jazzsequence.com>
  • Loading branch information
3 people authored Nov 21, 2023
1 parent 74d54ca commit 35b3c76
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ There's a known issue with WordPress `alloptions` cache design. Specifically, a
### 1.4.4-dev ###
* Updates security policy [[#441](https://github.com/pantheon-systems/wp-redis/pull/441)]
* Updates Pantheon WP Coding Standards to 2.0 [[#445](https://github.com/pantheon-systems/wp-redis/pull/445)]
* Handle duplicate keys in `get_multiple` function [[#448](https://github.com/pantheon-systems/wp-redis/pull/448)] (props @Souptik2001)

### 1.4.3 (June 26, 2023) ###
* Bug fix: Fixes assumption that CACHE_PORT & CACHE_PASSWORD are Set. [[428](https://github.com/pantheon-systems/wp-redis/pull/428)] (props @timnolte)
Expand Down
3 changes: 3 additions & 0 deletions object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,9 @@ public function get_multiple( $keys, $group = 'default', $force = false ) {
$group = 'default';
}

// Get unique keys.
$keys = array_unique( $keys );

$cache = [];
if ( ! $this->_should_persist( $group ) ) {
foreach ( $keys as $key ) {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Please report security bugs found in the source code of the WP Redis plugin thro
= 1.4.4-dev =
* Updates security policy [[#441](https://github.com/pantheon-systems/wp-redis/pull/441)]
* Updates Pantheon WP Coding Standards to 2.0 [[#445](https://github.com/pantheon-systems/wp-redis/pull/445)]
* Handle duplicate keys in `get_multiple` function [[#448](https://github.com/pantheon-systems/wp-redis/pull/448)] (props @souptik)

= 1.4.3 (June 26, 2023) =
* Bug fix: Fixes assumption that CACHE_PORT & CACHE_PASSWORD are Set. [[428](https://github.com/pantheon-systems/wp-redis/pull/428)] (props @tnolte)
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/test-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,31 @@ public function test_get_multiple() {
} else {
$this->assertEmpty( $this->cache->redis_calls );
}

$this->cache->set( 'foo3', 'bar1', 'group1' );
$this->cache->set( 'foo4', 'bar2', 'group1' );

$found = $this->cache->get_multiple( [ 'foo3', 'foo3', 'foo4' ], 'group1', true );

$expected = [
'foo3' => 'bar1',
'foo4' => 'bar2',
];

$this->assertSame( $expected, $found );
$this->assertEquals( 4, $this->cache->cache_hits );
$this->assertEquals( 1, $this->cache->cache_misses );
if ( $this->cache->is_redis_connected ) {
$this->assertEquals(
[
self::$mget_key => 2,
self::$set_key => 5,
],
$this->cache->redis_calls
);
} else {
$this->assertEmpty( $this->cache->redis_calls );
}
}

public function test_get_multiple_partial_cache_miss() {
Expand Down

0 comments on commit 35b3c76

Please sign in to comment.