diff --git a/wp-includes/cache-compat.php b/wp-includes/cache-compat.php index 1088f1c74b..c225d91515 100644 --- a/wp-includes/cache-compat.php +++ b/wp-includes/cache-compat.php @@ -141,3 +141,49 @@ if ( ! function_exists( '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_cache_supports_group_flush()` method. + * + * @since 6.1.0 + * + * @see WP_Object_Cache::flush_group() + * @global WP_Object_Cache $wp_object_cache Object cache global instance. + * + * @param string $group Name of group to remove from cache. + * @return bool True if group was flushed, false otherwise. + */ + function wp_cache_flush_group( $group ) { + global $wp_object_cache; + + if ( ! wp_cache_supports_group_flush() ) { + _doing_it_wrong( + __FUNCTION__, + __( 'Your object cache implementation does not support flushing individual groups.' ), + '6.1.0' + ); + + return false; + } + + return $wp_object_cache->flush_group( $group ); + } +endif; + +if ( ! function_exists( 'wp_cache_supports_group_flush' ) ) : + /** + * Whether the object cache implementation supports flushing individual cache groups. + * + * @since 6.1.0 + * + * @see WP_Object_Cache::flush_group() + * + * @return bool True if group flushing is supported, false otherwise. + */ + function wp_cache_supports_group_flush() { + return false; + } +endif; diff --git a/wp-includes/cache.php b/wp-includes/cache.php index 1999dd00ff..9d68b1bb04 100644 --- a/wp-includes/cache.php +++ b/wp-includes/cache.php @@ -22,6 +22,19 @@ function wp_cache_init() { $GLOBALS['wp_object_cache'] = new WP_Object_Cache(); } +/** + * Whether the object cache implementation supports flushing individual cache groups. + * + * @since 6.1.0 + * + * @see WP_Object_Cache::flush_group() + * + * @return bool True if group flushing is supported, false otherwise. + */ +function wp_cache_supports_group_flush() { + return true; +} + /** * Adds data to the cache, if the cache key doesn't already exist. * @@ -281,6 +294,25 @@ function wp_cache_flush_runtime() { return wp_cache_flush(); } +/** + * 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_cache_supports_group_flush()` method. + * + * @since 6.1.0 + * + * @see WP_Object_Cache::flush_group() + * @global WP_Object_Cache $wp_object_cache Object cache global instance. + * + * @param string $group Name of group to remove from cache. + * @return bool True if group was flushed, false otherwise. + */ +function wp_cache_flush_group( $group ) { + global $wp_object_cache; + + return $wp_object_cache->flush_group( $group ); +} + /** * Closes the cache. * diff --git a/wp-includes/class-wp-object-cache.php b/wp-includes/class-wp-object-cache.php index 89fd6ff011..e145bebcf3 100644 --- a/wp-includes/class-wp-object-cache.php +++ b/wp-includes/class-wp-object-cache.php @@ -290,6 +290,20 @@ class WP_Object_Cache { return $values; } + /** + * Removes all cache items in a group. + * + * @since 6.1.0 + * + * @param string $group Name of group to remove from cache. + * @return true Always returns true. + */ + public function flush_group( $group ) { + unset( $this->cache[ $group ] ); + + return true; + } + /** * Retrieves the cache contents, if it exists. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 6e851aba55..64d5314edd 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-53762'; +$wp_version = '6.1-alpha-53763'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.