Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8a7dbb4
Added clearing of cache groups
pbearne Jan 4, 2022
bdca39b
Update src/wp-includes/cache.php
pbearne Jan 6, 2022
f87e341
removed full cache if group cache fails
pbearne Jan 7, 2022
a9b1db9
whitespace
pbearne Jan 7, 2022
576d82c
comments
pbearne Jan 7, 2022
21e6eac
refactor function and new test for new functions
pbearne Jan 10, 2022
f92ef14
dumped the function down
pbearne Jan 20, 2022
7abf5d6
removed not needed tests
pbearne Jan 20, 2022
ee6f101
removed not needed function
pbearne Feb 2, 2022
3d89efd
Merge branch 'trunk' into INITS-115-Support_flushing_cache_groups
pbearne Mar 1, 2022
baa5f32
Update src/wp-includes/cache.php
pbearne Mar 1, 2022
9305962
Update src/wp-includes/class-wp-object-cache.php
pbearne Mar 1, 2022
63730d5
Update src/wp-includes/class-wp-object-cache.php
pbearne Mar 1, 2022
a9a9e4d
Added fallback to cache-compat.php
pbearne Mar 1, 2022
ea41652
Merge remote-tracking branch 'origin/INITS-115-Support_flushing_cache…
pbearne Mar 1, 2022
095ab96
Added wp_cache_flush_group to get_pluggable_function_signatures test
pbearne Mar 1, 2022
8dcc405
Added array return if array is passed
pbearne Mar 1, 2022
a7bc773
removed not needed test wp_cache_get_linked_meta
pbearne Mar 2, 2022
670e754
removed whitespace changes
pbearne Mar 2, 2022
fb650ec
Update src/wp-includes/cache.php
pbearne Mar 8, 2022
2bcc284
Update src/wp-includes/cache.php
pbearne Mar 8, 2022
c65c89c
Update src/wp-includes/cache-compat.php
pbearne Mar 8, 2022
c03b962
Merge branch 'WordPress:trunk' into INITS-115-Support_flushing_cache_…
pbearne Mar 10, 2022
fcf0c8f
Update tests/phpunit/tests/cache.php
pbearne Mar 10, 2022
a65bc31
Update tests/phpunit/tests/cache.php
pbearne Mar 14, 2022
26fcbd8
Update tests/phpunit/tests/cache.php
pbearne Mar 15, 2022
7d715b7
Update src/wp-includes/cache-compat.php
pbearne Mar 21, 2022
3f26680
moved helper class to includes
pbearne Mar 21, 2022
b8c19b0
Merge branch 'WordPress:trunk' into INITS-115-Support_flushing_cache_…
pbearne Mar 24, 2022
e890990
removed method exists check
pbearne Mar 24, 2022
a0f699d
removed method exists check
pbearne Mar 24, 2022
47a5b5c
Merge branch 'WordPress:trunk' into INITS-115-Support_flushing_cache_…
pbearne Apr 5, 2022
d3ceb58
whitespaces
pbearne Apr 5, 2022
593dc79
added flush_group to memcache test code
pbearne Apr 5, 2022
4ecb1de
added flush_group to memcache test code
pbearne Apr 5, 2022
a26df6d
added check for method before calling
pbearne Apr 5, 2022
adac259
added check for method before calling
pbearne Apr 6, 2022
1829e23
Update src/wp-includes/cache.php
pbearne Apr 6, 2022
e76618b
Update src/wp-includes/cache.php
pbearne Apr 18, 2022
d5e7ed8
Update src/wp-includes/cache-compat.php
pbearne Apr 18, 2022
5e4433d
Update src/wp-includes/cache-compat.php
pbearne Apr 18, 2022
63228eb
Update src/wp-includes/cache.php
pbearne Apr 18, 2022
7f69e33
Update src/wp-includes/class-wp-object-cache.php
pbearne Apr 18, 2022
73fbd0b
added constant WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH
pbearne Apr 19, 2022
156af11
Update src/wp-includes/cache.php
pbearne Apr 19, 2022
85d09da
Update src/wp-includes/cache-compat.php
pbearne Apr 19, 2022
f3f143a
Added WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH to the memcache test cache…
pbearne Apr 19, 2022
3020e38
fixed whitespace
pbearne Apr 19, 2022
cd279c0
Update src/wp-includes/cache-compat.php
pbearne Apr 19, 2022
91a50b8
Update tests/phpunit/tests/cache.php
pbearne Apr 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/wp-includes/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,110 @@ function wp_cache_flush() {

return $wp_object_cache->flush();
}
/**
* Removes all cache items in a group.
*
* @since 6.0.0
*
* @see WP_Object_Cache::flush_group()
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
*
* @param string|array $group name(s) of group to remove from cache.
*
* @return bool True on success, false on failure group not found.
*/
function wp_cache_flush_group( $group, $clear_linked = true ) {

global $wp_object_cache;

if ( ! wp_cache_supports_flushing_groups() ) {

return false;
}

// if group is an array loop and call each key in the array
if ( is_array( $group ) ) {

array_map( 'wp_cache_flush_group', array_values( $group ) );

return true;
}

// these are linked cache groups, so we have to flush them both if one is called
$cache_pairs = wp_cache_get_linked_meta();
if ( $clear_linked && isset( $cache_pairs[ $group ] ) ) {
foreach ( $cache_pairs[ $group ] as $cache_pair ) {
wp_cache_flush_group( $cache_pair, false );
}
}

/**
* Filters to allow caching plugins to set this function to be called to flush groups.
* See: filter "wp_cache_supports_flushing_groups" to declare group flushing is supported.
*
* @since 6.0.0
*
* @param string return $flush_group_function name of function to call from the cashing class.
*/
$flush_group_function = apply_filters( 'wp_cache_flush_group_function', 'flush_group' );
return $wp_object_cache->$flush_group_function( $group );
}

