[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Object Cache API
   4   *
   5   * @link http://codex.wordpress.org/Function_Reference/WP_Cache
   6   *
   7   * @package WordPress
   8   * @subpackage Cache
   9   */
  10  
  11  /**
  12   * Adds data to the cache, if the cache key doesn't already exist.
  13   *
  14   * @since 2.0.0
  15   * @uses $wp_object_cache Object Cache Class
  16   * @see WP_Object_Cache::add()
  17   *
  18   * @param int|string $key The cache key to use for retrieval later
  19   * @param mixed $data The data to add to the cache store
  20   * @param string $group The group to add the cache to
  21   * @param int $expire When the cache data should be expired
  22   * @return unknown
  23   */
  24  function wp_cache_add($key, $data, $group = '', $expire = 0) {
  25      global $wp_object_cache;
  26  
  27      return $wp_object_cache->add($key, $data, $group, $expire);
  28  }
  29  
  30  /**
  31   * Closes the cache.
  32   *
  33   * This function has ceased to do anything since WordPress 2.5. The
  34   * functionality was removed along with the rest of the persistent cache. This
  35   * does not mean that plugins can't implement this function when they need to
  36   * make sure that the cache is cleaned up after WordPress no longer needs it.
  37   *
  38   * @since 2.0.0
  39   *
  40   * @return bool Always returns True
  41   */
  42  function wp_cache_close() {
  43      return true;
  44  }
  45  
  46  /**
  47   * Decrement numeric cache item's value
  48   *
  49   * @since 3.3.0
  50   * @uses $wp_object_cache Object Cache Class
  51   * @see WP_Object_Cache::decr()
  52   *
  53   * @param int|string $key The cache key to increment
  54   * @param int $offset The amount by which to decrement the item's value. Default is 1.
  55   * @param string $group The group the key is in.
  56   * @return false|int False on failure, the item's new value on success.
  57   */
  58  function wp_cache_decr( $key, $offset = 1, $group = '' ) {
  59      global $wp_object_cache;
  60  
  61      return $wp_object_cache->decr( $key, $offset, $group );
  62  }
  63  
  64  /**
  65   * Removes the cache contents matching key and group.
  66   *
  67   * @since 2.0.0
  68   * @uses $wp_object_cache Object Cache Class
  69   * @see WP_Object_Cache::delete()
  70   *
  71   * @param int|string $key What the contents in the cache are called
  72   * @param string $group Where the cache contents are grouped
  73   * @return bool True on successful removal, false on failure
  74   */
  75  function wp_cache_delete($key, $group = '') {
  76      global $wp_object_cache;
  77  
  78      return $wp_object_cache->delete($key, $group);
  79  }
  80  
  81  /**
  82   * Removes all cache items.
  83   *
  84   * @since 2.0.0
  85   * @uses $wp_object_cache Object Cache Class
  86   * @see WP_Object_Cache::flush()
  87   *
  88   * @return bool Always returns true
  89   */
  90  function wp_cache_flush() {
  91      global $wp_object_cache;
  92  
  93      return $wp_object_cache->flush();
  94  }
  95  
  96  /**
  97   * Retrieves the cache contents from the cache by key and group.
  98   *
  99   * @since 2.0.0
 100   * @uses $wp_object_cache Object Cache Class
 101   * @see WP_Object_Cache::get()
 102   *
 103   * @param int|string $key What the contents in the cache are called
 104   * @param string $group Where the cache contents are grouped
 105   * @param bool $force Whether to force an update of the local cache from the persistent cache (default is false)
 106   * @param &bool $found Whether key was found in the cache. Disambiguates a return of false, a storable value.
 107   * @return bool|mixed False on failure to retrieve contents or the cache
 108   *        contents on success
 109   */
 110  function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
 111      global $wp_object_cache;
 112  
 113      return $wp_object_cache->get( $key, $group, $force, $found );
 114  }
 115  
 116  /**
 117   * Increment numeric cache item's value
 118   *
 119   * @since 3.3.0
 120   * @uses $wp_object_cache Object Cache Class
 121   * @see WP_Object_Cache::incr()
 122   *
 123   * @param int|string $key The cache key to increment
 124   * @param int $offset The amount by which to increment the item's value. Default is 1.
 125   * @param string $group The group the key is in.
 126   * @return false|int False on failure, the item's new value on success.
 127   */
 128  function wp_cache_incr( $key, $offset = 1, $group = '' ) {
 129      global $wp_object_cache;
 130  
 131      return $wp_object_cache->incr( $key, $offset, $group );
 132  }
 133  
 134  /**
 135   * Sets up Object Cache Global and assigns it.
 136   *
 137   * @since 2.0.0
 138   * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
 139   */
 140  function wp_cache_init() {
 141      $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
 142  }
 143  
 144  /**
 145   * Replaces the contents of the cache with new data.
 146   *
 147   * @since 2.0.0
 148   * @uses $wp_object_cache Object Cache Class
 149   * @see WP_Object_Cache::replace()
 150   *
 151   * @param int|string $key What to call the contents in the cache
 152   * @param mixed $data The contents to store in the cache
 153   * @param string $group Where to group the cache contents
 154   * @param int $expire When to expire the cache contents
 155   * @return bool False if cache key and group already exist, true on success
 156   */
 157  function wp_cache_replace($key, $data, $group = '', $expire = 0) {
 158      global $wp_object_cache;
 159  
 160      return $wp_object_cache->replace($key, $data, $group, $expire);
 161  }
 162  
 163  /**
 164   * Saves the data to the cache.
 165   *
 166   * @since 2.0
 167   * @uses $wp_object_cache Object Cache Class
 168   * @see WP_Object_Cache::set()
 169   *
 170   * @param int|string $key What to call the contents in the cache
 171   * @param mixed $data The contents to store in the cache
 172   * @param string $group Where to group the cache contents
 173   * @param int $expire When to expire the cache contents
 174   * @return bool False if cache key and group already exist, true on success
 175   */
 176  function wp_cache_set($key, $data, $group = '', $expire = 0) {
 177      global $wp_object_cache;
 178  
 179      return $wp_object_cache->set($key, $data, $group, $expire);
 180  }
 181  
 182  /**
 183   * Adds a group or set of groups to the list of global groups.
 184   *
 185   * @since 2.6.0
 186   *
 187   * @param string|array $groups A group or an array of groups to add
 188   */
 189  function wp_cache_add_global_groups( $groups ) {
 190      global $wp_object_cache;
 191  
 192      return $wp_object_cache->add_global_groups($groups);
 193  }
 194  
 195  /**
 196   * Adds a group or set of groups to the list of non-persistent groups.
 197   *
 198   * @since 2.6.0
 199   *
 200   * @param string|array $groups A group or an array of groups to add
 201   */
 202  function wp_cache_add_non_persistent_groups( $groups ) {
 203      // Default cache doesn't persist so nothing to do here.
 204      return;
 205  }
 206  
 207  /**
 208   * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys,
 209   * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
 210   *
 211   * @since 2.6.0
 212   */
 213  function wp_cache_reset() {
 214      global $wp_object_cache;
 215  
 216      return $wp_object_cache->reset();
 217  }
 218  
 219  /**
 220   * WordPress Object Cache
 221   *
 222   * The WordPress Object Cache is used to save on trips to the database. The
 223   * Object Cache stores all of the cache data to memory and makes the cache
 224   * contents available by using a key, which is used to name and later retrieve
 225   * the cache contents.
 226   *
 227   * The Object Cache can be replaced by other caching mechanisms by placing files
 228   * in the wp-content folder which is looked at in wp-settings. If that file
 229   * exists, then this file will not be included.
 230   *
 231   * @package WordPress
 232   * @subpackage Cache
 233   * @since 2.0
 234   */
 235  class WP_Object_Cache {
 236  
 237      /**
 238       * Holds the cached objects
 239       *
 240       * @var array
 241       * @access private
 242       * @since 2.0.0
 243       */
 244      var $cache = array ();
 245  
 246      /**
 247       * The amount of times the cache data was already stored in the cache.
 248       *
 249       * @since 2.5.0
 250       * @access private
 251       * @var int
 252       */
 253      var $cache_hits = 0;
 254  
 255      /**
 256       * Amount of times the cache did not have the request in cache
 257       *
 258       * @var int
 259       * @access public
 260       * @since 2.0.0
 261       */
 262      var $cache_misses = 0;
 263  
 264      /**
 265       * List of global groups
 266       *
 267       * @var array
 268       * @access protected
 269       * @since 3.0.0
 270       */
 271      var $global_groups = array();
 272  
 273      /**
 274       * Adds data to the cache if it doesn't already exist.
 275       *
 276       * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
 277       * @uses WP_Object_Cache::set Sets the data after the checking the cache
 278       *        contents existence.
 279       *
 280       * @since 2.0.0
 281       *
 282       * @param int|string $key What to call the contents in the cache
 283       * @param mixed $data The contents to store in the cache
 284       * @param string $group Where to group the cache contents
 285       * @param int $expire When to expire the cache contents
 286       * @return bool False if cache key and group already exist, true on success
 287       */
 288  	function add( $key, $data, $group = 'default', $expire = '' ) {
 289          if ( wp_suspend_cache_addition() )
 290              return false;
 291  
 292          if ( empty( $group ) )
 293              $group = 'default';
 294  
 295          if ( $this->_exists($key, $group) )
 296              return false;
 297  
 298          return $this->set($key, $data, $group, $expire);
 299      }
 300  
 301      /**
 302       * Sets the list of global groups.
 303       *
 304       * @since 3.0.0
 305       *
 306       * @param array $groups List of groups that are global.
 307       */
 308  	function add_global_groups( $groups ) {
 309          $groups = (array) $groups;
 310  
 311          $this->global_groups = array_merge($this->global_groups, $groups);
 312          $this->global_groups = array_unique($this->global_groups);
 313      }
 314  
 315      /**
 316       * Decrement numeric cache item's value
 317       *
 318       * @since 3.3.0
 319       *
 320       * @param int|string $key The cache key to increment
 321       * @param int $offset The amount by which to decrement the item's value. Default is 1.
 322       * @param string $group The group the key is in.
 323       * @return false|int False on failure, the item's new value on success.
 324       */
 325  	function decr( $key, $offset = 1, $group = 'default' ) {
 326          if ( ! $this->_exists( $key, $group ) )
 327              return false;
 328  
 329          if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
 330              $this->cache[ $group ][ $key ] = 0;
 331  
 332          $offset = (int) $offset;
 333  
 334          $this->cache[ $group ][ $key ] -= $offset;
 335  
 336          if ( $this->cache[ $group ][ $key ] < 0 )
 337              $this->cache[ $group ][ $key ] = 0;
 338  
 339          return $this->cache[ $group ][ $key ];
 340      }
 341  
 342      /**
 343       * Remove the contents of the cache key in the group
 344       *
 345       * If the cache key does not exist in the group and $force parameter is set
 346       * to false, then nothing will happen. The $force parameter is set to false
 347       * by default.
 348       *
 349       * @since 2.0.0
 350       *
 351       * @param int|string $key What the contents in the cache are called
 352       * @param string $group Where the cache contents are grouped
 353       * @param bool $force Optional. Whether to force the unsetting of the cache
 354       *        key in the group
 355       * @return bool False if the contents weren't deleted and true on success
 356       */
 357  	function delete($key, $group = 'default', $force = false) {
 358          if ( empty( $group ) )
 359              $group = 'default';
 360  
 361          if ( ! $force && ! $this->_exists( $key, $group ) )
 362              return false;
 363  
 364          unset( $this->cache[$group][$key] );
 365          return true;
 366      }
 367  
 368      /**
 369       * Clears the object cache of all data
 370       *
 371       * @since 2.0.0
 372       *
 373       * @return bool Always returns true
 374       */
 375  	function flush() {
 376          $this->cache = array ();
 377  
 378          return true;
 379      }
 380  
 381      /**
 382       * Retrieves the cache contents, if it exists
 383       *
 384       * The contents will be first attempted to be retrieved by searching by the
 385       * key in the cache group. If the cache is hit (success) then the contents
 386       * are returned.
 387       *
 388       * On failure, the number of cache misses will be incremented.
 389       *
 390       * @since 2.0.0
 391       *
 392       * @param int|string $key What the contents in the cache are called
 393       * @param string $group Where the cache contents are grouped
 394       * @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
 395       * @return bool|mixed False on failure to retrieve contents or the cache
 396       *        contents on success
 397       */
 398  	function get( $key, $group = 'default', $force = false, &$found = null ) {
 399          if ( empty( $group ) )
 400              $group = 'default';
 401  
 402          if ( $this->_exists( $key, $group ) ) {
 403              $found = true;
 404              $this->cache_hits += 1;
 405              if ( is_object($this->cache[$group][$key]) )
 406                  return clone $this->cache[$group][$key];
 407              else
 408                  return $this->cache[$group][$key];
 409          }
 410  
 411          $found = false;
 412          $this->cache_misses += 1;
 413          return false;
 414      }
 415  
 416      /**
 417       * Increment numeric cache item's value
 418       *
 419       * @since 3.3.0
 420       *
 421       * @param int|string $key The cache key to increment
 422       * @param int $offset The amount by which to increment the item's value. Default is 1.
 423       * @param string $group The group the key is in.
 424       * @return false|int False on failure, the item's new value on success.
 425       */
 426  	function incr( $key, $offset = 1, $group = 'default' ) {
 427          if ( empty( $group ) )
 428              $group = 'default';
 429  
 430          if ( ! $this->_exists( $key, $group ) )
 431              return false;
 432  
 433          if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
 434              $this->cache[ $group ][ $key ] = 0;
 435  
 436          $offset = (int) $offset;
 437  
 438          $this->cache[ $group ][ $key ] += $offset;
 439  
 440          if ( $this->cache[ $group ][ $key ] < 0 )
 441              $this->cache[ $group ][ $key ] = 0;
 442  
 443          return $this->cache[ $group ][ $key ];
 444      }
 445  
 446      /**
 447       * Replace the contents in the cache, if contents already exist
 448       *
 449       * @since 2.0.0
 450       * @see WP_Object_Cache::set()
 451       *
 452       * @param int|string $key What to call the contents in the cache
 453       * @param mixed $data The contents to store in the cache
 454       * @param string $group Where to group the cache contents
 455       * @param int $expire When to expire the cache contents
 456       * @return bool False if not exists, true if contents were replaced
 457       */
 458  	function replace($key, $data, $group = 'default', $expire = '') {
 459          if ( empty( $group ) )
 460              $group = 'default';
 461  
 462          if ( ! $this->_exists( $key, $group ) )
 463              return false;
 464  
 465          return $this->set($key, $data, $group, $expire);
 466      }
 467  
 468      /**
 469       * Reset keys
 470       *
 471       * @since 3.0.0
 472       */
 473  	function reset() {
 474          // Clear out non-global caches since the blog ID has changed.
 475          foreach ( array_keys($this->cache) as $group ) {
 476              if ( !in_array($group, $this->global_groups) )
 477                  unset($this->cache[$group]);
 478          }
 479      }
 480  
 481      /**
 482       * Sets the data contents into the cache
 483       *
 484       * The cache contents is grouped by the $group parameter followed by the
 485       * $key. This allows for duplicate ids in unique groups. Therefore, naming of
 486       * the group should be used with care and should follow normal function
 487       * naming guidelines outside of core WordPress usage.
 488       *
 489       * The $expire parameter is not used, because the cache will automatically
 490       * expire for each time a page is accessed and PHP finishes. The method is
 491       * more for cache plugins which use files.
 492       *
 493       * @since 2.0.0
 494       *
 495       * @param int|string $key What to call the contents in the cache
 496       * @param mixed $data The contents to store in the cache
 497       * @param string $group Where to group the cache contents
 498       * @param int $expire Not Used
 499       * @return bool Always returns true
 500       */
 501  	function set($key, $data, $group = 'default', $expire = '') {
 502          if ( empty( $group ) )
 503              $group = 'default';
 504  
 505          if ( is_object($data) )
 506              $data = clone $data;
 507  
 508          $this->cache[$group][$key] = $data;
 509          return true;
 510      }
 511  
 512      /**
 513       * Echoes the stats of the caching.
 514       *
 515       * Gives the cache hits, and cache misses. Also prints every cached group,
 516       * key and the data.
 517       *
 518       * @since 2.0.0
 519       */
 520  	function stats() {
 521          echo "<p>";
 522          echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
 523          echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
 524          echo "</p>";
 525          echo '<ul>';
 526          foreach ($this->cache as $group => $cache) {
 527              echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
 528          }
 529          echo '</ul>';
 530      }
 531  
 532      /**
 533       * Utility function to determine whether a key exists in the cache.
 534       * @access private
 535       */
 536  	protected function _exists($key, $group) {
 537          return isset( $this->cache[$group] ) && is_array( $this->cache[$group] ) && array_key_exists( $key, $this->cache[$group] );
 538      }
 539  
 540      /**
 541       * Sets up object properties; PHP 5 style constructor
 542       *
 543       * @since 2.0.8
 544       * @return null|WP_Object_Cache If cache is disabled, returns null.
 545       */
 546  	function __construct() {
 547          /**
 548           * @todo This should be moved to the PHP4 style constructor, PHP5
 549           * already calls __destruct()
 550           */
 551          register_shutdown_function(array(&$this, "__destruct"));
 552      }
 553  
 554      /**
 555       * Will save the object cache before object is completely destroyed.
 556       *
 557       * Called upon object destruction, which should be when PHP ends.
 558       *
 559       * @since  2.0.8
 560       *
 561       * @return bool True value. Won't be used by PHP
 562       */
 563  	function __destruct() {
 564          return true;
 565      }
 566  }


Generated: Fri May 25 03:56:23 2012 Hosted by follow the white rabbit.