Make WordPress Core

Changeset 52700


Ignore:
Timestamp:
02/11/2022 12:50:54 PM (4 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.

Location:
trunk
Files:
5 edited

Legend:

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

    r47944 r52700  
    3636        }
    3737endif;
     38
     39if ( ! function_exists( 'wp_cache_delete_multiple' ) ) :
     40        /**
     41         * Delete multiple values from the cache in one call.
     42         *
     43         * Compat function to mimic wp_cache_delete_multiple().
     44         *
     45         * @ignore
     46         * @since 6.0.0
     47         *
     48         * @see wp_cache_delete_multiple()
     49         *
     50         * @param array  $keys  Array of keys under which the cache to deleted.
     51         * @param string $group Optional. Where the cache contents are grouped. Default empty.
     52         * @return array Array of return values.
     53         */
     54        function wp_cache_delete_multiple( array $keys, $group = '' ) {
     55                $values = array();
     56
     57                foreach ( $keys as $key ) {
     58                        $values[ $key ] = wp_cache_delete( $key, $group );
     59                }
     60
     61                return $values;
     62        }
     63endif;
     64
     65
     66if ( ! function_exists( 'wp_cache_add_multiple' ) ) :
     67        /**
     68         * Add multiple values to the cache in one call, if the cache keys doesn't already exist.
     69         *
     70         * Compat function to mimic wp_cache_add_multiple().
     71         *
     72         * @ignore
     73         * @since 6.0.0
     74         *
     75         * @see wp_cache_add_multiple()
     76         *
     77         * @param array  $data  Array of key and value to be added.
     78         * @param string $group Optional. Where the cache contents are grouped. Default empty.
     79         * @param int    $expire Optional. When to expire the cache contents, in seconds. Default 0 (no expiration).
     80         * @return array Array of return values.
     81         */
     82        function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
     83                $values = array();
     84
     85                foreach ( $data as $key => $value ) {
     86                        $values[ $key ] = wp_cache_add( $key, $value, $group, $expire );
     87                }
     88
     89                return $values;
     90        }
     91endif;
     92
     93if ( ! function_exists( 'wp_cache_set_multiple' ) ) :
     94        /**
     95         * Set multiple values to the cache in one call.
     96         *
     97         * Differs from wp_cache_add_multiple() in that it will always write data.
     98         *
     99         * Compat function to mimic wp_cache_set_multiple().
     100         *
     101         * @ignore
     102         * @since 6.0.0
     103         *
     104         * @see wp_cache_set_multiple()
     105         *
     106         * @param array  $data  Array of key and value to be set.
     107         * @param string $group Optional. Where the cache contents are grouped. Default empty.
     108         * @param int    $expire Optional. When to expire the cache contents, in seconds. Default 0 (no expiration).
     109         * @return array Array of return values.
     110         */
     111        function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
     112                $values = array();
     113
     114                foreach ( $data as $key => $value ) {
     115                        $values[ $key ] = wp_cache_set( $key, $value, $group, $expire );
     116                }
     117
     118                return $values;
     119        }
     120endif;
  • trunk/src/wp-includes/cache.php

    r49693 r52700  
    146146}
    147147
     148
     149/**
     150 * Delete multiple values from the cache in one call.
     151 *
     152 * @since 6.0.0
     153 *
     154 * @see WP_Object_Cache::delete_multiple()
     155 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     156 *
     157 * @param array  $keys  Array of keys under which the cache to deleted.
     158 * @param string $group Optional. Where the cache contents are grouped. Default empty.
     159 * @return array Array of return values organized into groups.
     160 */
     161function wp_cache_delete_multiple( array $keys, $group = '' ) {
     162        global $wp_object_cache;
     163
     164        return $wp_object_cache->delete_multiple( $keys, $group );
     165}
     166
     167/**
     168 * Add multiple values to the cache in one call.
     169 *
     170 * @since 6.0.0
     171 *
     172 * @see WP_Object_Cache::add_multiple()
     173 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     174 *
     175 * @param array  $data  Array of key and value to be set.
     176 * @param string $group Optional. Where the cache contents are grouped. Default empty.
     177 * @param int    $expire Optional. When to expire the cache contents, in seconds.
     178 *                           Default 0 (no expiration).
     179 * @return array Array of return values.
     180 */
     181function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
     182        global $wp_object_cache;
     183
     184        return $wp_object_cache->add_multiple( $data, $group, $expire );
     185}
     186
     187/**
     188 * Set multiple values to the cache in one call.
     189 *
     190 * @since 6.0.0
     191 *
     192 * @see WP_Object_Cache::set_multiple()
     193 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     194 *
     195 * @param array  $data  Array of key and value to be set.
     196 * @param string $group Optional. Where the cache contents are grouped. Default empty.
     197 * @param int    $expire Optional. When to expire the cache contents, in seconds.
     198 *                           Default 0 (no expiration).
     199 * @return array Array of return values.
     200 */
     201function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
     202        global $wp_object_cache;
     203
     204        return $wp_object_cache->set_multiple( $data, $group, $expire );
     205}
     206
    148207/**
    149208 * Increment numeric cache item's value
  • trunk/src/wp-includes/class-wp-object-cache.php

    r49692 r52700  
    325325
    326326        /**
     327         * Delete multiple values from the cache in one call.
     328         *
     329         * @since 6.0.0
     330         *
     331         * @param array  $keys  Array of keys to be deleted.
     332         * @param string $group Optional. Where the cache contents are grouped. Default empty.
     333         * @return array Array of return values.
     334         */
     335        public function delete_multiple( array $keys, $group = '' ) {
     336                $values = array();
     337
     338                foreach ( $keys as $key ) {
     339                        $values[ $key ] = $this->delete( $key, $group );
     340                }
     341
     342                return $values;
     343        }
     344
     345        /**
     346         * Add multiple values to the cache in one call.
     347         *
     348         * @since 6.0.0
     349         *
     350         * @param array  $data   Array of key and value to be added.
     351         * @param string $group  Optional. Where the cache contents are grouped. Default empty.
     352         * @param int    $expire Optional. When to expire the cache contents, in seconds. Default 0 (no expiration).
     353         * @return array Array of return values.
     354         */
     355        public function add_multiple( array $data, $group = '', $expire = 0 ) {
     356                $values = array();
     357
     358                foreach ( $data as $key => $value ) {
     359                        $values[ $key ] = $this->add( $key, $value, $group, $expire );
     360                }
     361
     362                return $values;
     363        }
     364
     365        /**
     366         * Set multiple values to the cache in one call.
     367         *
     368         * @since 6.0.0
     369         *
     370         * @param array  $data   Array of key and value to be set.
     371         * @param string $group  Optional. Where the cache contents are grouped. Default empty.
     372         * @param int    $expire Optional. When to expire the cache contents, in seconds. Default 0 (no expiration).
     373         * @return array Array of return values.
     374         */
     375        public function set_multiple( array $data, $group = '', $expire = 0 ) {
     376                $values = array();
     377
     378                foreach ( $data as $key => $value ) {
     379                        $values[ $key ] = $this->set( $key, $value, $group, $expire );
     380                }
     381
     382                return $values;
     383        }
     384
     385        /**
    327386         * Increments numeric cache item's value.
    328387         *
  • 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}
  • trunk/tests/phpunit/tests/pluggable.php

    r51404 r52700  
    296296                                                'force' => false,
    297297                                        ),
     298                                        'wp_cache_set_multiple'              => array(
     299                                                'data',
     300                                                'group'  => '',
     301                                                'expire' => 0,
     302                                        ),
     303                                        'wp_cache_add_multiple'              => array(
     304                                                'data',
     305                                                'group'  => '',
     306                                                'expire' => 0,
     307                                        ),
     308                                        'wp_cache_delete_multiple'           => array(
     309                                                'keys',
     310                                                'group' => '',
     311                                        ),
    298312                                        'wp_cache_incr'                      => array(
    299313                                                'key',
Note: See TracChangeset for help on using the changeset viewer.