Make WordPress Core


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