Make WordPress Core

Changeset 54423


Ignore:
Timestamp:
10/08/2022 01:39:43 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Tests: Add wp_cache_*_multiple() functions to Memcached implementation used in the test suite.

Since this object cache implementation was added, WordPress has introduced a variety of caching API improvements:

  • wp_cache_add_multiple()
  • wp_cache_set_multiple()
  • wp_cache_get_multiple()
  • wp_cache_delete_multiple()

Although WordPress core provides a compatibility layer if these functions are missing from third-party object caches, this commit updates the Memcached object cache used in the test suite to implement these new functions directly.

Follow-up to [40561], [47938], [47944], [52700], [52703], [52706], [52708].

Props petitphp, spacedmonkey, tillkruss, SergeyBiryukov.
Fixes #54864.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/object-cache.php

    r53829 r54423  
    3939    global $wp_object_cache;
    4040    return $wp_object_cache->addByKey( $server_key, $key, $value, $group, $expiration );
     41}
     42
     43/**
     44 * Adds multiple values to the cache in one call, if the cache keys don't already exist.
     45 *
     46 * @param array  $items      Array of keys and values to be added.
     47 * @param string $group      Optional. Where the cache contents are grouped. Default empty.
     48 * @param int    $expiration Optional. When to expire the cache contents, in seconds.
     49 *                           Default 0 (no expiration).
     50 * @return bool[] Array of return values, grouped by key. Each value is either
     51 *                true on success, or false if cache key and group already exist.
     52 */
     53function wp_cache_add_multiple( array $items, $group = '', $expiration = 0 ) {
     54    global $wp_object_cache;
     55    return $wp_object_cache->addMultiple( $items, $group, $expiration );
    4156}
    4257
     
    246261    global $wp_object_cache;
    247262    return $wp_object_cache->deleteByKey( $server_key, $key, $group, $time );
     263}
     264
     265/**
     266 * Deletes multiple values from the cache in one call.
     267 *
     268 * @param array  $keys  Array of keys under which the cache to deleted.
     269 * @param string $group Optional. Where the cache contents are grouped. Default empty.
     270 * @return bool[] Array of return values, grouped by key. Each value is either
     271 *                true on success, or false if the contents were not deleted.
     272 */
     273function wp_cache_delete_multiple( array $keys, $group = '' ) {
     274    global $wp_object_cache;
     275    return $wp_object_cache->deleteMultiple( $keys, $group );
    248276}
    249277
     
    441469
    442470/**
     471 * Retrieves multiple values from the cache in one call.
     472 *
     473 * @param array  $keys  Array of keys under which the cache contents are stored.
     474 * @param string $group Optional. Where the cache contents are grouped. Default empty.
     475 * @param bool   $force Optional. Whether to force an update of the local cache
     476 *                      from the persistent cache. Default false.
     477 * @return array Array of return values, grouped by key. Each value is either
     478 *               the cache contents on success, or false on failure.
     479 */
     480function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
     481    global $wp_object_cache;
     482    return $wp_object_cache->getMultiple( $keys, $group, $force );
     483}
     484
     485/**
    443486 * Retrieves a Memcached option value.
    444487 *
     
    718761    global $wp_object_cache;
    719762    return $wp_object_cache->setMultiByKey( $server_key, $items, $groups, $expiration );
     763}
     764
     765/**
     766 * Sets multiple values to the cache in one call.
     767 *
     768 * Differs from wp_cache_add_multiple() in that it will always write data.
     769 *
     770 * @param array  $items      Array of keys and values to be set.
     771 * @param string $group      Optional. Where the cache contents are grouped. Default empty.
     772 * @param int    $expiration Optional. When to expire the cache contents, in seconds.
     773 *                           Default 0 (no expiration).
     774 * @return bool[] Array of return values, grouped by key. Each value is either
     775 *                true on success, or false on failure.
     776 */
     777function wp_cache_set_multiple( array $items, $group = '', $expiration = 0 ) {
     778    global $wp_object_cache;
     779    return $wp_object_cache->setMultiple( $items, $group, $expiration );
    720780}
    721781
     
    9581018
    9591019    /**
     1020     * Adds multiple values to cache.
     1021     *
     1022     * @param array  $items      Array of keys and values to be added.
     1023     * @param string $group      Optional. Where the cache contents are grouped. Default empty.
     1024     * @param int    $expiration Optional. When to expire the cache contents, in seconds.
     1025     *                           Default 0 (no expiration).
     1026     * @return bool[] Array of return values, grouped by key. Each value is either
     1027     *                true on success, or false if cache key and group already exist.
     1028     */
     1029    public function addMultiple( array $items, $group = '', $expiration = 0 ) {
     1030        $values = array();
     1031
     1032        foreach ( $items as $key => $value ) {
     1033            $values[ $key ] = $this->add( $key, $value, $group, $expiration );
     1034        }
     1035
     1036        return $values;
     1037    }
     1038
     1039    /**
    9601040     * Adds a single server to the list of Memcached servers.
    9611041     *
     
    12581338    public function deleteByKey( $server_key, $key, $group = 'default', $time = 0 ) {
    12591339        return $this->delete( $key, $group, $time, $server_key, true );
     1340    }
     1341
     1342    /**
     1343     * Removes multiple items from the cache.
     1344     *
     1345     * @param array  $keys  Array of keys under which the cache to deleted.
     1346     * @param string $group Optional. Where the cache contents are grouped. Default empty.
     1347     * @return bool[] Array of return values, grouped by key. Each value is either
     1348     *                true on success, or false if the contents were not deleted.
     1349     */
     1350    public function deleteMultiple( $keys, $group ) {
     1351        $values = array();
     1352
     1353        foreach ( $keys as $key ) {
     1354            $values[ $key ] = $this->delete( $key, $group );
     1355        }
     1356
     1357        return $values;
    12601358    }
    12611359
     
    15381636
    15391637    /**
     1638     * Get multiple items from the cache.
     1639     *
     1640     * @param array  $keys  Array of keys under which the cache contents are stored.
     1641     * @param string $group Optional. Where the cache contents are grouped. Default empty.
     1642     * @param bool   $force Optional. Whether to force an update of the local cache
     1643     *                      from the persistent cache. Default false.
     1644     * @return array Array of return values, grouped by key. Each value is either
     1645     *               the cache contents on success, or false on failure.
     1646     */
     1647    public function getMultiple( $keys, $group = '', $force = false ) {
     1648        $values = array();
     1649
     1650        foreach ( $keys as $key ) {
     1651            $found          = null;
     1652            $value          = $this->get( $key, $group, $force, $found );
     1653            $values[ $key ] = $found ? $value : false;
     1654        }
     1655
     1656        return $values;
     1657    }
     1658
     1659    /**
    15401660     * Retrieves a Memcached option value.
    15411661     *
     
    19512071    public function setMultiByKey( $server_key, $items, $groups = 'default', $expiration = 0 ) {
    19522072        return $this->setMulti( $items, $groups, $expiration, $server_key, true );
     2073    }
     2074
     2075    /**
     2076     * Sets multiple values in cache.
     2077     *
     2078     * @param array  $items      Array of keys and values to be set.
     2079     * @param string $group      Optional. Where the cache contents are grouped. Default empty.
     2080     * @param int    $expiration Optional. When to expire the cache contents, in seconds.
     2081     *                           Default 0 (no expiration).
     2082     * @return bool[] Array of return values, grouped by key. Each value is either
     2083     *                true on success, or false on failure.
     2084     */
     2085    public function setMultiple( array $items, $group = '', $expiration = 0 ) {
     2086        $values = array();
     2087
     2088        foreach ( $items as $key => $value ) {
     2089            $values[ $key ] = $this->set( $key, $value, $group, $expiration );
     2090        }
     2091
     2092        return $values;
    19532093    }
    19542094
Note: See TracChangeset for help on using the changeset viewer.