Skip to content

Commit d8084f4

Browse files
authored
GH-79: Implement wp_cache_(add|delete|set)_multiple (#87)
* Implement `wp_cache_(add|delete|set)_multiple` * Add tests Tests were taken from https://github.com/WordPress/wordpress-develop/blob/5d7297d/tests/phpunit/tests/cache.php
1 parent f125409 commit d8084f4

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

object-cache.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
2222
return $wp_object_cache->add( $key, $data, $group, $expire );
2323
}
2424

25+
function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
26+
global $wp_object_cache;
27+
28+
return $wp_object_cache->add_multiple( $data, $group, $expire );
29+
}
30+
2531
function wp_cache_incr( $key, $n = 1, $group = '' ) {
2632
global $wp_object_cache;
2733

@@ -46,6 +52,12 @@ function wp_cache_delete( $key, $group = '' ) {
4652
return $wp_object_cache->delete( $key, $group );
4753
}
4854

55+
function wp_cache_delete_multiple( array $keys, $group = '' ) {
56+
global $wp_object_cache;
57+
58+
return $wp_object_cache->delete_multiple( $keys, $group );
59+
}
60+
4961
function wp_cache_flush() {
5062
global $wp_object_cache;
5163

@@ -98,6 +110,12 @@ function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
98110
}
99111
}
100112

113+
function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
114+
global $wp_object_cache;
115+
116+
return $wp_object_cache->set_multiple( $data, $group, $expire );
117+
}
118+
101119
function wp_cache_switch_to_blog( $blog_id ) {
102120
global $wp_object_cache;
103121

@@ -210,6 +228,16 @@ function add( $id, $data, $group = 'default', $expire = 0 ) {
210228
return $result;
211229
}
212230

231+
public function add_multiple( array $data, $group = '', $expire = 0 ) {
232+
$values = array();
233+
234+
foreach ( $data as $key => $value ) {
235+
$values[ $key ] = $this->add( $key, $value, $group, $expire );
236+
}
237+
238+
return $values;
239+
}
240+
213241
function add_global_groups( $groups ) {
214242
if ( ! is_array( $groups ) ) {
215243
$groups = (array) $groups;
@@ -285,6 +313,16 @@ function delete( $id, $group = 'default' ) {
285313
return $result;
286314
}
287315

316+
public function delete_multiple( array $keys, $group = '' ) {
317+
$values = array();
318+
319+
foreach ( $keys as $key ) {
320+
$values[ $key ] = $this->delete( $key, $group );
321+
}
322+
323+
return $values;
324+
}
325+
288326
// Gets number from all default servers, replicating if needed
289327
function get_max_flush_number( $group ) {
290328
$key = $this->key( $this->flush_key, $group );
@@ -633,6 +671,16 @@ function set( $id, $data, $group = 'default', $expire = 0 ) {
633671
return $result;
634672
}
635673

674+
public function set_multiple( array $data, $group = '', $expire = 0 ) {
675+
$values = array();
676+
677+
foreach ( $data as $key => $value ) {
678+
$values[ $key ] = $this->set( $key, $value, $group, $expire );
679+
}
680+
681+
return $values;
682+
}
683+
636684
function switch_to_blog( $blog_id ) {
637685
global $table_prefix;
638686

tests/test-object-cache.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,4 +1041,61 @@ public function test_colorize_debug_line( $command, $expected_color ) {
10411041

10421042
$this->assertStringContainsString( $expected_color, $colorized );
10431043
}
1044+
1045+
public function test_wp_cache_add_multiple() {
1046+
$found = wp_cache_add_multiple(
1047+
array(
1048+
'foo1' => 'bar',
1049+
'foo2' => 'bar',
1050+
'foo3' => 'bar',
1051+
),
1052+
'group1'
1053+
);
1054+
1055+
$expected = array(
1056+
'foo1' => true,
1057+
'foo2' => true,
1058+
'foo3' => true,
1059+
);
1060+
1061+
$this->assertSame( $expected, $found );
1062+
}
1063+
1064+
public function test_wp_cache_set_multiple() {
1065+
$found = wp_cache_set_multiple(
1066+
array(
1067+
'foo1' => 'bar',
1068+
'foo2' => 'bar',
1069+
'foo3' => 'bar',
1070+
),
1071+
'group1'
1072+
);
1073+
1074+
$expected = array(
1075+
'foo1' => true,
1076+
'foo2' => true,
1077+
'foo3' => true,
1078+
);
1079+
1080+
$this->assertSame( $expected, $found );
1081+
}
1082+
1083+
public function test_wp_cache_delete_multiple() {
1084+
wp_cache_set( 'foo1', 'bar', 'group1' );
1085+
wp_cache_set( 'foo2', 'bar', 'group1' );
1086+
wp_cache_set( 'foo3', 'bar', 'group2' );
1087+
1088+
$found = wp_cache_delete_multiple(
1089+
array( 'foo1', 'foo2', 'foo3' ),
1090+
'group1'
1091+
);
1092+
1093+
$expected = array(
1094+
'foo1' => true,
1095+
'foo2' => true,
1096+
'foo3' => false,
1097+
);
1098+
1099+
$this->assertSame( $expected, $found );
1100+
}
10441101
}

0 commit comments

Comments
 (0)