Make WordPress Core

Changeset 52706


Ignore:
Timestamp:
02/11/2022 06:47:38 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Cache API: Reorder object cache functions and methods for consistency.

The original order was alphabetical, which became less obvious as newer functions got added, resulting in a somewhat random order.

This commits aims to organize the functions and related WP_Object_Cache methods in a more predictable order:

  • wp_cache_init()
  • wp_cache_add()
  • wp_cache_add_multiple()
  • wp_cache_replace()
  • wp_cache_set()
  • wp_cache_set_multiple()
  • wp_cache_get()
  • wp_cache_get_multiple()
  • wp_cache_delete()
  • wp_cache_delete_multiple()
  • wp_cache_incr()
  • wp_cache_decr()
  • wp_cache_flush()
  • wp_cache_close()
  • wp_cache_add_global_groups()
  • wp_cache_add_non_persistent_groups()
  • wp_cache_switch_to_blog()
  • wp_cache_reset()

Follow-up to [3011], [6543], [7986], [13066], [18580], [21403], [47938], [52700], [52703-52705].

See #54728, #54574.

Location:
trunk
Files:
5 edited

Legend:

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

    r52703 r52706  
    88 * @subpackage Cache
    99 */
    10 
    11 if ( ! function_exists( 'wp_cache_get_multiple' ) ) :
    12     /**
    13      * Retrieves multiple values from the cache in one call.
    14      *
    15      * Compat function to mimic wp_cache_get_multiple().
    16      *
    17      * @ignore
    18      * @since 5.5.0
    19      *
    20      * @see wp_cache_get_multiple()
    21      *
    22      * @param array  $keys  Array of keys under which the cache contents are stored.
    23      * @param string $group Optional. Where the cache contents are grouped. Default empty.
    24      * @param bool   $force Optional. Whether to force an update of the local cache
    25      *                      from the persistent cache. Default false.
    26      * @return array Array of values organized into groups.
    27      */
    28     function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
    29         $values = array();
    30 
    31         foreach ( $keys as $key ) {
    32             $values[ $key ] = wp_cache_get( $key, $group, $force );
    33         }
    34 
    35         return $values;
    36     }
    37 endif;
    38 
    39 if ( ! function_exists( 'wp_cache_delete_multiple' ) ) :
    40     /**
    41      * Deletes 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     }
    63 endif;
    64 
    6510
    6611if ( ! function_exists( 'wp_cache_add_multiple' ) ) :
     
    12166    }
    12267endif;
     68
     69if ( ! function_exists( 'wp_cache_get_multiple' ) ) :
     70    /**
     71     * Retrieves multiple values from the cache in one call.
     72     *
     73     * Compat function to mimic wp_cache_get_multiple().
     74     *
     75     * @ignore
     76     * @since 5.5.0
     77     *
     78     * @see wp_cache_get_multiple()
     79     *
     80     * @param array  $keys  Array of keys under which the cache contents are stored.
     81     * @param string $group Optional. Where the cache contents are grouped. Default empty.
     82     * @param bool   $force Optional. Whether to force an update of the local cache
     83     *                      from the persistent cache. Default false.
     84     * @return array Array of values organized into groups.
     85     */
     86    function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
     87        $values = array();
     88
     89        foreach ( $keys as $key ) {
     90            $values[ $key ] = wp_cache_get( $key, $group, $force );
     91        }
     92
     93        return $values;
     94    }
     95endif;
     96
     97if ( ! function_exists( 'wp_cache_delete_multiple' ) ) :
     98    /**
     99     * Deletes multiple values from the cache in one call.
     100     *
     101     * Compat function to mimic wp_cache_delete_multiple().
     102     *
     103     * @ignore
     104     * @since 6.0.0
     105     *
     106     * @see wp_cache_delete_multiple()
     107     *
     108     * @param array  $keys  Array of keys under which the cache to deleted.
     109     * @param string $group Optional. Where the cache contents are grouped. Default empty.
     110     * @return array Array of return values.
     111     */
     112    function wp_cache_delete_multiple( array $keys, $group = '' ) {
     113        $values = array();
     114
     115        foreach ( $keys as $key ) {
     116            $values[ $key ] = wp_cache_delete( $key, $group );
     117        }
     118
     119        return $values;
     120    }
     121endif;
  • trunk/src/wp-includes/cache.php

    r52705 r52706  
    1111/** WP_Object_Cache class */
    1212require_once ABSPATH . WPINC . '/class-wp-object-cache.php';
     13
     14/**
     15 * Sets up Object Cache Global and assigns it.
     16 *
     17 * @since 2.0.0
     18 *
     19 * @global WP_Object_Cache $wp_object_cache
     20 */
     21function wp_cache_init() {
     22    $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
     23}
    1324
    1425/**
     
    3546
    3647/**
    37  * Closes the cache.
    38  *
    39  * This function has ceased to do anything since WordPress 2.5. The
    40  * functionality was removed along with the rest of the persistent cache.
    41  *
    42  * This does not mean that plugins can't implement this function when they need
    43  * to make sure that the cache is cleaned up after WordPress no longer needs it.
    44  *
    45  * @since 2.0.0
    46  *
    47  * @return true Always returns true.
    48  */
    49 function wp_cache_close() {
    50     return true;
    51 }
    52 
    53 /**
    54  * Decrements numeric cache item's value.
    55  *
    56  * @since 3.3.0
    57  *
    58  * @see WP_Object_Cache::decr()
    59  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    60  *
    61  * @param int|string $key    The cache key to decrement.
    62  * @param int        $offset Optional. The amount by which to decrement the item's value.
    63  *                           Default 1.
    64  * @param string     $group  Optional. The group the key is in. Default empty.
    65  * @return int|false The item's new value on success, false on failure.
    66  */
    67 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
    68     global $wp_object_cache;
    69 
    70     return $wp_object_cache->decr( $key, $offset, $group );
    71 }
    72 
    73 /**
    74  * Removes the cache contents matching key and group.
    75  *
    76  * @since 2.0.0
    77  *
    78  * @see WP_Object_Cache::delete()
    79  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    80  *
    81  * @param int|string $key   What the contents in the cache are called.
    82  * @param string     $group Optional. Where the cache contents are grouped. Default empty.
    83  * @return bool True on successful removal, false on failure.
    84  */
    85 function wp_cache_delete( $key, $group = '' ) {
    86     global $wp_object_cache;
    87 
    88     return $wp_object_cache->delete( $key, $group );
    89 }
    90 
    91 /**
    92  * Removes all cache items.
    93  *
    94  * @since 2.0.0
    95  *
    96  * @see WP_Object_Cache::flush()
    97  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    98  *
     48 * Adds multiple values to the cache in one call.
     49 *
     50 * @since 6.0.0
     51 *
     52 * @see WP_Object_Cache::add_multiple()
     53 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     54 *
     55 * @param array  $data   Array of keys and values to be set.
     56 * @param string $group  Optional. Where the cache contents are grouped. Default empty.
     57 * @param int    $expire Optional. When to expire the cache contents, in seconds.
     58 *                       Default 0 (no expiration).
     59 * @return array Array of return values.
     60 */
     61function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
     62    global $wp_object_cache;
     63
     64    return $wp_object_cache->add_multiple( $data, $group, $expire );
     65}
     66
     67/**
     68 * Replaces the contents of the cache with new data.
     69 *
     70 * @since 2.0.0
     71 *
     72 * @see WP_Object_Cache::replace()
     73 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     74 *
     75 * @param int|string $key    The key for the cache data that should be replaced.
     76 * @param mixed      $data   The new data to store in the cache.
     77 * @param string     $group  Optional. The group for the cache data that should be replaced.
     78 *                           Default empty.
     79 * @param int        $expire Optional. When to expire the cache contents, in seconds.
     80 *                           Default 0 (no expiration).
     81 * @return bool True if contents were replaced, false if original value does not exist.
     82 */
     83function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
     84    global $wp_object_cache;
     85
     86    return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
     87}
     88
     89/**
     90 * Saves the data to the cache.
     91 *
     92 * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
     93 *
     94 * @since 2.0.0
     95 *
     96 * @see WP_Object_Cache::set()
     97 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     98 *
     99 * @param int|string $key    The cache key to use for retrieval later.
     100 * @param mixed      $data   The contents to store in the cache.
     101 * @param string     $group  Optional. Where to group the cache contents. Enables the same key
     102 *                           to be used across groups. Default empty.
     103 * @param int        $expire Optional. When to expire the cache contents, in seconds.
     104 *                           Default 0 (no expiration).
    99105 * @return bool True on success, false on failure.
    100106 */
    101 function wp_cache_flush() {
    102     global $wp_object_cache;
    103 
    104     return $wp_object_cache->flush();
     107function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
     108    global $wp_object_cache;
     109
     110    return $wp_object_cache->set( $key, $data, $group, (int) $expire );
     111}
     112
     113/**
     114 * Sets multiple values to the cache in one call.
     115 *
     116 * @since 6.0.0
     117 *
     118 * @see WP_Object_Cache::set_multiple()
     119 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     120 *
     121 * @param array  $data   Array of keys and values to be set.
     122 * @param string $group  Optional. Where the cache contents are grouped. Default empty.
     123 * @param int    $expire Optional. When to expire the cache contents, in seconds.
     124 *                       Default 0 (no expiration).
     125 * @return array Array of return values.
     126 */
     127function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
     128    global $wp_object_cache;
     129
     130    return $wp_object_cache->set_multiple( $data, $group, $expire );
    105131}
    106132
     
    148174
    149175/**
     176 * Removes the cache contents matching key and group.
     177 *
     178 * @since 2.0.0
     179 *
     180 * @see WP_Object_Cache::delete()
     181 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     182 *
     183 * @param int|string $key   What the contents in the cache are called.
     184 * @param string     $group Optional. Where the cache contents are grouped. Default empty.
     185 * @return bool True on successful removal, false on failure.
     186 */
     187function wp_cache_delete( $key, $group = '' ) {
     188    global $wp_object_cache;
     189
     190    return $wp_object_cache->delete( $key, $group );
     191}
     192
     193/**
    150194 * Deletes multiple values from the cache in one call.
    151195 *
     
    163207
    164208    return $wp_object_cache->delete_multiple( $keys, $group );
    165 }
    166 
    167 /**
    168  * Adds multiple values to the cache in one call.
    169  *
    170  * @since 6.0.0
    171  *
    172  * @see WP_Object_Cache::add_multiple()
    173  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    174  *
    175  * @param array  $data   Array of keys and values to be set.
    176  * @param string $group  Optional. Where the cache contents are grouped. Default empty.
    177  * @param int    $expire Optional. When to expire the cache contents, in seconds.
    178  *                       Default 0 (no expiration).
    179  * @return array Array of return values.
    180  */
    181 function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
    182     global $wp_object_cache;
    183 
    184     return $wp_object_cache->add_multiple( $data, $group, $expire );
    185 }
    186 
    187 /**
    188  * Sets multiple values to the cache in one call.
    189  *
    190  * @since 6.0.0
    191  *
    192  * @see WP_Object_Cache::set_multiple()
    193  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    194  *
    195  * @param array  $data   Array of keys and values to be set.
    196  * @param string $group  Optional. Where the cache contents are grouped. Default empty.
    197  * @param int    $expire Optional. When to expire the cache contents, in seconds.
    198  *                       Default 0 (no expiration).
    199  * @return array Array of return values.
    200  */
    201 function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
    202     global $wp_object_cache;
    203 
    204     return $wp_object_cache->set_multiple( $data, $group, $expire );
    205209}
    206210
     
    226230
    227231/**
    228  * Sets up Object Cache Global and assigns it.
    229  *
    230  * @since 2.0.0
    231  *
    232  * @global WP_Object_Cache $wp_object_cache
    233  */
    234 function wp_cache_init() {
    235     $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
    236 }
    237 
    238 /**
    239  * Replaces the contents of the cache with new data.
    240  *
    241  * @since 2.0.0
    242  *
    243  * @see WP_Object_Cache::replace()
    244  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    245  *
    246  * @param int|string $key    The key for the cache data that should be replaced.
    247  * @param mixed      $data   The new data to store in the cache.
    248  * @param string     $group  Optional. The group for the cache data that should be replaced.
    249  *                           Default empty.
    250  * @param int        $expire Optional. When to expire the cache contents, in seconds.
    251  *                           Default 0 (no expiration).
    252  * @return bool True if contents were replaced, false if original value does not exist.
    253  */
    254 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
    255     global $wp_object_cache;
    256 
    257     return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
    258 }
    259 
    260 /**
    261  * Saves the data to the cache.
    262  *
    263  * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
    264  *
    265  * @since 2.0.0
    266  *
    267  * @see WP_Object_Cache::set()
    268  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    269  *
    270  * @param int|string $key    The cache key to use for retrieval later.
    271  * @param mixed      $data   The contents to store in the cache.
    272  * @param string     $group  Optional. Where to group the cache contents. Enables the same key
    273  *                           to be used across groups. Default empty.
    274  * @param int        $expire Optional. When to expire the cache contents, in seconds.
    275  *                           Default 0 (no expiration).
     232 * Decrements numeric cache item's value.
     233 *
     234 * @since 3.3.0
     235 *
     236 * @see WP_Object_Cache::decr()
     237 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     238 *
     239 * @param int|string $key    The cache key to decrement.
     240 * @param int        $offset Optional. The amount by which to decrement the item's value.
     241 *                           Default 1.
     242 * @param string     $group  Optional. The group the key is in. Default empty.
     243 * @return int|false The item's new value on success, false on failure.
     244 */
     245function wp_cache_decr( $key, $offset = 1, $group = '' ) {
     246    global $wp_object_cache;
     247
     248    return $wp_object_cache->decr( $key, $offset, $group );
     249}
     250
     251/**
     252 * Removes all cache items.
     253 *
     254 * @since 2.0.0
     255 *
     256 * @see WP_Object_Cache::flush()
     257 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     258 *
    276259 * @return bool True on success, false on failure.
    277260 */
    278 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
    279     global $wp_object_cache;
    280 
    281     return $wp_object_cache->set( $key, $data, $group, (int) $expire );
    282 }
    283 
    284 /**
    285  * Switches the internal blog ID.
    286  *
    287  * This changes the blog id used to create keys in blog specific groups.
    288  *
    289  * @since 3.5.0
    290  *
    291  * @see WP_Object_Cache::switch_to_blog()
    292  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    293  *
    294  * @param int $blog_id Site ID.
    295  */
    296 function wp_cache_switch_to_blog( $blog_id ) {
    297     global $wp_object_cache;
    298 
    299     $wp_object_cache->switch_to_blog( $blog_id );
     261function wp_cache_flush() {
     262    global $wp_object_cache;
     263
     264    return $wp_object_cache->flush();
     265}
     266
     267/**
     268 * Closes the cache.
     269 *
     270 * This function has ceased to do anything since WordPress 2.5. The
     271 * functionality was removed along with the rest of the persistent cache.
     272 *
     273 * This does not mean that plugins can't implement this function when they need
     274 * to make sure that the cache is cleaned up after WordPress no longer needs it.
     275 *
     276 * @since 2.0.0
     277 *
     278 * @return true Always returns true.
     279 */
     280function wp_cache_close() {
     281    return true;
    300282}
    301283
     
    328310
    329311/**
    330  * Reset internal cache keys and structures.
     312 * Switches the internal blog ID.
     313 *
     314 * This changes the blog id used to create keys in blog specific groups.
     315 *
     316 * @since 3.5.0
     317 *
     318 * @see WP_Object_Cache::switch_to_blog()
     319 * @global WP_Object_Cache $wp_object_cache Object cache global instance.
     320 *
     321 * @param int $blog_id Site ID.
     322 */
     323function wp_cache_switch_to_blog( $blog_id ) {
     324    global $wp_object_cache;
     325
     326    $wp_object_cache->switch_to_blog( $blog_id );
     327}
     328
     329/**
     330 * Resets internal cache keys and structures.
    331331 *
    332332 * If the cache back end uses global blog or site IDs as part of its cache keys,
  • trunk/src/wp-includes/class-wp-object-cache.php

    r52705 r52706  
    131131
    132132    /**
     133     * Serves as a utility function to determine whether a key exists in the cache.
     134     *
     135     * @since 3.4.0
     136     *
     137     * @param int|string $key   Cache key to check for existence.
     138     * @param string     $group Cache group for the key existence check.
     139     * @return bool Whether the key exists in the cache for the given group.
     140     */
     141    protected function _exists( $key, $group ) {
     142        return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
     143    }
     144
     145    /**
    133146     * Adds data to the cache if it doesn't already exist.
    134147     *
     
    168181
    169182    /**
    170      * Sets the list of global cache groups.
    171      *
    172      * @since 3.0.0
    173      *
    174      * @param string|string[] $groups List of groups that are global.
    175      */
    176     public function add_global_groups( $groups ) {
    177         $groups = (array) $groups;
    178 
    179         $groups              = array_fill_keys( $groups, true );
    180         $this->global_groups = array_merge( $this->global_groups, $groups );
    181     }
    182 
    183     /**
    184      * Decrements numeric cache item's value.
    185      *
    186      * @since 3.3.0
    187      *
    188      * @param int|string $key    The cache key to decrement.
    189      * @param int        $offset Optional. The amount by which to decrement the item's value.
    190      *                           Default 1.
    191      * @param string     $group  Optional. The group the key is in. Default 'default'.
    192      * @return int|false The item's new value on success, false on failure.
    193      */
    194     public function decr( $key, $offset = 1, $group = 'default' ) {
     183     * Adds multiple values to the cache in one call.
     184     *
     185     * @since 6.0.0
     186     *
     187     * @param array  $data   Array of keys and values to be added.
     188     * @param string $group  Optional. Where the cache contents are grouped. Default empty.
     189     * @param int    $expire Optional. When to expire the cache contents, in seconds.
     190     *                       Default 0 (no expiration).
     191     * @return array Array of return values.
     192     */
     193    public function add_multiple( array $data, $group = '', $expire = 0 ) {
     194        $values = array();
     195
     196        foreach ( $data as $key => $value ) {
     197            $values[ $key ] = $this->add( $key, $value, $group, $expire );
     198        }
     199
     200        return $values;
     201    }
     202
     203    /**
     204     * Replaces the contents in the cache, if contents already exist.
     205     *
     206     * @since 2.0.0
     207     *
     208     * @see WP_Object_Cache::set()
     209     *
     210     * @param int|string $key    What to call the contents in the cache.
     211     * @param mixed      $data   The contents to store in the cache.
     212     * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
     213     * @param int        $expire Optional. When to expire the cache contents, in seconds.
     214     *                           Default 0 (no expiration).
     215     * @return bool True if contents were replaced, false if original value does not exist.
     216     */
     217    public function replace( $key, $data, $group = 'default', $expire = 0 ) {
     218        if ( empty( $group ) ) {
     219            $group = 'default';
     220        }
     221
     222        $id = $key;
     223        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
     224            $id = $this->blog_prefix . $key;
     225        }
     226
     227        if ( ! $this->_exists( $id, $group ) ) {
     228            return false;
     229        }
     230
     231        return $this->set( $key, $data, $group, (int) $expire );
     232    }
     233
     234    /**
     235     * Sets the data contents into the cache.
     236     *
     237     * The cache contents are grouped by the $group parameter followed by the
     238     * $key. This allows for duplicate IDs in unique groups. Therefore, naming of
     239     * the group should be used with care and should follow normal function
     240     * naming guidelines outside of core WordPress usage.
     241     *
     242     * The $expire parameter is not used, because the cache will automatically
     243     * expire for each time a page is accessed and PHP finishes. The method is
     244     * more for cache plugins which use files.
     245     *
     246     * @since 2.0.0
     247     *
     248     * @param int|string $key    What to call the contents in the cache.
     249     * @param mixed      $data   The contents to store in the cache.
     250     * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
     251     * @param int        $expire Optional. Not used.
     252     * @return true Always returns true.
     253     */
     254    public function set( $key, $data, $group = 'default', $expire = 0 ) {
    195255        if ( empty( $group ) ) {
    196256            $group = 'default';
     
    201261        }
    202262
    203         if ( ! $this->_exists( $key, $group ) ) {
    204             return false;
    205         }
    206 
    207         if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
    208             $this->cache[ $group ][ $key ] = 0;
    209         }
    210 
    211         $offset = (int) $offset;
    212 
    213         $this->cache[ $group ][ $key ] -= $offset;
    214 
    215         if ( $this->cache[ $group ][ $key ] < 0 ) {
    216             $this->cache[ $group ][ $key ] = 0;
    217         }
    218 
    219         return $this->cache[ $group ][ $key ];
    220     }
    221 
    222     /**
    223      * Removes the contents of the cache key in the group.
    224      *
    225      * If the cache key does not exist in the group, then nothing will happen.
    226      *
    227      * @since 2.0.0
    228      *
    229      * @param int|string $key        What the contents in the cache are called.
    230      * @param string     $group      Optional. Where the cache contents are grouped. Default 'default'.
    231      * @param bool       $deprecated Optional. Unused. Default false.
    232      * @return bool True on success, false if the contents were not deleted.
    233      */
    234     public function delete( $key, $group = 'default', $deprecated = false ) {
    235         if ( empty( $group ) ) {
    236             $group = 'default';
    237         }
    238 
    239         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
    240             $key = $this->blog_prefix . $key;
    241         }
    242 
    243         if ( ! $this->_exists( $key, $group ) ) {
    244             return false;
    245         }
    246 
    247         unset( $this->cache[ $group ][ $key ] );
     263        if ( is_object( $data ) ) {
     264            $data = clone $data;
     265        }
     266
     267        $this->cache[ $group ][ $key ] = $data;
    248268        return true;
    249269    }
    250270
    251271    /**
    252      * Clears the object cache of all data.
    253      *
    254      * @since 2.0.0
    255      *
    256      * @return true Always returns true.
    257      */
    258     public function flush() {
    259         $this->cache = array();
    260 
    261         return true;
     272     * Sets multiple values to the cache in one call.
     273     *
     274     * @since 6.0.0
     275     *
     276     * @param array  $data   Array of key and value to be set.
     277     * @param string $group  Optional. Where the cache contents are grouped. Default empty.
     278     * @param int    $expire Optional. When to expire the cache contents, in seconds.
     279     *                       Default 0 (no expiration).
     280     * @return array Array of return values.
     281     */
     282    public function set_multiple( array $data, $group = '', $expire = 0 ) {
     283        $values = array();
     284
     285        foreach ( $data as $key => $value ) {
     286            $values[ $key ] = $this->set( $key, $value, $group, $expire );
     287        }
     288
     289        return $values;
    262290    }
    263291
     
    327355
    328356    /**
     357     * Removes the contents of the cache key in the group.
     358     *
     359     * If the cache key does not exist in the group, then nothing will happen.
     360     *
     361     * @since 2.0.0
     362     *
     363     * @param int|string $key        What the contents in the cache are called.
     364     * @param string     $group      Optional. Where the cache contents are grouped. Default 'default'.
     365     * @param bool       $deprecated Optional. Unused. Default false.
     366     * @return bool True on success, false if the contents were not deleted.
     367     */
     368    public function delete( $key, $group = 'default', $deprecated = false ) {
     369        if ( empty( $group ) ) {
     370            $group = 'default';
     371        }
     372
     373        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
     374            $key = $this->blog_prefix . $key;
     375        }
     376
     377        if ( ! $this->_exists( $key, $group ) ) {
     378            return false;
     379        }
     380
     381        unset( $this->cache[ $group ][ $key ] );
     382        return true;
     383    }
     384
     385    /**
    329386     * Deletes multiple values from the cache in one call.
    330387     *
     
    340397        foreach ( $keys as $key ) {
    341398            $values[ $key ] = $this->delete( $key, $group );
    342         }
    343 
    344         return $values;
    345     }
    346 
    347     /**
    348      * Adds multiple values to the cache in one call.
    349      *
    350      * @since 6.0.0
    351      *
    352      * @param array  $data   Array of keys and values to be added.
    353      * @param string $group  Optional. Where the cache contents are grouped. Default empty.
    354      * @param int    $expire Optional. When to expire the cache contents, in seconds.
    355      *                       Default 0 (no expiration).
    356      * @return array Array of return values.
    357      */
    358     public function add_multiple( array $data, $group = '', $expire = 0 ) {
    359         $values = array();
    360 
    361         foreach ( $data as $key => $value ) {
    362             $values[ $key ] = $this->add( $key, $value, $group, $expire );
    363         }
    364 
    365         return $values;
    366     }
    367 
    368     /**
    369      * Sets multiple values to the cache in one call.
    370      *
    371      * @since 6.0.0
    372      *
    373      * @param array  $data   Array of key and value to be set.
    374      * @param string $group  Optional. Where the cache contents are grouped. Default empty.
    375      * @param int    $expire Optional. When to expire the cache contents, in seconds.
    376      *                       Default 0 (no expiration).
    377      * @return array Array of return values.
    378      */
    379     public function set_multiple( array $data, $group = '', $expire = 0 ) {
    380         $values = array();
    381 
    382         foreach ( $data as $key => $value ) {
    383             $values[ $key ] = $this->set( $key, $value, $group, $expire );
    384399        }
    385400
     
    427442
    428443    /**
    429      * Replaces the contents in the cache, if contents already exist.
    430      *
    431      * @since 2.0.0
    432      *
    433      * @see WP_Object_Cache::set()
    434      *
    435      * @param int|string $key    What to call the contents in the cache.
    436      * @param mixed      $data   The contents to store in the cache.
    437      * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
    438      * @param int        $expire Optional. When to expire the cache contents, in seconds.
    439      *                           Default 0 (no expiration).
    440      * @return bool True if contents were replaced, false if original value does not exist.
    441      */
    442     public function replace( $key, $data, $group = 'default', $expire = 0 ) {
    443         if ( empty( $group ) ) {
    444             $group = 'default';
    445         }
    446 
    447         $id = $key;
    448         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
    449             $id = $this->blog_prefix . $key;
    450         }
    451 
    452         if ( ! $this->_exists( $id, $group ) ) {
     444     * Decrements numeric cache item's value.
     445     *
     446     * @since 3.3.0
     447     *
     448     * @param int|string $key    The cache key to decrement.
     449     * @param int        $offset Optional. The amount by which to decrement the item's value.
     450     *                           Default 1.
     451     * @param string     $group  Optional. The group the key is in. Default 'default'.
     452     * @return int|false The item's new value on success, false on failure.
     453     */
     454    public function decr( $key, $offset = 1, $group = 'default' ) {
     455        if ( empty( $group ) ) {
     456            $group = 'default';
     457        }
     458
     459        if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
     460            $key = $this->blog_prefix . $key;
     461        }
     462
     463        if ( ! $this->_exists( $key, $group ) ) {
    453464            return false;
    454465        }
    455466
    456         return $this->set( $key, $data, $group, (int) $expire );
     467        if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
     468            $this->cache[ $group ][ $key ] = 0;
     469        }
     470
     471        $offset = (int) $offset;
     472
     473        $this->cache[ $group ][ $key ] -= $offset;
     474
     475        if ( $this->cache[ $group ][ $key ] < 0 ) {
     476            $this->cache[ $group ][ $key ] = 0;
     477        }
     478
     479        return $this->cache[ $group ][ $key ];
     480    }
     481
     482    /**
     483     * Clears the object cache of all data.
     484     *
     485     * @since 2.0.0
     486     *
     487     * @return true Always returns true.
     488     */
     489    public function flush() {
     490        $this->cache = array();
     491
     492        return true;
     493    }
     494
     495    /**
     496     * Sets the list of global cache groups.
     497     *
     498     * @since 3.0.0
     499     *
     500     * @param string|string[] $groups List of groups that are global.
     501     */
     502    public function add_global_groups( $groups ) {
     503        $groups = (array) $groups;
     504
     505        $groups              = array_fill_keys( $groups, true );
     506        $this->global_groups = array_merge( $this->global_groups, $groups );
     507    }
     508
     509    /**
     510     * Switches the internal blog ID.
     511     *
     512     * This changes the blog ID used to create keys in blog specific groups.
     513     *
     514     * @since 3.5.0
     515     *
     516     * @param int $blog_id Blog ID.
     517     */
     518    public function switch_to_blog( $blog_id ) {
     519        $blog_id           = (int) $blog_id;
     520        $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
    457521    }
    458522
     
    474538            }
    475539        }
    476     }
    477 
    478     /**
    479      * Sets the data contents into the cache.
    480      *
    481      * The cache contents are grouped by the $group parameter followed by the
    482      * $key. This allows for duplicate IDs in unique groups. Therefore, naming of
    483      * the group should be used with care and should follow normal function
    484      * naming guidelines outside of core WordPress usage.
    485      *
    486      * The $expire parameter is not used, because the cache will automatically
    487      * expire for each time a page is accessed and PHP finishes. The method is
    488      * more for cache plugins which use files.
    489      *
    490      * @since 2.0.0
    491      *
    492      * @param int|string $key    What to call the contents in the cache.
    493      * @param mixed      $data   The contents to store in the cache.
    494      * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
    495      * @param int        $expire Optional. Not used.
    496      * @return true Always returns true.
    497      */
    498     public function set( $key, $data, $group = 'default', $expire = 0 ) {
    499         if ( empty( $group ) ) {
    500             $group = 'default';
    501         }
    502 
    503         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
    504             $key = $this->blog_prefix . $key;
    505         }
    506 
    507         if ( is_object( $data ) ) {
    508             $data = clone $data;
    509         }
    510 
    511         $this->cache[ $group ][ $key ] = $data;
    512         return true;
    513540    }
    514541
     
    532559        echo '</ul>';
    533560    }
    534 
    535     /**
    536      * Switches the internal blog ID.
    537      *
    538      * This changes the blog ID used to create keys in blog specific groups.
    539      *
    540      * @since 3.5.0
    541      *
    542      * @param int $blog_id Blog ID.
    543      */
    544     public function switch_to_blog( $blog_id ) {
    545         $blog_id           = (int) $blog_id;
    546         $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
    547     }
    548 
    549     /**
    550      * Serves as a utility function to determine whether a key exists in the cache.
    551      *
    552      * @since 3.4.0
    553      *
    554      * @param int|string $key   Cache key to check for existence.
    555      * @param string     $group Cache group for the key existence check.
    556      * @return bool Whether the key exists in the cache for the given group.
    557      */
    558     protected function _exists( $key, $group ) {
    559         return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
    560     }
    561561}
  • trunk/tests/phpunit/tests/cache.php

    r52702 r52706  
    333333
    334334    /**
    335      * @ticket 20875
    336      */
    337     public function test_get_multiple() {
    338         wp_cache_set( 'foo1', 'bar', 'group1' );
    339         wp_cache_set( 'foo2', 'bar', 'group1' );
    340         wp_cache_set( 'foo1', 'bar', 'group2' );
    341 
    342         $found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' );
    343 
    344         $expected = array(
    345             'foo1' => 'bar',
    346             'foo2' => 'bar',
    347             'foo3' => false,
    348         );
    349 
    350         $this->assertSame( $expected, $found );
    351     }
    352 
    353     /**
    354335     * @ticket 54574
    355336     */
     
    396377
    397378    /**
     379     * @ticket 20875
     380     */
     381    public function test_wp_cache_get_multiple() {
     382        wp_cache_set( 'foo1', 'bar', 'group1' );
     383        wp_cache_set( 'foo2', 'bar', 'group1' );
     384        wp_cache_set( 'foo1', 'bar', 'group2' );
     385
     386        $found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3' ), 'group1' );
     387
     388        $expected = array(
     389            'foo1' => 'bar',
     390            'foo2' => 'bar',
     391            'foo3' => false,
     392        );
     393
     394        $this->assertSame( $expected, $found );
     395    }
     396
     397    /**
    398398     * @ticket 54574
    399399     */
  • trunk/tests/phpunit/tests/pluggable.php

    r52700 r52706  
    268268
    269269                    // wp-includes/cache.php:
     270                    'wp_cache_init'                      => array(),
    270271                    'wp_cache_add'                       => array(
    271272                        'key',
     
    274275                        'expire' => 0,
    275276                    ),
    276                     'wp_cache_close'                     => array(),
    277                     'wp_cache_decr'                      => array(
    278                         'key',
    279                         'offset' => 1,
    280                         'group'  => '',
    281                     ),
    282                     'wp_cache_delete'                    => array(
    283                         'key',
    284                         'group' => '',
    285                     ),
    286                     'wp_cache_flush'                     => array(),
     277                    'wp_cache_add_multiple'              => array(
     278                        'data',
     279                        'group'  => '',
     280                        'expire' => 0,
     281                    ),
     282                    'wp_cache_replace'                   => array(
     283                        'key',
     284                        'data',
     285                        'group'  => '',
     286                        'expire' => 0,
     287                    ),
     288                    'wp_cache_set'                       => array(
     289                        'key',
     290                        'data',
     291                        'group'  => '',
     292                        'expire' => 0,
     293                    ),
     294                    'wp_cache_set_multiple'              => array(
     295                        'data',
     296                        'group'  => '',
     297                        'expire' => 0,
     298                    ),
    287299                    'wp_cache_get'                       => array(
    288300                        'key',
     
    296308                        'force' => false,
    297309                    ),
    298                     'wp_cache_set_multiple'              => array(
    299                         'data',
    300                         'group'  => '',
    301                         'expire' => 0,
    302                     ),
    303                     'wp_cache_add_multiple'              => array(
    304                         'data',
    305                         'group'  => '',
    306                         'expire' => 0,
     310                    'wp_cache_delete'                    => array(
     311                        'key',
     312                        'group' => '',
    307313                    ),
    308314                    'wp_cache_delete_multiple'           => array(
     
    315321                        'group'  => '',
    316322                    ),
    317                     'wp_cache_init'                      => array(),
    318                     'wp_cache_replace'                   => array(
    319                         'key',
    320                         'data',
    321                         'group'  => '',
    322                         'expire' => 0,
    323                     ),
    324                     'wp_cache_set'                       => array(
    325                         'key',
    326                         'data',
    327                         'group'  => '',
    328                         'expire' => 0,
    329                     ),
    330                     'wp_cache_switch_to_blog'            => array( 'blog_id' ),
     323                    'wp_cache_decr'                      => array(
     324                        'key',
     325                        'offset' => 1,
     326                        'group'  => '',
     327                    ),
     328                    'wp_cache_flush'                     => array(),
     329                    'wp_cache_close'                     => array(),
    331330                    'wp_cache_add_global_groups'         => array( 'groups' ),
    332331                    'wp_cache_add_non_persistent_groups' => array( 'groups' ),
     332                    'wp_cache_switch_to_blog'            => array( 'blog_id' ),
    333333                    'wp_cache_reset'                     => array(),
    334334                )
Note: See TracChangeset for help on using the changeset viewer.