Make WordPress Core


Ignore:
Timestamp:
02/11/2022 06:47:38 PM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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,
Note: See TracChangeset for help on using the changeset viewer.