Make WordPress Core

Changeset 53763


Ignore:
Timestamp:
07/22/2022 08:50:31 PM (4 years ago)
Author:
spacedmonkey
Message:

Cache API: Add wp_cache_flush_group function.

Add a new plugable function called wp_cache_flush_group, that will allow developers to clear whole cache groups with a single call. Developers can detect if their current implementation of an object cache supports flushing by group, by calling wp_cache_supports_group_flush which returns true if it is supported. If the developers of the object cache drop-in has not implemented wp_cache_flush_group and wp_cache_supports_group_flush, these functions are polyfilled and wp_cache_supports_group_flush defaults to false.

Props Spacedmonkey, filosofo, ryan, sc0ttkclark, SergeyBiryukov, scribu, Ste_95, dd32, dhilditch, dougal, lucasbustamante, dg12345, tillkruess, peterwilsoncc, flixos90, pbearne.
Fixes #4476.

Location:
trunk
Files:
6 edited

Legend:

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

    r53206 r53763  
    142142        }
    143143endif;
     144
     145if ( ! function_exists( 'wp_cache_flush_group' ) ) :
     146        /**
     147         * Removes all cache items in a group, if the object cache implementation supports it.
     148         * Before calling this method, always check for group flushing support using the
     149         * `wp_cache_supports_group_flush()` method.
     150         *
     151         * @since 6.1.0
     152         *
     153         * @see WP_Object_Cache::flush_group()
     154         * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     155         *
     156         * @param string $group Name of group to remove from cache.
     157         * @return bool True if group was flushed, false otherwise.
     158         */
     159        function wp_cache_flush_group( $group ) {
     160                global $wp_object_cache;
     161
     162                if ( ! wp_cache_supports_group_flush() ) {
     163                        _doing_it_wrong(
     164                                __FUNCTION__,
     165                                __( 'Your object cache implementation does not support flushing individual groups.' ),
     166                                '6.1.0'
     167                        );
     168
     169                        return false;
     170                }
     171
     172                return $wp_object_cache->flush_group( $group );
     173        }
     174endif;
     175
     176if ( ! function_exists( 'wp_cache_supports_group_flush' ) ) :
     177        /**
     178         * Whether the object cache implementation supports flushing individual cache groups.
     179         *
     180         * @since 6.1.0
     181         *
     182         * @see WP_Object_Cache::flush_group()
     183         *
     184         * @return bool True if group flushing is supported, false otherwise.
     185         */
     186        function wp_cache_supports_group_flush() {
     187                return false;
     188        }
     189endif;
  • trunk/src/wp-includes/cache.php

    r53206 r53763  
    2121function wp_cache_init() {
    2222        $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
     23}
     24
     25/**
     26 * Whether the object cache implementation supports flushing individual cache groups.
     27 *
     28 * @since 6.1.0
     29 *
     30 * @see WP_Object_Cache::flush_group()
     31 *
     32 * @return bool True if group flushing is supported, false otherwise.
     33 */
     34function wp_cache_supports_group_flush() {
     35        return true;
    2336}
    2437
    … …  
    283296
    284297/**
     298 * Removes all cache items in a group, if the object cache implementation supports it.
     299 * Before calling this method, always check for group flushing support using the
     300 * `wp_cache_supports_group_flush()` method.
     301 *
     302 * @since 6.1.0
     303 *
     304 * @see WP_Object_Cache::flush_group()
     305 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     306 *
     307 * @param string $group Name of group to remove from cache.
     308 * @return bool True if group was flushed, false otherwise.
     309 */
     310function wp_cache_flush_group( $group ) {
     311        global $wp_object_cache;
     312
     313        return $wp_object_cache->flush_group( $group );
     314}
     315
     316/**
    285317 * Closes the cache.
    286318 *
  • trunk/src/wp-includes/class-wp-object-cache.php

    r52708 r53763  
    292292
    293293        /**
     294         * Removes all cache items in a group.
     295         *
     296         * @since 6.1.0
     297         *
     298         * @param string $group Name of group to remove from cache.
     299         * @return true Always returns true.
     300         */
     301        public function flush_group( $group ) {
     302                unset( $this->cache[ $group ] );
     303
     304                return true;
     305        }
     306
     307        /**
    294308         * Retrieves the cache contents, if it exists.
    295309         *
  • trunk/tests/phpunit/includes/object-cache.php

    r52976 r53763  
    744744        global $wp_object_cache;
    745745        $wp_object_cache = new WP_Object_Cache();
     746}
     747
     748/**
     749 * Whether the object cache implementation supports flushing individual cache groups.
     750 *
     751 * @since 6.1.0
     752 *
     753 * @return bool True if group flushing is supported, false otherwise.
     754 */
     755function wp_cache_supports_group_flush() {
     756        return false;
    746757}
    747758
  • trunk/tests/phpunit/tests/cache.php

    r52706 r53763  
    416416                $this->assertSame( $expected, $found );
    417417        }
     418
     419        /**
     420         * @ticket 4476
     421         * @ticket 9773
     422         *
     423         * test wp_cache_flush_group
     424         *
     425         * @covers ::wp_cache_flush_group
     426         */
     427        public function test_wp_cache_flush_group() {
     428                $key = 'my-key';
     429                $val = 'my-val';
     430
     431                wp_cache_set( $key, $val, 'group-test' );
     432                wp_cache_set( $key, $val, 'group-kept' );
     433
     434                $this->assertSame( $val, wp_cache_get( $key, 'group-test' ), 'test_wp_cache_flush_group: group-test should contain my-val' );
     435
     436                if ( wp_using_ext_object_cache() ) {
     437                        $this->setExpectedIncorrectUsage( 'wp_cache_flush_group' );
     438                }
     439
     440                $results = wp_cache_flush_group( 'group-test' );
     441
     442                if ( wp_using_ext_object_cache() ) {
     443                        $this->assertFalse( $results );
     444                } else {
     445                        $this->assertTrue( $results );
     446                        $this->assertFalse( wp_cache_get( $key, 'group-test' ), 'test_wp_cache_flush_group: group-test should return false' );
     447                        $this->assertSame( $val, wp_cache_get( $key, 'group-kept' ), 'test_wp_cache_flush_group: group-kept should still contain my-val' );
     448                }
     449        }
    418450}
  • trunk/tests/phpunit/tests/pluggable.php

    r53478 r53763  
    269269                                        // wp-includes/cache.php:
    270270                                        'wp_cache_init'                      => array(),
     271                                        'wp_cache_supports_group_flush'      => array(),
    271272                                        'wp_cache_add'                       => array(
    272273                                                'key',
    … …  
    328329                                        'wp_cache_flush'                     => array(),
    329330                                        'wp_cache_flush_runtime'             => array(),
     331                                        'wp_cache_flush_group'               => array( 'group' ),
    330332                                        'wp_cache_close'                     => array(),
    331333                                        'wp_cache_add_global_groups'         => array( 'groups' ),
Note: See TracChangeset for help on using the changeset viewer.