Make WordPress Core


Ignore:
Timestamp:
02/11/2022 12:50:54 PM (5 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/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         *
Note: See TracChangeset for help on using the changeset viewer.