Make WordPress Core

Changeset 54448


Ignore:
Timestamp:
10/10/2022 06:20:28 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Cache API: Introduce wp_cache_supports() function.

WordPress has recently introduced a variety of caching API improvements:

  • wp_cache_add_multiple()
  • wp_cache_set_multiple()
  • wp_cache_get_multiple()
  • wp_cache_delete_multiple()
  • wp_cache_flush_runtime()
  • wp_cache_flush_group()

Although WordPress core provides a compatibility layer if these functions are missing from third-party object cache implementations, there should be a method of checking whether the cache backend supports a particular feature.

This commit aims to improve developer experience by allowing third-party object cache plugins to declare a wp_cache_supports() function and correctly list their supported features:

  • add_multiple
  • set_multiple
  • get_multiple
  • delete_multiple
  • flush_runtime
  • flush_group

Note: The wp_cache_supports() function replaces and supersedes the wp_cache_supports_group_flush() function added earlier.

Follow-up to [47938], [47944], [52700], [52703], [52706], [52708], [53763], [53767], [54423].

Props johnjamesjacoby, tillkruess, spacedmonkey, SergeyBiryukov.
Fixes #56605.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/cache-compat.php

    r53767 r54448  
    139139     */
    140140    function wp_cache_flush_runtime() {
    141         return wp_using_ext_object_cache() ? false : wp_cache_flush();
     141        if ( ! wp_cache_supports( 'flush_runtime' ) ) {
     142            _doing_it_wrong(
     143                __FUNCTION__,
     144                __( 'Your object cache implementation does not support flushing the in-memory runtime cache.' ),
     145                '6.1.0'
     146            );
     147
     148            return false;
     149        }
     150
     151        return wp_cache_flush();
    142152    }
    143153endif;
     
    148158     *
    149159     * Before calling this function, always check for group flushing support using the
    150      * `wp_cache_supports_group_flush()` function.
     160     * `wp_cache_supports( 'flush_group' )` function.
    151161     *
    152162     * @since 6.1.0
     
    161171        global $wp_object_cache;
    162172
    163         if ( ! wp_cache_supports_group_flush() ) {
     173        if ( ! wp_cache_supports( 'flush_group' ) ) {
    164174            _doing_it_wrong(
    165175                __FUNCTION__,
     
    175185endif;
    176186
    177 if ( ! function_exists( 'wp_cache_supports_group_flush' ) ) :
    178     /**
    179      * Determines whether the object cache implementation supports flushing individual cache groups.
     187if ( ! function_exists( 'wp_cache_supports' ) ) :
     188    /**
     189     * Determines whether the object cache implementation supports a particular feature.
    180190     *
    181191     * @since 6.1.0
    182192     *
    183      * @see WP_Object_Cache::flush_group()
    184      *
    185      * @return bool True if group flushing is supported, false otherwise.
    186      */
    187     function wp_cache_supports_group_flush() {
     193     * @param string $feature Name of the feature to check for. Possible values include:
     194     *                        'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple',
     195     *                        'flush_runtime', 'flush_group'.
     196     * @return bool True if the feature is supported, false otherwise.
     197     */
     198    function wp_cache_supports( $feature ) {
    188199        return false;
    189200    }
  • trunk/src/wp-includes/cache.php

    r53767 r54448  
    286286 *
    287287 * Before calling this function, always check for group flushing support using the
    288  * `wp_cache_supports_group_flush()` function.
     288 * `wp_cache_supports( 'flush_group' )` function.
    289289 *
    290290 * @since 6.1.0
     
    303303
    304304/**
    305  * Determines whether the object cache implementation supports flushing individual cache groups.
     305 * Determines whether the object cache implementation supports a particular feature.
    306306 *
    307307 * @since 6.1.0
    308308 *
    309  * @see WP_Object_Cache::flush_group()
    310  *
    311  * @return bool True if group flushing is supported, false otherwise.
    312  */
    313 function wp_cache_supports_group_flush() {
    314     return true;
     309 * @param string $feature Name of the feature to check for. Possible values include:
     310 *                        'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple',
     311 *                        'flush_runtime', 'flush_group'.
     312 * @return bool True if the feature is supported, false otherwise.
     313 */
     314function wp_cache_supports( $feature ) {
     315    switch ( $feature ) {
     316        case 'add_multiple':
     317        case 'set_multiple':
     318        case 'get_multiple':
     319        case 'delete_multiple':
     320        case 'flush_runtime':
     321        case 'flush_group':
     322            return true;
     323
     324        default:
     325            return false;
     326    }
    315327}
    316328
  • trunk/tests/phpunit/includes/object-cache.php

    r54423 r54448  
    314314
    315315/**
    316  * Whether the object cache implementation supports flushing individual cache groups.
     316 * Determines whether the object cache implementation supports a particular feature.
    317317 *
    318318 * @since 6.1.0
    319319 *
    320  * @return bool True if group flushing is supported, false otherwise.
    321  */
    322 function wp_cache_supports_group_flush() {
     320 * @param string $feature Name of the feature to check for. Possible values include:
     321 *                        'add_multiple', 'set_multiple', 'get_multiple', 'delete_multiple',
     322 *                        'flush_runtime', 'flush_group'.
     323 * @return bool True if the feature is supported, false otherwise.
     324 */
     325function wp_cache_supports( $feature ) {
    323326    return false;
    324327}
  • trunk/tests/phpunit/tests/pluggable.php

    r54240 r54448  
    327327                    'wp_cache_flush_runtime'             => array(),
    328328                    'wp_cache_flush_group'               => array( 'group' ),
    329                     'wp_cache_supports_group_flush'      => array(),
     329                    'wp_cache_supports'                  => array( 'feature' ),
    330330                    'wp_cache_close'                     => array(),
    331331                    'wp_cache_add_global_groups'         => array( 'groups' ),
Note: See TracChangeset for help on using the changeset viewer.