Make WordPress Core


Ignore:
Timestamp:
07/22/2022 08:50:31 PM (2 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.