Make WordPress Core

Changeset 47938


Ignore:
Timestamp:
06/09/2020 07:45:27 PM (4 years ago)
Author:
whyisjake
Message:

Cache API: Introduce wp_cache_get_multi().

Many caching backend have support for multiple gets in a single request. This brings that support to core, with a compatability fallback that will loop over requests if needed.

Fixes: #20875.
Props: nacin, tollmanz, wonderboymusic, ryan, jeremyfelt, spacedmonkey, boonebgorges, dd32, rmccue, ocean90, jipmoors, johnjamesjacoby, tillkruess, donmhico, davidbaumwald, SergeyBiryukov, whyisjake.

Location:
trunk
Files:
5 edited

Legend:

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

    r47197 r47938  
    128128
    129129/**
     130 * Gets multiple values from cache in one call.
     131 *
     132 * @since 5.5.0
     133 * @see WP_Object_Cache::get_multiple()
     134 *
     135 * @param array       $keys   Array of keys to get from group.
     136 * @param string      $group  Optional. Where the cache contents are grouped. Default empty.
     137 * @param bool        $force  Optional. Whether to force an update of the local cache from the persistent
     138 *                            cache. Default false.
     139 * @return array|bool Array of values.
     140 */
     141function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
     142    global $wp_object_cache;
     143
     144    return $wp_object_cache->get_multiple( $keys, $group, $force );
     145}
     146
     147/**
    130148 * Increment numeric cache item's value
    131149 *
  • trunk/src/wp-includes/class-wp-object-cache.php

    r47637 r47938  
    304304
    305305    /**
     306     * Retrieves multiple values from the cache.
     307     *
     308     * @since  5.5.0
     309     *
     310     * @param array $keys        Array of keys to fetch.
     311     * @param bool  $force       Optional. Unused. Whether to force a refetch rather than relying on the local
     312     *                           cache. Default false.
     313     *
     314     * @return array Array of values organized into groups.
     315     */
     316    public function get_multiple( $keys, $group = 'default', $force = false ) {
     317        $values = array();
     318
     319        foreach ( $keys as $key ) {
     320            $values[ $key ] = $this->get( $key, $group, $force );
     321        }
     322
     323        return $values;
     324    }
     325
     326    /**
    306327     * Increments numeric cache item's value.
    307328     *
  • trunk/src/wp-includes/load.php

    r47919 r47938  
    629629    }
    630630
     631    require_once( ABSPATH . WPINC . '/cache-compat.php' );
     632
    631633    /*
    632634     * If cache supports reset, reset instead of init if already
  • trunk/tests/phpunit/tests/cache.php

    r47122 r47938  
    316316        $this->assertFalse( wp_cache_get( $fake_key ) );
    317317    }
     318
     319    /**
     320     * @ticket 20875
     321     */
     322    public function test_get_multiple() {
     323        wp_cache_set( 'foo1', 'bar', 'group1' );
     324        wp_cache_set( 'foo2', 'bar', 'group1' );
     325        wp_cache_set( 'foo1', 'bar', 'group2' );
     326
     327        $found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3', ), 'group1' );
     328
     329        $expected = array(
     330            'foo1' => 'bar',
     331            'foo2' => 'bar',
     332            'foo3' => false,
     333        );
     334
     335        $this->assertSame( $expected, $found );
     336    }
    318337}
  • trunk/tests/phpunit/tests/pluggable.php

    r47122 r47938  
    291291                        'found' => null,
    292292                    ),
     293                    'wp_cache_get_multiple'              => array(
     294                        'keys',
     295                        'group' => '',
     296                        'force' => false,
     297                    ),
    293298                    'wp_cache_incr'                      => array(
    294299                        'key',
Note: See TracChangeset for help on using the changeset viewer.