[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> cache.php (source)

   1  <?php
   2  /**
   3   * Object Cache API
   4   *
   5   * @link https://developer.wordpress.org/reference/classes/wp_object_cache/
   6   *
   7   * @package WordPress
   8   * @subpackage Cache
   9   */
  10  
  11  /** WP_Object_Cache class */
  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  }
  24  
  25  /**
  26   * Adds data to the cache, if the cache key doesn't already exist.
  27   *
  28   * @since 2.0.0
  29   *
  30   * @see WP_Object_Cache::add()
  31   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  32   *
  33   * @param int|string $key    The cache key to use for retrieval later.
  34   * @param mixed      $data   The data to add to the cache.
  35   * @param string     $group  Optional. The group to add the cache to. Enables the same key
  36   *                           to be used across groups. Default empty.
  37   * @param int        $expire Optional. When the cache data should expire, in seconds.
  38   *                           Default 0 (no expiration).
  39   * @return bool True on success, false if cache key and group already exist.
  40   */
  41  function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
  42      global $wp_object_cache;
  43  
  44      return $wp_object_cache->add( $key, $data, $group, (int) $expire );
  45  }
  46  
  47  /**
  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 bool[] Array of return values, grouped by key. Each value is either
  60   *                true on success, or false if cache key and group already exist.
  61   */
  62  function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) {
  63      global $wp_object_cache;
  64  
  65      return $wp_object_cache->add_multiple( $data, $group, $expire );
  66  }
  67  
  68  /**
  69   * Replaces the contents of the cache with new data.
  70   *
  71   * @since 2.0.0
  72   *
  73   * @see WP_Object_Cache::replace()
  74   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  75   *
  76   * @param int|string $key    The key for the cache data that should be replaced.
  77   * @param mixed      $data   The new data to store in the cache.
  78   * @param string     $group  Optional. The group for the cache data that should be replaced.
  79   *                           Default empty.
  80   * @param int        $expire Optional. When to expire the cache contents, in seconds.
  81   *                           Default 0 (no expiration).
  82   * @return bool True if contents were replaced, false if original value does not exist.
  83   */
  84  function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
  85      global $wp_object_cache;
  86  
  87      return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
  88  }
  89  
  90  /**
  91   * Saves the data to the cache.
  92   *
  93   * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
  94   *
  95   * @since 2.0.0
  96   *
  97   * @see WP_Object_Cache::set()
  98   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  99   *
 100   * @param int|string $key    The cache key to use for retrieval later.
 101   * @param mixed      $data   The contents to store in the cache.
 102   * @param string     $group  Optional. Where to group the cache contents. Enables the same key
 103   *                           to be used across groups. Default empty.
 104   * @param int        $expire Optional. When to expire the cache contents, in seconds.
 105   *                           Default 0 (no expiration).
 106   * @return bool True on success, false on failure.
 107   */
 108  function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
 109      global $wp_object_cache;
 110  
 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 );
 133  }
 134  
 135  /**
 136   * Retrieves the cache contents from the cache by key and group.
 137   *
 138   * @since 2.0.0
 139   *
 140   * @see WP_Object_Cache::get()
 141   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 142   *
 143   * @param int|string $key   The key under which the cache contents are stored.
 144   * @param string     $group Optional. Where the cache contents are grouped. Default empty.
 145   * @param bool       $force Optional. Whether to force an update of the local cache
 146   *                          from the persistent cache. Default false.
 147   * @param bool       $found Optional. Whether the key was found in the cache (passed by reference).
 148   *                          Disambiguates a return of false, a storable value. Default null.
 149   * @return mixed|false The cache contents on success, false on failure to retrieve contents.
 150   */
 151  function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
 152      global $wp_object_cache;
 153  
 154      return $wp_object_cache->get( $key, $group, $force, $found );
 155  }
 156  
 157  /**
 158   * Retrieves multiple values from the cache in one call.
 159   *
 160   * @since 5.5.0
 161   *
 162   * @see WP_Object_Cache::get_multiple()
 163   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 164   *
 165   * @param array  $keys  Array of keys under which the cache contents are stored.
 166   * @param string $group Optional. Where the cache contents are grouped. Default empty.
 167   * @param bool   $force Optional. Whether to force an update of the local cache
 168   *                      from the persistent cache. Default false.
 169   * @return array Array of return values, grouped by key. Each value is either
 170   *               the cache contents on success, or false on failure.
 171   */
 172  function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
 173      global $wp_object_cache;
 174  
 175      return $wp_object_cache->get_multiple( $keys, $group, $force );
 176  }
 177  
 178  /**
 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.
 217   *
 218   * @since 3.3.0
 219   *
 220   * @see WP_Object_Cache::incr()
 221   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 222   *
 223   * @param int|string $key    The key for the cache contents that should be incremented.
 224   * @param int        $offset Optional. The amount by which to increment the item's value.
 225   *                           Default 1.
 226   * @param string     $group  Optional. The group the key is in. Default empty.
 227   * @return int|false The item's new value on success, false on failure.
 228   */
 229  function wp_cache_incr( $key, $offset = 1, $group = '' ) {
 230      global $wp_object_cache;
 231  
 232      return $wp_object_cache->incr( $key, $offset, $group );
 233  }
 234  
 235  /**
 236   * Decrements numeric cache item's value.
 237   *
 238   * @since 3.3.0
 239   *
 240   * @see WP_Object_Cache::decr()
 241   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 242   *
 243   * @param int|string $key    The cache key to decrement.
 244   * @param int        $offset Optional. The amount by which to decrement the item's value.
 245   *                           Default 1.
 246   * @param string     $group  Optional. The group the key is in. Default empty.
 247   * @return int|false The item's new value on success, false on failure.
 248   */
 249  function wp_cache_decr( $key, $offset = 1, $group = '' ) {
 250      global $wp_object_cache;
 251  
 252      return $wp_object_cache->decr( $key, $offset, $group );
 253  }
 254  
 255  /**
 256   * Removes all cache items.
 257   *
 258   * @since 2.0.0
 259   *
 260   * @see WP_Object_Cache::flush()
 261   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 262   *
 263   * @return bool True on success, false on failure.
 264   */
 265  function wp_cache_flush() {
 266      global $wp_object_cache;
 267  
 268      return $wp_object_cache->flush();
 269  }
 270  
 271  /**
 272   * Removes all cache items from the in-memory runtime cache.
 273   *
 274   * @since 6.0.0
 275   *
 276   * @see WP_Object_Cache::flush()
 277   *
 278   * @return bool True on success, false on failure.
 279   */
 280  function wp_cache_flush_runtime() {
 281      return wp_cache_flush();
 282  }
 283  
 284  /**
 285   * Closes the cache.
 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;
 299  }
 300  
 301  /**
 302   * Adds a group or set of groups to the list of global groups.
 303   *
 304   * @since 2.6.0
 305   *
 306   * @see WP_Object_Cache::add_global_groups()
 307   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 308   *
 309   * @param string|string[] $groups A group or an array of groups to add.
 310   */
 311  function wp_cache_add_global_groups( $groups ) {
 312      global $wp_object_cache;
 313  
 314      $wp_object_cache->add_global_groups( $groups );
 315  }
 316  
 317  /**
 318   * Adds a group or set of groups to the list of non-persistent groups.
 319   *
 320   * @since 2.6.0
 321   *
 322   * @param string|string[] $groups A group or an array of groups to add.
 323   */
 324  function wp_cache_add_non_persistent_groups( $groups ) {
 325      // Default cache doesn't persist so nothing to do here.
 326  }
 327  
 328  /**
 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.
 348   *
 349   * If the cache back end uses global blog or site IDs as part of its cache keys,
 350   * this function instructs the back end to reset those keys and perform any cleanup
 351   * since blog or site IDs have changed since cache init.
 352   *
 353   * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
 354   * function when preparing the cache for a blog switch. For clearing the cache
 355   * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
 356   * recommended outside of unit tests as the performance penalty for using it is high.
 357   *
 358   * @since 3.0.0
 359   * @deprecated 3.5.0 Use wp_cache_switch_to_blog()
 360   * @see WP_Object_Cache::reset()
 361   *
 362   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 363   */
 364  function wp_cache_reset() {
 365      _deprecated_function( __FUNCTION__, '3.5.0', 'wp_cache_switch_to_blog()' );
 366  
 367      global $wp_object_cache;
 368  
 369      $wp_object_cache->reset();
 370  }


Generated: Sun Aug 14 01:00:02 2022 Cross-referenced by PHPXref 0.7.1