Build/Test Tools: Call wp_cache_flush_runtime in WP_UnitTestCase.

In `WP_UnitTestCase::flush_cache` method, the properties of the global `$wp_object_cache` object were manaully being reset to flush the cache. The function `wp_cache_flush_runtime` was added in [52772] and is designed to reset any class properties to default values. Using `wp_cache_flush_runtime` improve compatibility with third party object caches, as it allows developers to define their own `wp_cache_flush_runtime` function. 

Props rmccue, johnbillion, wonderboymusic, boonebgorges, voldemortensen, dd32, DrewAPicture, tillkruess, spacedmonkey.
Fixes #31463.

git-svn-id: https://develop.svn.wordpress.org/trunk@55741 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris 2023-05-10 09:09:06 +00:00
parent 860d506a8d
commit c204a6aa27
2 changed files with 24 additions and 5 deletions

View File

@ -387,12 +387,9 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
public static function flush_cache() {
global $wp_object_cache;
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();
wp_cache_flush_runtime();
if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
if ( is_object( $wp_object_cache ) && method_exists( $wp_object_cache, '__remoteset' ) ) {
$wp_object_cache->__remoteset();
}

View File

@ -325,12 +325,23 @@ function wp_cache_flush( $delay = 0 ) {
function wp_cache_supports( $feature ) {
switch ( $feature ) {
case 'get_multiple':
case 'flush_runtime':
return true;
default:
return false;
}
}
/**
* Removes all cache items from the in-memory runtime cache.
*
* @return bool True on success, false on failure.
*/
function wp_cache_flush_runtime() {
global $wp_object_cache;
return $wp_object_cache->flush_runtime();
}
/**
* Retrieves object from cache.
*
@ -1412,6 +1423,17 @@ class WP_Object_Cache {
return $result;
}
/**
* Clears the in-memory cache of all data leaving the external cache untouched.
*
* @return bool Always returns true.
*/
public function flush_runtime() {
$this->cache = array();
return true;
}
/**
* Retrieves object from cache.
*