/**
* checks for group flushing capabilities
*
* @since 6.0.0
*
* @return bool True if group flushing is supported.
*/
function wp_cache_supports_flushing_groups() {
global $wp_object_cache;
/**
* Filters to allow caching plugins to set this function to be called to flush groups.
* See: filter "wp_cache_supports_flushing_groups" to declare group flushing is supported.
*
* @since 6.0.0
*
* @param string return $flush_group_function name of function to call from the cashing class.
*/
$flush_group_function = apply_filters( 'wp_cache_flush_group_function', 'flush_group' );
if ( method_exists( $wp_object_cache, $flush_group_function ) ) {
return true;
}

/**
* Filters to allow caching plugins to declare if they support flushing groups.
* See: filter "wp_cache_flush_group_function" to change the function used.
*
* @since 6.0.0
*
* @param boolean return true if your caching tool support group flushing.
*/
return apply_filters( 'wp_cache_supports_flushing_groups', false );
}

/**
* stores the mapping of linked cache groups
*
* @since 6.0.0
*
* @return array keyed by group, value is array of linked groups
*/
function wp_cache_get_linked_meta() {

/**
* some the cache groups are linked, so we need to flush them both if one is called
* this is the mapping for these groups
*
* @since 6.0.0
*
* @param array array of linked cache groups that need to delete as a set.
*/
return apply_filters( 'wp_cache_get_linked_meta',array(
'users' => array( 'user_meta' ),
'user_meta' => array( 'users' ),
) );
}

/**
* Retrieves the cache contents from the cache by key and group.
Expand Down
13 changes: 13 additions & 0 deletions src/wp-includes/class-wp-object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ public function flush() {
return true;
}

/**
* Clears the object cache of all data for the given group.
*
* @since 6.0.0
*
* @return true Always returns true.
*/
public function flush_group( $group ) {

unset( $this->cache[ $group ] );

return true;
}
/**
* Retrieves the cache contents, if it exists.
*
Expand Down
207 changes: 207 additions & 0 deletions tests/phpunit/tests/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,211 @@ public function test_get_multiple() {

$this->assertSame( $expected, $found );
}

/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_flush_group
*
* @covers ::wp_cache_flush_group
*/
public function test_wp_cache_flush_group() {
$key = 'my-key';
$val = 'my-val';

wp_cache_set( $key, $val, 'group-test' );
wp_cache_set( $key, $val, 'group-kept' );
$this->assertSame( $val, wp_cache_get( $key, 'group-test' ), 'test_wp_cache_flush_group: group-test should contain my-val' );

wp_cache_flush_group( 'group-test' );
$this->assertFalse( wp_cache_get( $key, 'group-test' ), 'test_wp_cache_flush_group: group-test should return false' );
$this->assertSame( $val, wp_cache_get( $key, 'group-kept' ), 'test_wp_cache_flush_group: group-kept should still contain my-val' );
}

/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_flush_group with an array of groups
*
* @covers ::wp_cache_flush_groups
*/
public function test_wp_cache_flush_groups() {
$key = 'my-key';
$val = 'my-val';

wp_cache_set( $key, $val, 'group-test' );
wp_cache_set( $key, $val, 'group-test2' );
wp_cache_set( $key, $val, 'group-kept' );
$this->assertSame( $val, wp_cache_get( $key, 'group-test' ), 'test_wp_cache_flush_groups: group-test should contain my-val' );
$this->assertSame( $val, wp_cache_get( $key, 'group-test2' ), 'test_wp_cache_flush_groups: group-test2 should contain my-val' );

wp_cache_flush_group( array( 'group-test', 'group-test2' ) );
$this->assertFalse( wp_cache_get( $key, 'group-test' ), 'test_wp_cache_flush_groups: group-test should return false' );
$this->assertFalse( wp_cache_get( $key, 'group-test2' ), 'test_wp_cache_flush_groups: group-test2 should return false' );
$this->assertSame( $val, wp_cache_get( $key, 'group-kept' ), 'test_wp_cache_flush_groups: group-kept should still contain my-val' );
}

