-
+ 5195BA056B3AE2FD553155AFC159F375E5D3498629471D4428E01C9BCDB8C705BAA3CD6F4A1261EB5EAA96D8AF5AE03D49412182AC06AD33C602FA50792FFC2A
mp-wp/wp-includes/cache.php
(0 . 0)(1 . 452)
72387 <?php
72388 /**
72389 * Object Cache API
72390 *
72391 * @link http://codex.wordpress.org/Function_Reference/WP_Cache
72392 *
72393 * @package WordPress
72394 * @subpackage Cache
72395 */
72396
72397 /**
72398 * Adds data to the cache, if the cache key doesn't aleady exist.
72399 *
72400 * @since 2.0.0
72401 * @uses $wp_object_cache Object Cache Class
72402 * @see WP_Object_Cache::add()
72403 *
72404 * @param int|string $key The cache ID to use for retrieval later
72405 * @param mixed $data The data to add to the cache store
72406 * @param string $flag The group to add the cache to
72407 * @param int $expire When the cache data should be expired
72408 * @return unknown
72409 */
72410 function wp_cache_add($key, $data, $flag = '', $expire = 0) {
72411 global $wp_object_cache;
72412
72413 return $wp_object_cache->add($key, $data, $flag, $expire);
72414 }
72415
72416 /**
72417 * Closes the cache.
72418 *
72419 * This function has ceased to do anything since WordPress 2.5. The
72420 * functionality was removed along with the rest of the persistant cache. This
72421 * does not mean that plugins can't implement this function when they need to
72422 * make sure that the cache is cleaned up after WordPress no longer needs it.
72423 *
72424 * @since 2.0.0
72425 *
72426 * @return bool Always returns True
72427 */
72428 function wp_cache_close() {
72429 return true;
72430 }
72431
72432 /**
72433 * Removes the cache contents matching ID and flag.
72434 *
72435 * @since 2.0.0
72436 * @uses $wp_object_cache Object Cache Class
72437 * @see WP_Object_Cache::delete()
72438 *
72439 * @param int|string $id What the contents in the cache are called
72440 * @param string $flag Where the cache contents are grouped
72441 * @return bool True on successful removal, false on failure
72442 */
72443 function wp_cache_delete($id, $flag = '') {
72444 global $wp_object_cache;
72445
72446 return $wp_object_cache->delete($id, $flag);
72447 }
72448
72449 /**
72450 * Removes all cache items.
72451 *
72452 * @since 2.0.0
72453 * @uses $wp_object_cache Object Cache Class
72454 * @see WP_Object_Cache::flush()
72455 *
72456 * @return bool Always returns true
72457 */
72458 function wp_cache_flush() {
72459 global $wp_object_cache;
72460
72461 return $wp_object_cache->flush();
72462 }
72463
72464 /**
72465 * Retrieves the cache contents from the cache by ID and flag.
72466 *
72467 * @since 2.0.0
72468 * @uses $wp_object_cache Object Cache Class
72469 * @see WP_Object_Cache::get()
72470 *
72471 * @param int|string $id What the contents in the cache are called
72472 * @param string $flag Where the cache contents are grouped
72473 * @return bool|mixed False on failure to retrieve contents or the cache
72474 * contents on success
72475 */
72476 function wp_cache_get($id, $flag = '') {
72477 global $wp_object_cache;
72478
72479 return $wp_object_cache->get($id, $flag);
72480 }
72481
72482 /**
72483 * Sets up Object Cache Global and assigns it.
72484 *
72485 * @since 2.0.0
72486 * @global WP_Object_Cache $wp_object_cache WordPress Object Cache
72487 */
72488 function wp_cache_init() {
72489 $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
72490 }
72491
72492 /**
72493 * Replaces the contents of the cache with new data.
72494 *
72495 * @since 2.0.0
72496 * @uses $wp_object_cache Object Cache Class
72497 * @see WP_Object_Cache::replace()
72498 *
72499 * @param int|string $id What to call the contents in the cache
72500 * @param mixed $data The contents to store in the cache
72501 * @param string $flag Where to group the cache contents
72502 * @param int $expire When to expire the cache contents
72503 * @return bool False if cache ID and group already exists, true on success
72504 */
72505 function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
72506 global $wp_object_cache;
72507
72508 return $wp_object_cache->replace($key, $data, $flag, $expire);
72509 }
72510
72511 /**
72512 * Saves the data to the cache.
72513 *
72514 * @since 2.0
72515 * @uses $wp_object_cache Object Cache Class
72516 * @see WP_Object_Cache::set()
72517 *
72518 * @param int|string $id What to call the contents in the cache
72519 * @param mixed $data The contents to store in the cache
72520 * @param string $flag Where to group the cache contents
72521 * @param int $expire When to expire the cache contents
72522 * @return bool False if cache ID and group already exists, true on success
72523 */
72524 function wp_cache_set($key, $data, $flag = '', $expire = 0) {
72525 global $wp_object_cache;
72526
72527 return $wp_object_cache->set($key, $data, $flag, $expire);
72528 }
72529
72530 /**
72531 * Adds a group or set of groups to the list of global groups.
72532 *
72533 * @since 2.6.0
72534 *
72535 * @param string|array $groups A group or an array of groups to add
72536 */
72537 function wp_cache_add_global_groups( $groups ) {
72538 // Default cache doesn't persist so nothing to do here.
72539 return;
72540 }
72541
72542 /**
72543 * Adds a group or set of groups to the list of non-persistent groups.
72544 *
72545 * @since 2.6.0
72546 *
72547 * @param string|array $groups A group or an array of groups to add
72548 */
72549 function wp_cache_add_non_persistent_groups( $groups ) {
72550 // Default cache doesn't persist so nothing to do here.
72551 return;
72552 }
72553
72554 /**
72555 * WordPress Object Cache
72556 *
72557 * The WordPress Object Cache is used to save on trips to the database. The
72558 * Object Cache stores all of the cache data to memory and makes the cache
72559 * contents available by using a key, which is used to name and later retrieve
72560 * the cache contents.
72561 *
72562 * The Object Cache can be replaced by other caching mechanisms by placing files
72563 * in the wp-content folder which is looked at in wp-settings. If that file
72564 * exists, then this file will not be included.
72565 *
72566 * @package WordPress
72567 * @subpackage Cache
72568 * @since 2.0
72569 */
72570 class WP_Object_Cache {
72571
72572 /**
72573 * Holds the cached objects
72574 *
72575 * @var array
72576 * @access private
72577 * @since 2.0.0
72578 */
72579 var $cache = array ();
72580
72581 /**
72582 * Cache objects that do not exist in the cache
72583 *
72584 * @var array
72585 * @access private
72586 * @since 2.0.0
72587 */
72588 var $non_existant_objects = array ();
72589
72590 /**
72591 * The amount of times the cache data was already stored in the cache.
72592 *
72593 * @since 2.5.0
72594 * @access private
72595 * @var int
72596 */
72597 var $cache_hits = 0;
72598
72599 /**
72600 * Amount of times the cache did not have the request in cache
72601 *
72602 * @var int
72603 * @access public
72604 * @since 2.0.0
72605 */
72606 var $cache_misses = 0;
72607
72608 /**
72609 * Adds data to the cache if it doesn't already exist.
72610 *
72611 * @uses WP_Object_Cache::get Checks to see if the cache already has data.
72612 * @uses WP_Object_Cache::set Sets the data after the checking the cache
72613 * contents existance.
72614 *
72615 * @since 2.0.0
72616 *
72617 * @param int|string $id What to call the contents in the cache
72618 * @param mixed $data The contents to store in the cache
72619 * @param string $group Where to group the cache contents
72620 * @param int $expire When to expire the cache contents
72621 * @return bool False if cache ID and group already exists, true on success
72622 */
72623 function add($id, $data, $group = 'default', $expire = '') {
72624 if (empty ($group))
72625 $group = 'default';
72626
72627 if (false !== $this->get($id, $group, false))
72628 return false;
72629
72630 return $this->set($id, $data, $group, $expire);
72631 }
72632
72633 /**
72634 * Remove the contents of the cache ID in the group
72635 *
72636 * If the cache ID does not exist in the group and $force parameter is set
72637 * to false, then nothing will happen. The $force parameter is set to false
72638 * by default.
72639 *
72640 * On success the group and the id will be added to the
72641 * $non_existant_objects property in the class.
72642 *
72643 * @since 2.0.0
72644 *
72645 * @param int|string $id What the contents in the cache are called
72646 * @param string $group Where the cache contents are grouped
72647 * @param bool $force Optional. Whether to force the unsetting of the cache
72648 * ID in the group
72649 * @return bool False if the contents weren't deleted and true on success
72650 */
72651 function delete($id, $group = 'default', $force = false) {
72652 if (empty ($group))
72653 $group = 'default';
72654
72655 if (!$force && false === $this->get($id, $group, false))
72656 return false;
72657
72658 unset ($this->cache[$group][$id]);
72659 $this->non_existant_objects[$group][$id] = true;
72660 return true;
72661 }
72662
72663 /**
72664 * Clears the object cache of all data
72665 *
72666 * @since 2.0.0
72667 *
72668 * @return bool Always returns true
72669 */
72670 function flush() {
72671 $this->cache = array ();
72672
72673 return true;
72674 }
72675
72676 /**
72677 * Retrieves the cache contents, if it exists
72678 *
72679 * The contents will be first attempted to be retrieved by searching by the
72680 * ID in the cache group. If the cache is hit (success) then the contents
72681 * are returned.
72682 *
72683 * On failure, the $non_existant_objects property is checked and if the
72684 * cache group and ID exist in there the cache misses will not be
72685 * incremented. If not in the nonexistant objects property, then the cache
72686 * misses will be incremented and the cache group and ID will be added to
72687 * the nonexistant objects.
72688 *
72689 * @since 2.0.0
72690 *
72691 * @param int|string $id What the contents in the cache are called
72692 * @param string $group Where the cache contents are grouped
72693 * @return bool|mixed False on failure to retrieve contents or the cache
72694 * contents on success
72695 */
72696 function get($id, $group = 'default') {
72697 if (empty ($group))
72698 $group = 'default';
72699
72700 if (isset ($this->cache[$group][$id])) {
72701 $this->cache_hits += 1;
72702 if ( is_object($this->cache[$group][$id]) )
72703 return wp_clone($this->cache[$group][$id]);
72704 else
72705 return $this->cache[$group][$id];
72706 }
72707
72708 if ( isset ($this->non_existant_objects[$group][$id]) )
72709 return false;
72710
72711 $this->non_existant_objects[$group][$id] = true;
72712 $this->cache_misses += 1;
72713 return false;
72714 }
72715
72716 /**
72717 * Replace the contents in the cache, if contents already exist
72718 *
72719 * @since 2.0.0
72720 * @see WP_Object_Cache::set()
72721 *
72722 * @param int|string $id What to call the contents in the cache
72723 * @param mixed $data The contents to store in the cache
72724 * @param string $group Where to group the cache contents
72725 * @param int $expire When to expire the cache contents
72726 * @return bool False if not exists, true if contents were replaced
72727 */
72728 function replace($id, $data, $group = 'default', $expire = '') {
72729 if (empty ($group))
72730 $group = 'default';
72731
72732 if (false === $this->get($id, $group, false))
72733 return false;
72734
72735 return $this->set($id, $data, $group, $expire);
72736 }
72737
72738 /**
72739 * Sets the data contents into the cache
72740 *
72741 * The cache contents is grouped by the $group parameter followed by the
72742 * $id. This allows for duplicate ids in unique groups. Therefore, naming of
72743 * the group should be used with care and should follow normal function
72744 * naming guidelines outside of core WordPress usage.
72745 *
72746 * The $expire parameter is not used, because the cache will automatically
72747 * expire for each time a page is accessed and PHP finishes. The method is
72748 * more for cache plugins which use files.
72749 *
72750 * @since 2.0.0
72751 *
72752 * @param int|string $id What to call the contents in the cache
72753 * @param mixed $data The contents to store in the cache
72754 * @param string $group Where to group the cache contents
72755 * @param int $expire Not Used
72756 * @return bool Always returns true
72757 */
72758 function set($id, $data, $group = 'default', $expire = '') {
72759 if (empty ($group))
72760 $group = 'default';
72761
72762 if (NULL === $data)
72763 $data = '';
72764
72765 if ( is_object($data) )
72766 $data = wp_clone($data);
72767
72768 $this->cache[$group][$id] = $data;
72769
72770 if(isset($this->non_existant_objects[$group][$id]))
72771 unset ($this->non_existant_objects[$group][$id]);
72772
72773 return true;
72774 }
72775
72776 /**
72777 * Echos the stats of the caching.
72778 *
72779 * Gives the cache hits, and cache misses. Also prints every cached group,
72780 * key and the data.
72781 *
72782 * @since 2.0.0
72783 */
72784 function stats() {
72785 echo "<p>";
72786 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
72787 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
72788 echo "</p>";
72789
72790 foreach ($this->cache as $group => $cache) {
72791 echo "<p>";
72792 echo "<strong>Group:</strong> $group<br />";
72793 echo "<strong>Cache:</strong>";
72794 echo "<pre>";
72795 print_r($cache);
72796 echo "</pre>";
72797 }
72798 }
72799
72800 /**
72801 * PHP4 constructor; Calls PHP 5 style constructor
72802 *
72803 * @since 2.0.0
72804 *
72805 * @return WP_Object_Cache
72806 */
72807 function WP_Object_Cache() {
72808 return $this->__construct();
72809 }
72810
72811 /**
72812 * Sets up object properties; PHP 5 style constructor
72813 *
72814 * @since 2.0.8
72815 * @return null|WP_Object_Cache If cache is disabled, returns null.
72816 */
72817 function __construct() {
72818 /**
72819 * @todo This should be moved to the PHP4 style constructor, PHP5
72820 * already calls __destruct()
72821 */
72822 register_shutdown_function(array(&$this, "__destruct"));
72823 }
72824
72825 /**
72826 * Will save the object cache before object is completely destroyed.
72827 *
72828 * Called upon object destruction, which should be when PHP ends.
72829 *
72830 * @since 2.0.8
72831 *
72832 * @return bool True value. Won't be used by PHP
72833 */
72834 function __destruct() {
72835 return true;
72836 }
72837 }
72838 ?>