Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Cache by Group #2368

Closed
Closed
Show file tree
Hide file tree
Changes from all 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/wp-includes/cache-compat.php
Expand Up @@ -141,3 +141,30 @@ function wp_cache_flush_runtime() {
return wp_using_ext_object_cache() ? false : wp_cache_flush();
}
endif;

if ( ! function_exists( 'wp_cache_flush_group' ) ) :
/**
* Removes all cache items in a group, if the object cache implementation supports it.
* Before calling this method, always check for group flushing support using the
* `WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH` constant.
*
* @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|array|WP_Error Bool or array of bool if array passed, WP_Error if not supported.
*/
function wp_cache_flush_group( $group ) {
if ( ! defined( 'WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH' ) || ! WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH ) {
$error = new WP_Error( 'unsupported', __( 'Your object cache implementation does not support flushing individual groups.' ) );

_doing_it_wrong( __FUNCTION__, $error->get_error_message(), '6.0.0' );

return $error;
}

return $wp_object_cache->flush_group( $group );
}
endif;
29 changes: 29 additions & 0 deletions src/wp-includes/cache.php
Expand Up @@ -11,6 +11,11 @@
/** WP_Object_Cache class */
require_once ABSPATH . WPINC . '/class-wp-object-cache.php';

/**
* Indicate that this object cache implementation supports group flushing.
*/
define( 'WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH', true );

/**
* Sets up Object Cache Global and assigns it.
*
Expand Down Expand Up @@ -132,6 +137,30 @@ function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
return $wp_object_cache->set_multiple( $data, $group, $expire );
}

/**
* Removes all cache items in a group, if the object cache implementation supports it.
* Before calling this method, always check for group flushing support using the
* `WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH` constant.
*
* @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|array|WP_Error Bool or array of bool if array passed, WP_Error if not supported.
*/
function wp_cache_flush_group( $group ) {
pbearne marked this conversation as resolved.
Show resolved Hide resolved
global $wp_object_cache;

// if group is an array loop and call each key in the array
if ( is_array( $group ) ) {
return array_map( 'wp_cache_flush_group', array_values( $group ) );
}

return $wp_object_cache->flush_group( $group );
pbearne marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Retrieves the cache contents from the cache by key and group.
*
Expand Down
12 changes: 12 additions & 0 deletions src/wp-includes/class-wp-object-cache.php
Expand Up @@ -290,6 +290,18 @@ public function set_multiple( array $data, $group = '', $expire = 0 ) {
return $values;
}

/**
* Removes all cache items in a 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
4 changes: 4 additions & 0 deletions tests/phpunit/includes/object-cache.php
@@ -1,4 +1,8 @@
<?php
/**
* Indicate that this object cache implementation does not support group flushing.
*/
define( 'WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH', false );
/**
* Adds a value to cache.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/phpunit/tests/cache.php
Expand Up @@ -415,4 +415,58 @@ public function test_wp_cache_delete_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';
pbearne marked this conversation as resolved.
Show resolved Hide resolved
$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' );

$results = wp_cache_flush_group( 'group-test' );
$this->assertTrue( $results );
$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' );
Comment on lines +436 to +438
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertTrue( $results );
$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' );
if (WP_OBJECT_CACHE_SUPPORTS_GROUP_FLUSH) {
$this->assertTrue( $results );
$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' );
} else {
$this->assertWPError( $results );
$this->assertSame( 'unsupported', $results->get_error_code() );
}

}

/**
* @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';
pbearne marked this conversation as resolved.
Show resolved Hide resolved
$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' );

$results = wp_cache_flush_group( array( 'group-test', 'group-test2' ) );

if ( wp_using_ext_object_cache() ) {
$this->assertWPError( $results );
$this->assertSame( 'unsupported', $results->get_error_code() );
} else {
$this->assertIsArray( $results );
$this->assertCount( 2, $results );
$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' );
}
}
}
1 change: 1 addition & 0 deletions tests/phpunit/tests/pluggable.php
Expand Up @@ -332,6 +332,7 @@ public function get_pluggable_function_signatures() {
'wp_cache_add_non_persistent_groups' => array( 'groups' ),
'wp_cache_switch_to_blog' => array( 'blog_id' ),
'wp_cache_reset' => array(),
'wp_cache_flush_group' => array( 'group' ),
)
);
}
Expand Down