Make WordPress Core

Changeset 53767


Ignore:
Timestamp:
07/23/2022 02:56:51 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Cache API: Make the placement of wp_cache_flush_group() more consistent.

Includes:

  • Placing WP_Object_Cache::flush_group() next to ::flush().
  • Placing wp_cache_supports_group_flush() next to wp_cache_flush_group().
  • Placing the wp_cache_flush_group() unit test next to the ::flush() method test.
  • Removing test name from assertion messages, as it is already mentioned directly above in case of failure.
  • Adjusting function descriptions per the documentation standards.

Follow-up to [52706], [53763].

See #55647, #4476.

Location:
trunk
Files:
6 edited

Legend:

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

    r53763 r53767  
    146146    /**
    147147     * 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.
     148     *
     149     * Before calling this function, always check for group flushing support using the
     150     * `wp_cache_supports_group_flush()` function.
    150151     *
    151152     * @since 6.1.0
     
    176177if ( ! function_exists( 'wp_cache_supports_group_flush' ) ) :
    177178    /**
    178      * Whether the object cache implementation supports flushing individual cache groups.
     179     * Determines whether the object cache implementation supports flushing individual cache groups.
    179180     *
    180181     * @since 6.1.0
  • trunk/src/wp-includes/cache.php

    r53763 r53767  
    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  */
    34 function wp_cache_supports_group_flush() {
    35     return true;
    3623}
    3724
     
    297284/**
    298285 * 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.
     286 *
     287 * Before calling this function, always check for group flushing support using the
     288 * `wp_cache_supports_group_flush()` function.
    301289 *
    302290 * @since 6.1.0
     
    312300
    313301    return $wp_object_cache->flush_group( $group );
     302}
     303
     304/**
     305 * Determines whether the object cache implementation supports flushing individual cache groups.
     306 *
     307 * @since 6.1.0
     308 *
     309 * @see WP_Object_Cache::flush_group()
     310 *
     311 * @return bool True if group flushing is supported, false otherwise.
     312 */
     313function wp_cache_supports_group_flush() {
     314    return true;
    314315}
    315316
  • trunk/src/wp-includes/class-wp-object-cache.php

    r53763 r53767  
    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     /**
    308294     * Retrieves the cache contents, if it exists.
    309295     *
     
    511497
    512498    /**
     499     * Removes all cache items in a group.
     500     *
     501     * @since 6.1.0
     502     *
     503     * @param string $group Name of group to remove from cache.
     504     * @return true Always returns true.
     505     */
     506    public function flush_group( $group ) {
     507        unset( $this->cache[ $group ] );
     508
     509        return true;
     510    }
     511
     512    /**
    513513     * Sets the list of global cache groups.
    514514     *
  • trunk/tests/phpunit/includes/object-cache.php

    r53763 r53767  
    283283    global $wp_object_cache;
    284284    return $wp_object_cache->flush( $delay );
     285}
     286
     287/**
     288 * Whether the object cache implementation supports flushing individual cache groups.
     289 *
     290 * @since 6.1.0
     291 *
     292 * @return bool True if group flushing is supported, false otherwise.
     293 */
     294function wp_cache_supports_group_flush() {
     295    return false;
    285296}
    286297
     
    744755    global $wp_object_cache;
    745756    $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  */
    755 function wp_cache_supports_group_flush() {
    756     return false;
    757757}
    758758
  • trunk/tests/phpunit/tests/cache.php

    r53763 r53767  
    128128        // If there is no value get returns false.
    129129        $this->assertFalse( $this->cache->get( $key ) );
     130    }
     131
     132    /**
     133     * @ticket 4476
     134     * @ticket 9773
     135     *
     136     * @covers ::wp_cache_flush_group
     137     */
     138    public function test_wp_cache_flush_group() {
     139        $key = 'my-key';
     140        $val = 'my-val';
     141
     142        wp_cache_set( $key, $val, 'group-test' );
     143        wp_cache_set( $key, $val, 'group-kept' );
     144
     145        $this->assertSame( $val, wp_cache_get( $key, 'group-test' ), 'group-test should contain my-val' );
     146
     147        if ( wp_using_ext_object_cache() ) {
     148            $this->setExpectedIncorrectUsage( 'wp_cache_flush_group' );
     149        }
     150
     151        $results = wp_cache_flush_group( 'group-test' );
     152
     153        if ( wp_using_ext_object_cache() ) {
     154            $this->assertFalse( $results );
     155        } else {
     156            $this->assertTrue( $results );
     157            $this->assertFalse( wp_cache_get( $key, 'group-test' ), 'group-test should return false' );
     158            $this->assertSame( $val, wp_cache_get( $key, 'group-kept' ), 'group-kept should still contain my-val' );
     159        }
    130160    }
    131161
     
    416446        $this->assertSame( $expected, $found );
    417447    }
    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     }
    450448}
  • trunk/tests/phpunit/tests/pluggable.php

    r53763 r53767  
    269269                    // wp-includes/cache.php:
    270270                    'wp_cache_init'                      => array(),
    271                     'wp_cache_supports_group_flush'      => array(),
    272271                    'wp_cache_add'                       => array(
    273272                        'key',
     
    330329                    'wp_cache_flush_runtime'             => array(),
    331330                    'wp_cache_flush_group'               => array( 'group' ),
     331                    'wp_cache_supports_group_flush'      => array(),
    332332                    'wp_cache_close'                     => array(),
    333333                    'wp_cache_add_global_groups'         => array( 'groups' ),
Note: See TracChangeset for help on using the changeset viewer.