wp/wp-includes/cache.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Object Cache API
     3  * Object Cache API
     4  *
     4  *
     5  * @link https://codex.wordpress.org/Class_Reference/WP_Object_Cache
     5  * @link https://developer.wordpress.org/reference/classes/wp_object_cache/
     6  *
     6  *
     7  * @package WordPress
     7  * @package WordPress
     8  * @subpackage Cache
     8  * @subpackage Cache
     9  */
     9  */
    10 
    10 
    11 /** WP_Object_Cache class */
    11 /** WP_Object_Cache class */
    12 require_once ABSPATH . WPINC . '/class-wp-object-cache.php';
    12 require_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  */
       
    21 function wp_cache_init() {
       
    22 	$GLOBALS['wp_object_cache'] = new WP_Object_Cache();
       
    23 }
    13 
    24 
    14 /**
    25 /**
    15  * Adds data to the cache, if the cache key doesn't already exist.
    26  * Adds data to the cache, if the cache key doesn't already exist.
    16  *
    27  *
    17  * @since 2.0.0
    28  * @since 2.0.0
    32 
    43 
    33 	return $wp_object_cache->add( $key, $data, $group, (int) $expire );
    44 	return $wp_object_cache->add( $key, $data, $group, (int) $expire );
    34 }
    45 }
    35 
    46 
    36 /**
    47 /**
    37  * Closes the cache.
    48  * Adds multiple values to the cache in one call.
    38  *
    49  *
    39  * This function has ceased to do anything since WordPress 2.5. The
    50  * @since 6.0.0
    40  * functionality was removed along with the rest of the persistent cache.
    51  *
    41  *
    52  * @see WP_Object_Cache::add_multiple()
    42  * This does not mean that plugins can't implement this function when they need
    53  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    43  * to make sure that the cache is cleaned up after WordPress no longer needs it.
    54  *
    44  *
    55  * @param array  $data   Array of keys and values to be set.
    45  * @since 2.0.0
    56  * @param string $group  Optional. Where the cache contents are grouped. Default empty.
    46  *
    57  * @param int    $expire Optional. When to expire the cache contents, in seconds.
    47  * @return true Always returns true.
    58  *                       Default 0 (no expiration).
    48  */
    59  * @return bool[] Array of return values, grouped by key. Each value is either
    49 function wp_cache_close() {
    60  *                true on success, or false if cache key and group already exist.
    50 	return true;
    61  */
    51 }
    62 function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
    52 
    63 	global $wp_object_cache;
    53 /**
    64 
    54  * Decrements numeric cache item's value.
    65 	return $wp_object_cache->add_multiple( $data, $group, $expire );
    55  *
    66 }
    56  * @since 3.3.0
    67 
    57  *
    68 /**
    58  * @see WP_Object_Cache::decr()
    69  * Replaces the contents of the cache with new data.
    59  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    70  *
    60  *
    71  * @since 2.0.0
    61  * @param int|string $key    The cache key to decrement.
    72  *
    62  * @param int        $offset Optional. The amount by which to decrement the item's value. Default 1.
    73  * @see WP_Object_Cache::replace()
    63  * @param string     $group  Optional. The group the key is in. Default empty.
    74  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    64  * @return int|false The item's new value on success, false on failure.
    75  *
    65  */
    76  * @param int|string $key    The key for the cache data that should be replaced.
    66 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
    77  * @param mixed      $data   The new data to store in the cache.
    67 	global $wp_object_cache;
    78  * @param string     $group  Optional. The group for the cache data that should be replaced.
    68 
    79  *                           Default empty.
    69 	return $wp_object_cache->decr( $key, $offset, $group );
    80  * @param int        $expire Optional. When to expire the cache contents, in seconds.
    70 }
    81  *                           Default 0 (no expiration).
    71 
    82  * @return bool True if contents were replaced, false if original value does not exist.
    72 /**
    83  */
    73  * Removes the cache contents matching key and group.
    84 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
    74  *
    85 	global $wp_object_cache;
    75  * @since 2.0.0
    86 
    76  *
    87 	return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
    77  * @see WP_Object_Cache::delete()
    88 }
    78  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    89 
    79  *
    90 /**
    80  * @param int|string $key   What the contents in the cache are called.
    91  * Saves the data to the cache.
    81  * @param string     $group Optional. Where the cache contents are grouped. Default empty.
    92  *
    82  * @return bool True on successful removal, false on failure.
    93  * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
    83  */
    94  *
    84 function wp_cache_delete( $key, $group = '' ) {
    95  * @since 2.0.0
    85 	global $wp_object_cache;
    96  *
    86 
    97  * @see WP_Object_Cache::set()
    87 	return $wp_object_cache->delete( $key, $group );
    98  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
    88 }
    99  *
    89 
   100  * @param int|string $key    The cache key to use for retrieval later.
    90 /**
   101  * @param mixed      $data   The contents to store in the cache.
    91  * Removes all cache items.
   102  * @param string     $group  Optional. Where to group the cache contents. Enables the same key
    92  *
   103  *                           to be used across groups. Default empty.
    93  * @since 2.0.0
   104  * @param int        $expire Optional. When to expire the cache contents, in seconds.
    94  *
   105  *                           Default 0 (no expiration).
    95  * @see WP_Object_Cache::flush()
       
    96  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
       
    97  *
       
    98  * @return bool True on success, false on failure.
   106  * @return bool True on success, false on failure.
    99  */
   107  */
   100 function wp_cache_flush() {
   108 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
   101 	global $wp_object_cache;
   109 	global $wp_object_cache;
   102 
   110 
   103 	return $wp_object_cache->flush();
   111 	return $wp_object_cache->set( $key, $data, $group, (int) $expire );
       
   112 }
       
   113 
       
   114 /**
       
   115  * Sets multiple values to the cache in one call.
       
   116  *
       
   117  * @since 6.0.0
       
   118  *
       
   119  * @see WP_Object_Cache::set_multiple()
       
   120  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
       
   121  *
       
   122  * @param array  $data   Array of keys and values to be set.
       
   123  * @param string $group  Optional. Where the cache contents are grouped. Default empty.
       
   124  * @param int    $expire Optional. When to expire the cache contents, in seconds.
       
   125  *                       Default 0 (no expiration).
       
   126  * @return bool[] Array of return values, grouped by key. Each value is either
       
   127  *                true on success, or false on failure.
       
   128  */
       
   129 function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) {
       
   130 	global $wp_object_cache;
       
   131 
       
   132 	return $wp_object_cache->set_multiple( $data, $group, $expire );
   104 }
   133 }
   105 
   134 
   106 /**
   135 /**
   107  * Retrieves the cache contents from the cache by key and group.
   136  * Retrieves the cache contents from the cache by key and group.
   108  *
   137  *
   135  *
   164  *
   136  * @param array  $keys  Array of keys under which the cache contents are stored.
   165  * @param array  $keys  Array of keys under which the cache contents are stored.
   137  * @param string $group Optional. Where the cache contents are grouped. Default empty.
   166  * @param string $group Optional. Where the cache contents are grouped. Default empty.
   138  * @param bool   $force Optional. Whether to force an update of the local cache
   167  * @param bool   $force Optional. Whether to force an update of the local cache
   139  *                      from the persistent cache. Default false.
   168  *                      from the persistent cache. Default false.
   140  * @return array Array of values organized into groups.
   169  * @return array Array of return values, grouped by key. Each value is either
       
   170  *               the cache contents on success, or false on failure.
   141  */
   171  */
   142 function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
   172 function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
   143 	global $wp_object_cache;
   173 	global $wp_object_cache;
   144 
   174 
   145 	return $wp_object_cache->get_multiple( $keys, $group, $force );
   175 	return $wp_object_cache->get_multiple( $keys, $group, $force );
   146 }
   176 }
   147 
   177 
   148 /**
   178 /**
   149  * Increment numeric cache item's value
   179  * Removes the cache contents matching key and group.
       
   180  *
       
   181  * @since 2.0.0
       
   182  *
       
   183  * @see WP_Object_Cache::delete()
       
   184  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
       
   185  *
       
   186  * @param int|string $key   What the contents in the cache are called.
       
   187  * @param string     $group Optional. Where the cache contents are grouped. Default empty.
       
   188  * @return bool True on successful removal, false on failure.
       
   189  */
       
   190 function wp_cache_delete( $key, $group = '' ) {
       
   191 	global $wp_object_cache;
       
   192 
       
   193 	return $wp_object_cache->delete( $key, $group );
       
   194 }
       
   195 
       
   196 /**
       
   197  * Deletes multiple values from the cache in one call.
       
   198  *
       
   199  * @since 6.0.0
       
   200  *
       
   201  * @see WP_Object_Cache::delete_multiple()
       
   202  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
       
   203  *
       
   204  * @param array  $keys  Array of keys under which the cache to deleted.
       
   205  * @param string $group Optional. Where the cache contents are grouped. Default empty.
       
   206  * @return bool[] Array of return values, grouped by key. Each value is either
       
   207  *                true on success, or false if the contents were not deleted.
       
   208  */
       
   209 function wp_cache_delete_multiple( array $keys, $group = '' ) {
       
   210 	global $wp_object_cache;
       
   211 
       
   212 	return $wp_object_cache->delete_multiple( $keys, $group );
       
   213 }
       
   214 
       
   215 /**
       
   216  * Increments numeric cache item's value.
   150  *
   217  *
   151  * @since 3.3.0
   218  * @since 3.3.0
   152  *
   219  *
   153  * @see WP_Object_Cache::incr()
   220  * @see WP_Object_Cache::incr()
   154  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   221  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   155  *
   222  *
   156  * @param int|string $key    The key for the cache contents that should be incremented.
   223  * @param int|string $key    The key for the cache contents that should be incremented.
   157  * @param int        $offset Optional. The amount by which to increment the item's value. Default 1.
   224  * @param int        $offset Optional. The amount by which to increment the item's value.
       
   225  *                           Default 1.
   158  * @param string     $group  Optional. The group the key is in. Default empty.
   226  * @param string     $group  Optional. The group the key is in. Default empty.
   159  * @return int|false The item's new value on success, false on failure.
   227  * @return int|false The item's new value on success, false on failure.
   160  */
   228  */
   161 function wp_cache_incr( $key, $offset = 1, $group = '' ) {
   229 function wp_cache_incr( $key, $offset = 1, $group = '' ) {
   162 	global $wp_object_cache;
   230 	global $wp_object_cache;
   163 
   231 
   164 	return $wp_object_cache->incr( $key, $offset, $group );
   232 	return $wp_object_cache->incr( $key, $offset, $group );
   165 }
   233 }
   166 
   234 
   167 /**
   235 /**
   168  * Sets up Object Cache Global and assigns it.
   236  * Decrements numeric cache item's value.
   169  *
   237  *
   170  * @since 2.0.0
   238  * @since 3.3.0
   171  *
   239  *
   172  * @global WP_Object_Cache $wp_object_cache
   240  * @see WP_Object_Cache::decr()
   173  */
   241  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   174 function wp_cache_init() {
   242  *
   175 	$GLOBALS['wp_object_cache'] = new WP_Object_Cache();
   243  * @param int|string $key    The cache key to decrement.
   176 }
   244  * @param int        $offset Optional. The amount by which to decrement the item's value.
   177 
   245  *                           Default 1.
   178 /**
   246  * @param string     $group  Optional. The group the key is in. Default empty.
   179  * Replaces the contents of the cache with new data.
   247  * @return int|false The item's new value on success, false on failure.
   180  *
   248  */
   181  * @since 2.0.0
   249 function wp_cache_decr( $key, $offset = 1, $group = '' ) {
   182  *
   250 	global $wp_object_cache;
   183  * @see WP_Object_Cache::replace()
   251 
   184  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   252 	return $wp_object_cache->decr( $key, $offset, $group );
   185  *
   253 }
   186  * @param int|string $key    The key for the cache data that should be replaced.
   254 
   187  * @param mixed      $data   The new data to store in the cache.
   255 /**
   188  * @param string     $group  Optional. The group for the cache data that should be replaced.
   256  * Removes all cache items.
   189  *                           Default empty.
   257  *
   190  * @param int        $expire Optional. When to expire the cache contents, in seconds.
   258  * @since 2.0.0
   191  *                           Default 0 (no expiration).
   259  *
   192  * @return bool False if original value does not exist, true if contents were replaced
   260  * @see WP_Object_Cache::flush()
   193  */
   261  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   194 function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
   262  *
   195 	global $wp_object_cache;
       
   196 
       
   197 	return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
       
   198 }
       
   199 
       
   200 /**
       
   201  * Saves the data to the cache.
       
   202  *
       
   203  * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
       
   204  *
       
   205  * @since 2.0.0
       
   206  *
       
   207  * @see WP_Object_Cache::set()
       
   208  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
       
   209  *
       
   210  * @param int|string $key    The cache key to use for retrieval later.
       
   211  * @param mixed      $data   The contents to store in the cache.
       
   212  * @param string     $group  Optional. Where to group the cache contents. Enables the same key
       
   213  *                           to be used across groups. Default empty.
       
   214  * @param int        $expire Optional. When to expire the cache contents, in seconds.
       
   215  *                           Default 0 (no expiration).
       
   216  * @return bool True on success, false on failure.
   263  * @return bool True on success, false on failure.
   217  */
   264  */
   218 function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
   265 function wp_cache_flush() {
   219 	global $wp_object_cache;
   266 	global $wp_object_cache;
   220 
   267 
   221 	return $wp_object_cache->set( $key, $data, $group, (int) $expire );
   268 	return $wp_object_cache->flush();
   222 }
   269 }
   223 
   270 
   224 /**
   271 /**
   225  * Switches the internal blog ID.
   272  * Removes all cache items from the in-memory runtime cache.
   226  *
   273  *
   227  * This changes the blog id used to create keys in blog specific groups.
   274  * @since 6.0.0
   228  *
   275  *
   229  * @since 3.5.0
   276  * @see WP_Object_Cache::flush()
   230  *
   277  *
   231  * @see WP_Object_Cache::switch_to_blog()
   278  * @return bool True on success, false on failure.
   232  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   279  */
   233  *
   280 function wp_cache_flush_runtime() {
   234  * @param int $blog_id Site ID.
   281 	return wp_cache_flush();
   235  */
   282 }
   236 function wp_cache_switch_to_blog( $blog_id ) {
   283 
   237 	global $wp_object_cache;
   284 /**
   238 
   285  * Closes the cache.
   239 	$wp_object_cache->switch_to_blog( $blog_id );
   286  *
       
   287  * This function has ceased to do anything since WordPress 2.5. The
       
   288  * functionality was removed along with the rest of the persistent cache.
       
   289  *
       
   290  * This does not mean that plugins can't implement this function when they need
       
   291  * to make sure that the cache is cleaned up after WordPress no longer needs it.
       
   292  *
       
   293  * @since 2.0.0
       
   294  *
       
   295  * @return true Always returns true.
       
   296  */
       
   297 function wp_cache_close() {
       
   298 	return true;
   240 }
   299 }
   241 
   300 
   242 /**
   301 /**
   243  * Adds a group or set of groups to the list of global groups.
   302  * Adds a group or set of groups to the list of global groups.
   244  *
   303  *
   265 function wp_cache_add_non_persistent_groups( $groups ) {
   324 function wp_cache_add_non_persistent_groups( $groups ) {
   266 	// Default cache doesn't persist so nothing to do here.
   325 	// Default cache doesn't persist so nothing to do here.
   267 }
   326 }
   268 
   327 
   269 /**
   328 /**
   270  * Reset internal cache keys and structures.
   329  * Switches the internal blog ID.
       
   330  *
       
   331  * This changes the blog id used to create keys in blog specific groups.
       
   332  *
       
   333  * @since 3.5.0
       
   334  *
       
   335  * @see WP_Object_Cache::switch_to_blog()
       
   336  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
       
   337  *
       
   338  * @param int $blog_id Site ID.
       
   339  */
       
   340 function wp_cache_switch_to_blog( $blog_id ) {
       
   341 	global $wp_object_cache;
       
   342 
       
   343 	$wp_object_cache->switch_to_blog( $blog_id );
       
   344 }
       
   345 
       
   346 /**
       
   347  * Resets internal cache keys and structures.
   271  *
   348  *
   272  * If the cache back end uses global blog or site IDs as part of its cache keys,
   349  * If the cache back end uses global blog or site IDs as part of its cache keys,
   273  * this function instructs the back end to reset those keys and perform any cleanup
   350  * this function instructs the back end to reset those keys and perform any cleanup
   274  * since blog or site IDs have changed since cache init.
   351  * since blog or site IDs have changed since cache init.
   275  *
   352  *
   276  * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
   353  * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
   277  * function when preparing the cache for a blog switch. For clearing the cache
   354  * function when preparing the cache for a blog switch. For clearing the cache
   278  * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
   355  * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
   279  * recommended outside of unit tests as the performance penalty for using it is
   356  * recommended outside of unit tests as the performance penalty for using it is high.
   280  * high.
   357  *
   281  *
   358  * @since 3.0.0
   282  * @since 2.6.0
   359  * @deprecated 3.5.0 Use wp_cache_switch_to_blog()
   283  * @deprecated 3.5.0 WP_Object_Cache::reset()
       
   284  * @see WP_Object_Cache::reset()
   360  * @see WP_Object_Cache::reset()
   285  *
   361  *
   286  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   362  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
   287  */
   363  */
   288 function wp_cache_reset() {
   364 function wp_cache_reset() {
   289 	_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()' );
   365 	_deprecated_function( __FUNCTION__, '3.5.0', 'wp_cache_switch_to_blog()' );
   290 
   366 
   291 	global $wp_object_cache;
   367 	global $wp_object_cache;
   292 
   368 
   293 	$wp_object_cache->reset();
   369 	$wp_object_cache->reset();
   294 }
   370 }