[ 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://codex.wordpress.org/Class_Reference/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   * Adds data to the cache, if the cache key doesn't already exist.
  16   *
  17   * @since 2.0.0
  18   *
  19   * @see WP_Object_Cache::add()
  20   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  21   *
  22   * @param int|string $key    The cache key to use for retrieval later.
  23   * @param mixed      $data   The data to add to the cache.
  24   * @param string     $group  Optional. The group to add the cache to. Enables the same key
  25   *                           to be used across groups. Default empty.
  26   * @param int        $expire Optional. When the cache data should expire, in seconds.
  27   *                           Default 0 (no expiration).
  28   * @return bool True on success, false if cache key and group already exist.
  29   */
  30  function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
  31      global $wp_object_cache;
  32  
  33      return $wp_object_cache->add( $key, $data, $group, (int) $expire );
  34  }
  35  
  36  /**
  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. Default 1.
  63   * @param string     $group  Optional. The group the key is in. Default empty.
  64   * @return int|false The item's new value on success, false on failure.
  65   */
  66  function wp_cache_decr( $key, $offset = 1, $group = '' ) {
  67      global $wp_object_cache;
  68  
  69      return $wp_object_cache->decr( $key, $offset, $group );
  70  }
  71  
  72  /**
  73   * Removes the cache contents matching key and group.
  74   *
  75   * @since 2.0.0
  76   *
  77   * @see WP_Object_Cache::delete()
  78   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  79   *
  80   * @param int|string $key   What the contents in the cache are called.
  81   * @param string     $group Optional. Where the cache contents are grouped. Default empty.
  82   * @return bool True on successful removal, false on failure.
  83   */
  84  function wp_cache_delete( $key, $group = '' ) {
  85      global $wp_object_cache;
  86  
  87      return $wp_object_cache->delete( $key, $group );
  88  }
  89  
  90  /**
  91   * Removes all cache items.
  92   *
  93   * @since 2.0.0
  94   *
  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.
  99   */
 100  function wp_cache_flush() {
 101      global $wp_object_cache;
 102  
 103      return $wp_object_cache->flush();
 104  }
 105  
 106  /**
 107   * Retrieves the cache contents from the cache by key and group.
 108   *
 109   * @since 2.0.0
 110   *
 111   * @see WP_Object_Cache::get()
 112   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 113   *
 114   * @param int|string  $key    The key under which the cache contents are stored.
 115   * @param string      $group  Optional. Where the cache contents are grouped. Default empty.
 116   * @param bool        $force  Optional. Whether to force an update of the local cache from the persistent
 117   *                            cache. Default false.
 118   * @param bool        $found  Optional. Whether the key was found in the cache (passed by reference).
 119   *                            Disambiguates a return of false, a storable value. Default null.
 120   * @return bool|mixed False on failure to retrieve contents or the cache
 121   *                    contents on success
 122   */
 123  function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
 124      global $wp_object_cache;
 125  
 126      return $wp_object_cache->get( $key, $group, $force, $found );
 127  }
 128  
 129  /**
 130   * Increment numeric cache item's value
 131   *
 132   * @since 3.3.0
 133   *
 134   * @see WP_Object_Cache::incr()
 135   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 136   *
 137   * @param int|string $key    The key for the cache contents that should be incremented.
 138   * @param int        $offset Optional. The amount by which to increment the item's value. Default 1.
 139   * @param string     $group  Optional. The group the key is in. Default empty.
 140   * @return int|false The item's new value on success, false on failure.
 141   */
 142  function wp_cache_incr( $key, $offset = 1, $group = '' ) {
 143      global $wp_object_cache;
 144  
 145      return $wp_object_cache->incr( $key, $offset, $group );
 146  }
 147  
 148  /**
 149   * Sets up Object Cache Global and assigns it.
 150   *
 151   * @since 2.0.0
 152   *
 153   * @global WP_Object_Cache $wp_object_cache
 154   */
 155  function wp_cache_init() {
 156      $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
 157  }
 158  
 159  /**
 160   * Replaces the contents of the cache with new data.
 161   *
 162   * @since 2.0.0
 163   *
 164   * @see WP_Object_Cache::replace()
 165   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 166   *
 167   * @param int|string $key    The key for the cache data that should be replaced.
 168   * @param mixed      $data   The new data to store in the cache.
 169   * @param string     $group  Optional. The group for the cache data that should be replaced.
 170   *                           Default empty.
 171   * @param int        $expire Optional. When to expire the cache contents, in seconds.
 172   *                           Default 0 (no expiration).
 173   * @return bool False if original value does not exist, true if contents were replaced
 174   */
 175  function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
 176      global $wp_object_cache;
 177  
 178      return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
 179  }
 180  
 181  /**
 182   * Saves the data to the cache.
 183   *
 184   * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
 185   *
 186   * @since 2.0.0
 187   *
 188   * @see WP_Object_Cache::set()
 189   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 190   *
 191   * @param int|string $key    The cache key to use for retrieval later.
 192   * @param mixed      $data   The contents to store in the cache.
 193   * @param string     $group  Optional. Where to group the cache contents. Enables the same key
 194   *                           to be used across groups. Default empty.
 195   * @param int        $expire Optional. When to expire the cache contents, in seconds.
 196   *                           Default 0 (no expiration).
 197   * @return bool True on success, false on failure.
 198   */
 199  function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
 200      global $wp_object_cache;
 201  
 202      return $wp_object_cache->set( $key, $data, $group, (int) $expire );
 203  }
 204  
 205  /**
 206   * Switches the internal blog ID.
 207   *
 208   * This changes the blog id used to create keys in blog specific groups.
 209   *
 210   * @since 3.5.0
 211   *
 212   * @see WP_Object_Cache::switch_to_blog()
 213   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 214   *
 215   * @param int $blog_id Site ID.
 216   */
 217  function wp_cache_switch_to_blog( $blog_id ) {
 218      global $wp_object_cache;
 219  
 220      $wp_object_cache->switch_to_blog( $blog_id );
 221  }
 222  
 223  /**
 224   * Adds a group or set of groups to the list of global groups.
 225   *
 226   * @since 2.6.0
 227   *
 228   * @see WP_Object_Cache::add_global_groups()
 229   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 230   *
 231   * @param string|array $groups A group or an array of groups to add.
 232   */
 233  function wp_cache_add_global_groups( $groups ) {
 234      global $wp_object_cache;
 235  
 236      $wp_object_cache->add_global_groups( $groups );
 237  }
 238  
 239  /**
 240   * Adds a group or set of groups to the list of non-persistent groups.
 241   *
 242   * @since 2.6.0
 243   *
 244   * @param string|array $groups A group or an array of groups to add.
 245   */
 246  function wp_cache_add_non_persistent_groups( $groups ) {
 247      // Default cache doesn't persist so nothing to do here.
 248  }
 249  
 250  /**
 251   * Reset internal cache keys and structures.
 252   *
 253   * If the cache back end uses global blog or site IDs as part of its cache keys,
 254   * this function instructs the back end to reset those keys and perform any cleanup
 255   * since blog or site IDs have changed since cache init.
 256   *
 257   * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
 258   * function when preparing the cache for a blog switch. For clearing the cache
 259   * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
 260   * recommended outside of unit tests as the performance penalty for using it is
 261   * high.
 262   *
 263   * @since 2.6.0
 264   * @deprecated 3.5.0 WP_Object_Cache::reset()
 265   * @see WP_Object_Cache::reset()
 266   *
 267   * @global WP_Object_Cache $wp_object_cache Object cache global instance.
 268   */
 269  function wp_cache_reset() {
 270      _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()' );
 271  
 272      global $wp_object_cache;
 273  
 274      $wp_object_cache->reset();
 275  }


Generated: Wed Feb 26 01:00:04 2020 Cross-referenced by PHPXref 0.7.1