Make WordPress Core

Ticket #20875: 20875.11.diff

File 20875.11.diff, 4.9 KB (added by tillkruess, 6 years ago)
  • src/wp-includes/cache-compat.php

     
     1<?php
     2/**
     3 * Object Cache API functions missing from 3rd party object caches.
     4 *
     5 * @link https://codex.wordpress.org/Class_Reference/WP_Object_Cache
     6 *
     7 * @package WordPress
     8 * @subpackage Cache
     9 */
     10
     11if ( ! function_exists( 'wp_cache_get_multiple' ) ) :
     12        /**
     13         * Compat function to mimic wp_cache_get_multiple.
     14         * Retrieves multiple values from the cache.
     15         *
     16         * @ignore
     17         * @since 5.5.0
     18         *
     19         * @see wp_cache_get_multiple()
     20         *
     21         * @param array $keys        Array of keys to fetch.
     22         * @param bool  $force       Optional. Unused. Whether to force a refetch rather than relying on the local
     23         *                           cache. Default false.
     24         *
     25         * @return array Array of values organized into groups.
     26         */
     27        function wp_cache_get_multiple( $keys, $group = 'default', $force = false ) {
     28                $values = array();
     29
     30                foreach ( $keys as $key ) {
     31                        $values[ $key ] = wp_cache_get( $key, $group, $force );
     32                }
     33
     34                return $values;
     35        }
     36endif;
  • src/wp-includes/cache.php

    Property changes on: src/wp-includes/cache-compat.php
    ___________________________________________________________________
    Added: svn:eol-style
    ## -0,0 +1 ##
    +native
    \ No newline at end of property
     
    127127}
    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 *
    132150 * @since 3.3.0
  • src/wp-includes/class-wp-object-cache.php

     
    303303        }
    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         *
    308329         * @since 3.3.0
  • src/wp-includes/load.php

     
    569569                require_once ABSPATH . WPINC . '/cache.php';
    570570        }
    571571
     572        require_once( ABSPATH . WPINC . '/cache-compat.php' );
     573
    572574        /*
    573575         * If cache supports reset, reset instead of init if already
    574576         * initialized. Reset signals to the cache that global IDs
  • tests/phpunit/tests/cache.php

     
    315315                // Make sure $fake_key is not stored.
    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}
  • tests/phpunit/tests/pluggable.php

     
    290290                                                'force' => false,
    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',
    295300                                                'offset' => 1,