/**
* @ticket 4476
* @ticket 9773
*
* tests that is users cache is cleared user_meta is also cleared and vice versa.
*
* @covers ::wp_cache_flush_group
*/
public function test_wp_cache_flush_group_users() {
$key_users = 'my-user-key';
$val_users = 'my-user-val';
wp_cache_set( $key_users, $val_users, 'users' );

$key_meta = 'my-user_meta-key';
$val_meta = 'my-user_meta-val';
wp_cache_set( $key_meta, $val_meta, 'user_meta' );

wp_cache_flush_group( 'users' );
$this->assertFalse( wp_cache_get( $key_users, 'users' ), 'test_wp_cache_flush_group_users users: users should return false' );
$this->assertFalse( wp_cache_get( $key_meta, 'user_meta' ), 'test_wp_cache_flush_group_users users: user_meta should return false' );

wp_cache_set( $key_users, $val_users, 'users' );
wp_cache_set( $key_meta, $val_meta, 'user_meta' );

wp_cache_flush_group( 'user_meta' );
$this->assertFalse( wp_cache_get( $key_users, 'users' ), 'test_wp_cache_flush_group_users user_meta: users should return false' );
$this->assertFalse( wp_cache_get( $key_meta, 'user_meta' ), 'test_wp_cache_flush_group_users user_meta: user_meta should return false' );
}

/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_supports_flushing_groups
*
* @covers ::wp_cache_supports_flushing_groups
*/
public function test_wp_cache_supports_flushing_groups() {
$this->assertTrue( wp_cache_supports_flushing_groups(), 'test_wp_cache_supports_flushing_groups: should return true' );
}
/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_supports_flushing_groups
*
* @covers ::wp_cache_supports_flushing_groups
*/
public function test_wp_cache_supports_flushing_groups_no_support() {
global $wp_object_cache;
$temp = $wp_object_cache;
$wp_object_cache = new stdClass();
$this->assertFalse( wp_cache_supports_flushing_groups(), 'test_wp_cache_supports_flushing_groups: should return false' );

$wp_object_cache = $temp;
}

/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_supports_flushing_groups
*
* @covers ::wp_cache_supports_flushing_groups
*/
public function test_wp_cache_supports_flushing_groups_filtered_function() {
global $wp_object_cache;
$temp = $wp_object_cache;
$wp_object_cache = new WP_Object_Cache_dummy();

add_filter(
'wp_cache_flush_group_function',
static function() {
return 'dummy_function';
}
);

$this->assertTrue( wp_cache_supports_flushing_groups(), 'test_wp_cache_supports_flushing_groups: should return false' );

$wp_object_cache = $temp;
}
/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_supports_flushing_groups
*
* @covers ::wp_cache_supports_flushing_groups
*/
public function test_wp_cache_supports_flushing_groups_filtered() {
global $wp_object_cache;
$temp = $wp_object_cache;
$wp_object_cache = new WP_Object_Cache_dummy();

add_filter( 'wp_cache_supports_flushing_groups', '__return_true' );

$this->assertTrue( wp_cache_supports_flushing_groups(), 'test_wp_cache_supports_flushing_groups with filter: should return true' );

$wp_object_cache = $temp;
}

/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_supports_flushing_groups
*
* @covers ::wp_cache_supports_flushing_groups
*/
public function test_wp_cache_flushing_groups_filtered() {
global $wp_object_cache;
$temp = $wp_object_cache;
$wp_object_cache = new WP_Object_Cache_dummy();

add_filter(
'wp_cache_flush_group_function',
static function() {
return 'dummy_function';
}
);

$key = 'my-key';
$val = 'my-val';

wp_cache_set( $key, $val, 'group-test' );
wp_cache_set( $key, $val, 'group-kept' );
$this->assertTrue( wp_cache_flush_group( 'dummy_group' ) );

$wp_object_cache = $temp;
}



/**
* @ticket 4476
* @ticket 9773
*
* test wp_cache_get_linked_meta returns an array
*
* @covers ::wp_cache_get_linked_meta
*/
public function test_wp_cache_get_linked_meta() {

$this->assertIsArray( wp_cache_get_linked_meta() );
}


}
class WP_Object_Cache_dummy {
public function dummy_function() {
return true;
}
public function flush() {
return;
}
public function add_global_groups() {
return;
}
public function set() {
return;
}
}