Make WordPress Core


Ignore:
Timestamp:
02/11/2022 12:50:54 PM (3 years ago)
Author:
spacedmonkey
Message:

Cache: Add wp_cache_*_multiple functions.

Add new caching functions named wp_cache_add_multiple, wp_cache_set_multiple and wp_cache_delete_multiple. All of these functions allow for an array of data to be passed, so that multiple cache objects can be created / edited / deleted in a single function call. This follows on from [47938] where the wp_cache_get_multiple function was introduced and allowed for multiple cache objects to be received in one call.

Props: spacedmonkey, tillkruess, adamsilverstein, flixos90, mitogh, pbearne.
Fixes: #54574.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/cache.php

    r52010 r52700  
    350350        $this->assertSame( $expected, $found );
    351351    }
     352
     353    /**
     354     * @ticket 54574
     355     */
     356    public function test_wp_cache_add_multiple() {
     357        $found = wp_cache_add_multiple(
     358            array(
     359                'foo1' => 'bar',
     360                'foo2' => 'bar',
     361                'foo3' => 'bar',
     362            ),
     363            'group1'
     364        );
     365
     366        $expected = array(
     367            'foo1' => true,
     368            'foo2' => true,
     369            'foo3' => true,
     370        );
     371
     372        $this->assertSame( $expected, $found );
     373    }
     374
     375    /**
     376     * @ticket 54574
     377     */
     378    public function test_wp_cache_set_multiple() {
     379        wp_cache_set( 'foo1', 'bar', 'group1' );
     380        wp_cache_set( 'foo2', 'bar', 'group1' );
     381        wp_cache_set( 'foo3', 'bar', 'group2' );
     382
     383        $found = wp_cache_set_multiple(
     384            array(
     385                'foo1' => 'bar',
     386                'foo2' => 'bar',
     387                'foo3' => 'bar',
     388            ),
     389            'group1'
     390        );
     391
     392        $expected = array(
     393            'foo1' => true,
     394            'foo2' => true,
     395            'foo3' => true,
     396        );
     397
     398        $this->assertSame( $expected, $found );
     399    }
     400
     401    /**
     402     * @ticket 54574
     403     */
     404    public function test_wp_cache_delete_multiple() {
     405        wp_cache_set( 'foo1', 'bar', 'group1' );
     406        wp_cache_set( 'foo2', 'bar', 'group1' );
     407        wp_cache_set( 'foo3', 'bar', 'group2' );
     408
     409        $found = wp_cache_delete_multiple(
     410            array( 'foo1', 'foo2', 'foo3' ),
     411            'group1'
     412        );
     413
     414        $expected = array(
     415            'foo1' => true,
     416            'foo2' => true,
     417            'foo3' => false,
     418        );
     419
     420        $this->assertSame( $expected, $found );
     421    }
    352422}
Note: See TracChangeset for help on using the changeset viewer.