home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / cache.php < prev    next >
Encoding:
PHP Script  |  2017-10-02  |  21.1 KB  |  733 lines

  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. /**
  12.  * Adds data to the cache, if the cache key doesn't already exist.
  13.  *
  14.  * @since 2.0.0
  15.  *
  16.  * @see WP_Object_Cache::add()
  17.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  18.  *
  19.  * @param int|string $key    The cache key to use for retrieval later.
  20.  * @param mixed      $data   The data to add to the cache.
  21.  * @param string     $group  Optional. The group to add the cache to. Enables the same key
  22.  *                           to be used across groups. Default empty.
  23.  * @param int        $expire Optional. When the cache data should expire, in seconds.
  24.  *                           Default 0 (no expiration).
  25.  * @return bool False if cache key and group already exist, true on success.
  26.  */
  27. function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
  28.     global $wp_object_cache;
  29.  
  30.     return $wp_object_cache->add( $key, $data, $group, (int) $expire );
  31. }
  32.  
  33. /**
  34.  * Closes the cache.
  35.  *
  36.  * This function has ceased to do anything since WordPress 2.5. The
  37.  * functionality was removed along with the rest of the persistent cache.
  38.  *
  39.  * This does not mean that plugins can't implement this function when they need
  40.  * to make sure that the cache is cleaned up after WordPress no longer needs it.
  41.  *
  42.  * @since 2.0.0
  43.  *
  44.  * @return true Always returns true.
  45.  */
  46. function wp_cache_close() {
  47.     return true;
  48. }
  49.  
  50. /**
  51.  * Decrements numeric cache item's value.
  52.  *
  53.  * @since 3.3.0
  54.  *
  55.  * @see WP_Object_Cache::decr()
  56.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  57.  *
  58.  * @param int|string $key    The cache key to decrement.
  59.  * @param int        $offset Optional. The amount by which to decrement the item's value. Default 1.
  60.  * @param string     $group  Optional. The group the key is in. Default empty.
  61.  * @return false|int False on failure, the item's new value on success.
  62.  */
  63. function wp_cache_decr( $key, $offset = 1, $group = '' ) {
  64.     global $wp_object_cache;
  65.  
  66.     return $wp_object_cache->decr( $key, $offset, $group );
  67. }
  68.  
  69. /**
  70.  * Removes the cache contents matching key and group.
  71.  *
  72.  * @since 2.0.0
  73.  *
  74.  * @see WP_Object_Cache::delete()
  75.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  76.  *
  77.  * @param int|string $key   What the contents in the cache are called.
  78.  * @param string     $group Optional. Where the cache contents are grouped. Default empty.
  79.  * @return bool True on successful removal, false on failure.
  80.  */
  81. function wp_cache_delete( $key, $group = '' ) {
  82.     global $wp_object_cache;
  83.  
  84.     return $wp_object_cache->delete($key, $group);
  85. }
  86.  
  87. /**
  88.  * Removes all cache items.
  89.  *
  90.  * @since 2.0.0
  91.  *
  92.  * @see WP_Object_Cache::flush()
  93.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  94.  *
  95.  * @return bool False on failure, true on success
  96.  */
  97. function wp_cache_flush() {
  98.     global $wp_object_cache;
  99.  
  100.     return $wp_object_cache->flush();
  101. }
  102.  
  103. /**
  104.  * Retrieves the cache contents from the cache by key and group.
  105.  *
  106.  * @since 2.0.0
  107.  *
  108.  * @see WP_Object_Cache::get()
  109.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  110.  *
  111.  * @param int|string  $key    The key under which the cache contents are stored.
  112.  * @param string      $group  Optional. Where the cache contents are grouped. Default empty.
  113.  * @param bool        $force  Optional. Whether to force an update of the local cache from the persistent
  114.  *                            cache. Default false.
  115.  * @param bool        $found  Optional. Whether the key was found in the cache (passed by reference).
  116.  *                            Disambiguates a return of false, a storable value. Default null.
  117.  * @return bool|mixed False on failure to retrieve contents or the cache
  118.  *                      contents on success
  119.  */
  120. function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
  121.     global $wp_object_cache;
  122.  
  123.     return $wp_object_cache->get( $key, $group, $force, $found );
  124. }
  125.  
  126. /**
  127.  * Increment numeric cache item's value
  128.  *
  129.  * @since 3.3.0
  130.  *
  131.  * @see WP_Object_Cache::incr()
  132.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  133.  *
  134.  * @param int|string $key    The key for the cache contents that should be incremented.
  135.  * @param int        $offset Optional. The amount by which to increment the item's value. Default 1.
  136.  * @param string     $group  Optional. The group the key is in. Default empty.
  137.  * @return false|int False on failure, the item's new value on success.
  138.  */
  139. function wp_cache_incr( $key, $offset = 1, $group = '' ) {
  140.     global $wp_object_cache;
  141.  
  142.     return $wp_object_cache->incr( $key, $offset, $group );
  143. }
  144.  
  145. /**
  146.  * Sets up Object Cache Global and assigns it.
  147.  *
  148.  * @since 2.0.0
  149.  *
  150.  * @global WP_Object_Cache $wp_object_cache
  151.  */
  152. function wp_cache_init() {
  153.     $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
  154. }
  155.  
  156. /**
  157.  * Replaces the contents of the cache with new data.
  158.  *
  159.  * @since 2.0.0
  160.  *
  161.  * @see WP_Object_Cache::replace()
  162.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  163.  *
  164.  * @param int|string $key    The key for the cache data that should be replaced.
  165.  * @param mixed      $data   The new data to store in the cache.
  166.  * @param string     $group  Optional. The group for the cache data that should be replaced.
  167.  *                           Default empty.
  168.  * @param int        $expire Optional. When to expire the cache contents, in seconds.
  169.  *                           Default 0 (no expiration).
  170.  * @return bool False if original value does not exist, true if contents were replaced
  171.  */
  172. function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
  173.     global $wp_object_cache;
  174.  
  175.     return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
  176. }
  177.  
  178. /**
  179.  * Saves the data to the cache.
  180.  *
  181.  * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
  182.  *
  183.  * @since 2.0.0
  184.  *
  185.  * @see WP_Object_Cache::set()
  186.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  187.  *
  188.  * @param int|string $key    The cache key to use for retrieval later.
  189.  * @param mixed      $data   The contents to store in the cache.
  190.  * @param string     $group  Optional. Where to group the cache contents. Enables the same key
  191.  *                           to be used across groups. Default empty.
  192.  * @param int        $expire Optional. When to expire the cache contents, in seconds.
  193.  *                           Default 0 (no expiration).
  194.  * @return bool False on failure, true on success
  195.  */
  196. function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
  197.     global $wp_object_cache;
  198.  
  199.     return $wp_object_cache->set( $key, $data, $group, (int) $expire );
  200. }
  201.  
  202. /**
  203.  * Switches the internal blog ID.
  204.  *
  205.  * This changes the blog id used to create keys in blog specific groups.
  206.  *
  207.  * @since 3.5.0
  208.  *
  209.  * @see WP_Object_Cache::switch_to_blog()
  210.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  211.  *
  212.  * @param int $blog_id Site ID.
  213.  */
  214. function wp_cache_switch_to_blog( $blog_id ) {
  215.     global $wp_object_cache;
  216.  
  217.     $wp_object_cache->switch_to_blog( $blog_id );
  218. }
  219.  
  220. /**
  221.  * Adds a group or set of groups to the list of global groups.
  222.  *
  223.  * @since 2.6.0
  224.  *
  225.  * @see WP_Object_Cache::add_global_groups()
  226.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  227.  *
  228.  * @param string|array $groups A group or an array of groups to add.
  229.  */
  230. function wp_cache_add_global_groups( $groups ) {
  231.     global $wp_object_cache;
  232.  
  233.     $wp_object_cache->add_global_groups( $groups );
  234. }
  235.  
  236. /**
  237.  * Adds a group or set of groups to the list of non-persistent groups.
  238.  *
  239.  * @since 2.6.0
  240.  *
  241.  * @param string|array $groups A group or an array of groups to add.
  242.  */
  243. function wp_cache_add_non_persistent_groups( $groups ) {
  244.     // Default cache doesn't persist so nothing to do here.
  245. }
  246.  
  247. /**
  248.  * Reset internal cache keys and structures.
  249.  *
  250.  * If the cache back end uses global blog or site IDs as part of its cache keys,
  251.  * this function instructs the back end to reset those keys and perform any cleanup
  252.  * since blog or site IDs have changed since cache init.
  253.  *
  254.  * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
  255.  * function when preparing the cache for a blog switch. For clearing the cache
  256.  * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
  257.  * recommended outside of unit tests as the performance penalty for using it is
  258.  * high.
  259.  *
  260.  * @since 2.6.0
  261.  * @deprecated 3.5.0 WP_Object_Cache::reset()
  262.  * @see WP_Object_Cache::reset()
  263.  *
  264.  * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  265.  */
  266. function wp_cache_reset() {
  267.     _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()' );
  268.  
  269.     global $wp_object_cache;
  270.  
  271.     $wp_object_cache->reset();
  272. }
  273.  
  274. /**
  275.  * Core class that implements an object cache.
  276.  *
  277.  * The WordPress Object Cache is used to save on trips to the database. The
  278.  * Object Cache stores all of the cache data to memory and makes the cache
  279.  * contents available by using a key, which is used to name and later retrieve
  280.  * the cache contents.
  281.  *
  282.  * The Object Cache can be replaced by other caching mechanisms by placing files
  283.  * in the wp-content folder which is looked at in wp-settings. If that file
  284.  * exists, then this file will not be included.
  285.  *
  286.  * @since 2.0.0
  287.  */
  288. class WP_Object_Cache {
  289.  
  290.     /**
  291.      * Holds the cached objects.
  292.      *
  293.      * @since 2.0.0
  294.      * @var array
  295.      */
  296.     private $cache = array();
  297.  
  298.     /**
  299.      * The amount of times the cache data was already stored in the cache.
  300.      *
  301.      * @since 2.5.0
  302.      * @var int
  303.      */
  304.     public $cache_hits = 0;
  305.  
  306.     /**
  307.      * Amount of times the cache did not have the request in cache.
  308.      *
  309.      * @since 2.0.0
  310.      * @var int
  311.      */
  312.     public $cache_misses = 0;
  313.  
  314.     /**
  315.      * List of global cache groups.
  316.      *
  317.      * @since 3.0.0
  318.      * @var array
  319.      */
  320.     protected $global_groups = array();
  321.  
  322.     /**
  323.      * The blog prefix to prepend to keys in non-global groups.
  324.      *
  325.      * @since 3.5.0
  326.      * @var int
  327.      */
  328.     private $blog_prefix;
  329.  
  330.     /**
  331.      * Holds the value of is_multisite().
  332.      *
  333.      * @since 3.5.0
  334.      * @var bool
  335.      */
  336.     private $multisite;
  337.  
  338.     /**
  339.      * Makes private properties readable for backward compatibility.
  340.      *
  341.      * @since 4.0.0
  342.      *
  343.      * @param string $name Property to get.
  344.      * @return mixed Property.
  345.      */
  346.     public function __get( $name ) {
  347.         return $this->$name;
  348.     }
  349.  
  350.     /**
  351.      * Makes private properties settable for backward compatibility.
  352.      *
  353.      * @since 4.0.0
  354.      *
  355.      * @param string $name  Property to set.
  356.      * @param mixed  $value Property value.
  357.      * @return mixed Newly-set property.
  358.      */
  359.     public function __set( $name, $value ) {
  360.         return $this->$name = $value;
  361.     }
  362.  
  363.     /**
  364.      * Makes private properties checkable for backward compatibility.
  365.      *
  366.      * @since 4.0.0
  367.      *
  368.      * @param string $name Property to check if set.
  369.      * @return bool Whether the property is set.
  370.      */
  371.     public function __isset( $name ) {
  372.         return isset( $this->$name );
  373.     }
  374.  
  375.     /**
  376.      * Makes private properties un-settable for backward compatibility.
  377.      *
  378.      * @since 4.0.0
  379.      *
  380.      * @param string $name Property to unset.
  381.      */
  382.     public function __unset( $name ) {
  383.         unset( $this->$name );
  384.     }
  385.  
  386.     /**
  387.      * Adds data to the cache if it doesn't already exist.
  388.      *
  389.      * @since 2.0.0
  390.      *
  391.      * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data.
  392.      * @uses WP_Object_Cache::set()     Sets the data after the checking the cache
  393.      *                                    contents existence.
  394.      *
  395.      * @param int|string $key    What to call the contents in the cache.
  396.      * @param mixed      $data   The contents to store in the cache.
  397.      * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
  398.      * @param int        $expire Optional. When to expire the cache contents. Default 0 (no expiration).
  399.      * @return bool False if cache key and group already exist, true on success
  400.      */
  401.     public function add( $key, $data, $group = 'default', $expire = 0 ) {
  402.         if ( wp_suspend_cache_addition() )
  403.             return false;
  404.  
  405.         if ( empty( $group ) )
  406.             $group = 'default';
  407.  
  408.         $id = $key;
  409.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  410.             $id = $this->blog_prefix . $key;
  411.  
  412.         if ( $this->_exists( $id, $group ) )
  413.             return false;
  414.  
  415.         return $this->set( $key, $data, $group, (int) $expire );
  416.     }
  417.  
  418.     /**
  419.      * Sets the list of global cache groups.
  420.      *
  421.      * @since 3.0.0
  422.      *
  423.      * @param array $groups List of groups that are global.
  424.      */
  425.     public function add_global_groups( $groups ) {
  426.         $groups = (array) $groups;
  427.  
  428.         $groups = array_fill_keys( $groups, true );
  429.         $this->global_groups = array_merge( $this->global_groups, $groups );
  430.     }
  431.  
  432.     /**
  433.      * Decrements numeric cache item's value.
  434.      *
  435.      * @since 3.3.0
  436.      *
  437.      * @param int|string $key    The cache key to decrement.
  438.      * @param int        $offset Optional. The amount by which to decrement the item's value. Default 1.
  439.      * @param string     $group  Optional. The group the key is in. Default 'default'.
  440.      * @return false|int False on failure, the item's new value on success.
  441.      */
  442.     public function decr( $key, $offset = 1, $group = 'default' ) {
  443.         if ( empty( $group ) )
  444.             $group = 'default';
  445.  
  446.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  447.             $key = $this->blog_prefix . $key;
  448.  
  449.         if ( ! $this->_exists( $key, $group ) )
  450.             return false;
  451.  
  452.         if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
  453.             $this->cache[ $group ][ $key ] = 0;
  454.  
  455.         $offset = (int) $offset;
  456.  
  457.         $this->cache[ $group ][ $key ] -= $offset;
  458.  
  459.         if ( $this->cache[ $group ][ $key ] < 0 )
  460.             $this->cache[ $group ][ $key ] = 0;
  461.  
  462.         return $this->cache[ $group ][ $key ];
  463.     }
  464.  
  465.     /**
  466.      * Removes the contents of the cache key in the group.
  467.      *
  468.      * If the cache key does not exist in the group, then nothing will happen.
  469.      *
  470.      * @since 2.0.0
  471.      *
  472.      * @param int|string $key        What the contents in the cache are called.
  473.      * @param string     $group      Optional. Where the cache contents are grouped. Default 'default'.
  474.      * @param bool       $deprecated Optional. Unused. Default false.
  475.      * @return bool False if the contents weren't deleted and true on success.
  476.      */
  477.     public function delete( $key, $group = 'default', $deprecated = false ) {
  478.         if ( empty( $group ) )
  479.             $group = 'default';
  480.  
  481.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  482.             $key = $this->blog_prefix . $key;
  483.  
  484.         if ( ! $this->_exists( $key, $group ) )
  485.             return false;
  486.  
  487.         unset( $this->cache[$group][$key] );
  488.         return true;
  489.     }
  490.  
  491.     /**
  492.      * Clears the object cache of all data.
  493.      *
  494.      * @since 2.0.0
  495.      *
  496.      * @return true Always returns true.
  497.      */
  498.     public function flush() {
  499.         $this->cache = array();
  500.  
  501.         return true;
  502.     }
  503.  
  504.     /**
  505.      * Retrieves the cache contents, if it exists.
  506.      *
  507.      * The contents will be first attempted to be retrieved by searching by the
  508.      * key in the cache group. If the cache is hit (success) then the contents
  509.      * are returned.
  510.      *
  511.      * On failure, the number of cache misses will be incremented.
  512.      *
  513.      * @since 2.0.0
  514.      *
  515.      * @param int|string $key    What the contents in the cache are called.
  516.      * @param string     $group  Optional. Where the cache contents are grouped. Default 'default'.
  517.      * @param string     $force  Optional. Unused. Whether to force a refetch rather than relying on the local
  518.      *                           cache. Default false.
  519.      * @param bool        $found  Optional. Whether the key was found in the cache (passed by reference).
  520.      *                            Disambiguates a return of false, a storable value. Default null.
  521.      * @return false|mixed False on failure to retrieve contents or the cache contents on success.
  522.      */
  523.     public function get( $key, $group = 'default', $force = false, &$found = null ) {
  524.         if ( empty( $group ) )
  525.             $group = 'default';
  526.  
  527.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  528.             $key = $this->blog_prefix . $key;
  529.  
  530.         if ( $this->_exists( $key, $group ) ) {
  531.             $found = true;
  532.             $this->cache_hits += 1;
  533.             if ( is_object($this->cache[$group][$key]) )
  534.                 return clone $this->cache[$group][$key];
  535.             else
  536.                 return $this->cache[$group][$key];
  537.         }
  538.  
  539.         $found = false;
  540.         $this->cache_misses += 1;
  541.         return false;
  542.     }
  543.  
  544.     /**
  545.      * Increments numeric cache item's value.
  546.      *
  547.      * @since 3.3.0
  548.      *
  549.      * @param int|string $key    The cache key to increment
  550.      * @param int        $offset Optional. The amount by which to increment the item's value. Default 1.
  551.      * @param string     $group  Optional. The group the key is in. Default 'default'.
  552.      * @return false|int False on failure, the item's new value on success.
  553.      */
  554.     public function incr( $key, $offset = 1, $group = 'default' ) {
  555.         if ( empty( $group ) )
  556.             $group = 'default';
  557.  
  558.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  559.             $key = $this->blog_prefix . $key;
  560.  
  561.         if ( ! $this->_exists( $key, $group ) )
  562.             return false;
  563.  
  564.         if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
  565.             $this->cache[ $group ][ $key ] = 0;
  566.  
  567.         $offset = (int) $offset;
  568.  
  569.         $this->cache[ $group ][ $key ] += $offset;
  570.  
  571.         if ( $this->cache[ $group ][ $key ] < 0 )
  572.             $this->cache[ $group ][ $key ] = 0;
  573.  
  574.         return $this->cache[ $group ][ $key ];
  575.     }
  576.  
  577.     /**
  578.      * Replaces the contents in the cache, if contents already exist.
  579.      *
  580.      * @since 2.0.0
  581.      *
  582.      * @see WP_Object_Cache::set()
  583.      *
  584.      * @param int|string $key    What to call the contents in the cache.
  585.      * @param mixed      $data   The contents to store in the cache.
  586.      * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
  587.      * @param int        $expire Optional. When to expire the cache contents. Default 0 (no expiration).
  588.      * @return bool False if not exists, true if contents were replaced.
  589.      */
  590.     public function replace( $key, $data, $group = 'default', $expire = 0 ) {
  591.         if ( empty( $group ) )
  592.             $group = 'default';
  593.  
  594.         $id = $key;
  595.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  596.             $id = $this->blog_prefix . $key;
  597.  
  598.         if ( ! $this->_exists( $id, $group ) )
  599.             return false;
  600.  
  601.         return $this->set( $key, $data, $group, (int) $expire );
  602.     }
  603.  
  604.     /**
  605.      * Resets cache keys.
  606.      *
  607.      * @since 3.0.0
  608.      *
  609.      * @deprecated 3.5.0 Use switch_to_blog()
  610.      * @see switch_to_blog()
  611.      */
  612.     public function reset() {
  613.         _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' );
  614.  
  615.         // Clear out non-global caches since the blog ID has changed.
  616.         foreach ( array_keys( $this->cache ) as $group ) {
  617.             if ( ! isset( $this->global_groups[ $group ] ) )
  618.                 unset( $this->cache[ $group ] );
  619.         }
  620.     }
  621.  
  622.     /**
  623.      * Sets the data contents into the cache.
  624.      *
  625.      * The cache contents is grouped by the $group parameter followed by the
  626.      * $key. This allows for duplicate ids in unique groups. Therefore, naming of
  627.      * the group should be used with care and should follow normal function
  628.      * naming guidelines outside of core WordPress usage.
  629.      *
  630.      * The $expire parameter is not used, because the cache will automatically
  631.      * expire for each time a page is accessed and PHP finishes. The method is
  632.      * more for cache plugins which use files.
  633.      *
  634.      * @since 2.0.0
  635.      *
  636.      * @param int|string $key    What to call the contents in the cache.
  637.      * @param mixed      $data   The contents to store in the cache.
  638.      * @param string     $group  Optional. Where to group the cache contents. Default 'default'.
  639.      * @param int        $expire Not Used.
  640.      * @return true Always returns true.
  641.      */
  642.     public function set( $key, $data, $group = 'default', $expire = 0 ) {
  643.         if ( empty( $group ) )
  644.             $group = 'default';
  645.  
  646.         if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  647.             $key = $this->blog_prefix . $key;
  648.  
  649.         if ( is_object( $data ) )
  650.             $data = clone $data;
  651.  
  652.         $this->cache[$group][$key] = $data;
  653.         return true;
  654.     }
  655.  
  656.     /**
  657.      * Echoes the stats of the caching.
  658.      *
  659.      * Gives the cache hits, and cache misses. Also prints every cached group,
  660.      * key and the data.
  661.      *
  662.      * @since 2.0.0
  663.      */
  664.     public function stats() {
  665.         echo "<p>";
  666.         echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
  667.         echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
  668.         echo "</p>";
  669.         echo '<ul>';
  670.         foreach ($this->cache as $group => $cache) {
  671.             echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
  672.         }
  673.         echo '</ul>';
  674.     }
  675.  
  676.     /**
  677.      * Switches the internal blog ID.
  678.      *
  679.      * This changes the blog ID used to create keys in blog specific groups.
  680.      *
  681.      * @since 3.5.0
  682.      *
  683.      * @param int $blog_id Blog ID.
  684.      */
  685.     public function switch_to_blog( $blog_id ) {
  686.         $blog_id = (int) $blog_id;
  687.         $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
  688.     }
  689.  
  690.     /**
  691.      * Serves as a utility function to determine whether a key exists in the cache.
  692.      *
  693.      * @since 3.4.0
  694.      *
  695.      * @param int|string $key   Cache key to check for existence.
  696.      * @param string     $group Cache group for the key existence check.
  697.      * @return bool Whether the key exists in the cache for the given group.
  698.      */
  699.     protected function _exists( $key, $group ) {
  700.         return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
  701.     }
  702.  
  703.     /**
  704.      * Sets up object properties; PHP 5 style constructor.
  705.      *
  706.      * @since 2.0.8
  707.      */
  708.     public function __construct() {
  709.         $this->multisite = is_multisite();
  710.         $this->blog_prefix =  $this->multisite ? get_current_blog_id() . ':' : '';
  711.  
  712.  
  713.         /**
  714.          * @todo This should be moved to the PHP4 style constructor, PHP5
  715.          * already calls __destruct()
  716.          */
  717.         register_shutdown_function( array( $this, '__destruct' ) );
  718.     }
  719.  
  720.     /**
  721.      * Saves the object cache before object is completely destroyed.
  722.      *
  723.      * Called upon object destruction, which should be when PHP ends.
  724.      *
  725.      * @since 2.0.8
  726.      *
  727.      * @return true Always returns true.
  728.      */
  729.     public function __destruct() {
  730.         return true;
  731.     }
  732. }
  733.