[ Index ] |
PHP Cross Reference of BackPress |
[Summary view] [Print] [Text view]
1 <?php 2 // Last sync [WP11537] 3 4 /** 5 * WordPress Object Cache 6 * 7 * The WordPress Object Cache is used to save on trips to the database. The 8 * Object Cache stores all of the cache data to memory and makes the cache 9 * contents available by using a key, which is used to name and later retrieve 10 * the cache contents. 11 * 12 * The Object Cache can be replaced by other caching mechanisms by placing files 13 * in the wp-content folder which is looked at in wp-settings. If that file 14 * exists, then this file will not be included. 15 * 16 * @package WordPress 17 * @subpackage Cache 18 * @since 2.0 19 */ 20 class WP_Object_Cache { 21 22 /** 23 * Holds the cached objects 24 * 25 * @var array 26 * @access private 27 * @since 2.0.0 28 */ 29 var $cache = array (); 30 31 /** 32 * Cache objects that do not exist in the cache 33 * 34 * @var array 35 * @access private 36 * @since 2.0.0 37 */ 38 var $non_existant_objects = array (); 39 40 /** 41 * The amount of times the cache data was already stored in the cache. 42 * 43 * @since 2.5.0 44 * @access private 45 * @var int 46 */ 47 var $cache_hits = 0; 48 49 /** 50 * Amount of times the cache did not have the request in cache 51 * 52 * @var int 53 * @access public 54 * @since 2.0.0 55 */ 56 var $cache_misses = 0; 57 58 /** 59 * Adds data to the cache if it doesn't already exist. 60 * 61 * @uses WP_Object_Cache::get Checks to see if the cache already has data. 62 * @uses WP_Object_Cache::set Sets the data after the checking the cache 63 * contents existance. 64 * 65 * @since 2.0.0 66 * 67 * @param int|string $id What to call the contents in the cache 68 * @param mixed $data The contents to store in the cache 69 * @param string $group Where to group the cache contents 70 * @param int $expire When to expire the cache contents 71 * @return bool False if cache ID and group already exists, true on success 72 */ 73 function add($id, $data, $group = 'default', $expire = '') { 74 if (empty ($group)) 75 $group = 'default'; 76 77 if (false !== $this->get($id, $group, false)) 78 return false; 79 80 return $this->set($id, $data, $group, $expire); 81 } 82 83 /** 84 * Remove the contents of the cache ID in the group 85 * 86 * If the cache ID does not exist in the group and $force parameter is set 87 * to false, then nothing will happen. The $force parameter is set to false 88 * by default. 89 * 90 * On success the group and the id will be added to the 91 * $non_existant_objects property in the class. 92 * 93 * @since 2.0.0 94 * 95 * @param int|string $id What the contents in the cache are called 96 * @param string $group Where the cache contents are grouped 97 * @param bool $force Optional. Whether to force the unsetting of the cache 98 * ID in the group 99 * @return bool False if the contents weren't deleted and true on success 100 */ 101 function delete($id, $group = 'default', $force = false) { 102 if (empty ($group)) 103 $group = 'default'; 104 105 if (!$force && false === $this->get($id, $group, false)) 106 return false; 107 108 unset ($this->cache[$group][$id]); 109 $this->non_existant_objects[$group][$id] = true; 110 return true; 111 } 112 113 /** 114 * Clears the object cache of all data 115 * 116 * @since 2.0.0 117 * 118 * @return bool Always returns true 119 */ 120 function flush( $group = null ) { 121 // WP does not support group flushing 122 if ( is_null($group) ) { 123 $this->cache = array(); 124 $this->non_existant_objects = array(); 125 return true; 126 } 127 128 if ( isset($this->cache[$group]) ) 129 $this->cache[$group] = array(); 130 if ( isset($this->non_existant_objects[$group]) ) 131 $this->non_existant_objects[$group] = array(); 132 } 133 134 /** 135 * Retrieves the cache contents, if it exists 136 * 137 * The contents will be first attempted to be retrieved by searching by the 138 * ID in the cache group. If the cache is hit (success) then the contents 139 * are returned. 140 * 141 * On failure, the $non_existant_objects property is checked and if the 142 * cache group and ID exist in there the cache misses will not be 143 * incremented. If not in the nonexistant objects property, then the cache 144 * misses will be incremented and the cache group and ID will be added to 145 * the nonexistant objects. 146 * 147 * @since 2.0.0 148 * 149 * @param int|string $id What the contents in the cache are called 150 * @param string $group Where the cache contents are grouped 151 * @return bool|mixed False on failure to retrieve contents or the cache 152 * contents on success 153 */ 154 function get($id, $group = 'default') { 155 if (empty ($group)) 156 $group = 'default'; 157 158 if (isset ($this->cache[$group][$id])) { 159 $this->cache_hits += 1; 160 if ( is_object($this->cache[$group][$id]) ) 161 return wp_clone($this->cache[$group][$id]); 162 else 163 return $this->cache[$group][$id]; 164 } 165 166 if ( isset ($this->non_existant_objects[$group][$id]) ) 167 return false; 168 169 $this->non_existant_objects[$group][$id] = true; 170 $this->cache_misses += 1; 171 return false; 172 } 173 174 /** 175 * Replace the contents in the cache, if contents already exist 176 * 177 * @since 2.0.0 178 * @see WP_Object_Cache::set() 179 * 180 * @param int|string $id What to call the contents in the cache 181 * @param mixed $data The contents to store in the cache 182 * @param string $group Where to group the cache contents 183 * @param int $expire When to expire the cache contents 184 * @return bool False if not exists, true if contents were replaced 185 */ 186 function replace($id, $data, $group = 'default', $expire = '') { 187 if (empty ($group)) 188 $group = 'default'; 189 190 if (false === $this->get($id, $group, false)) 191 return false; 192 193 return $this->set($id, $data, $group, $expire); 194 } 195 196 /** 197 * Sets the data contents into the cache 198 * 199 * The cache contents is grouped by the $group parameter followed by the 200 * $id. This allows for duplicate ids in unique groups. Therefore, naming of 201 * the group should be used with care and should follow normal function 202 * naming guidelines outside of core WordPress usage. 203 * 204 * The $expire parameter is not used, because the cache will automatically 205 * expire for each time a page is accessed and PHP finishes. The method is 206 * more for cache plugins which use files. 207 * 208 * @since 2.0.0 209 * 210 * @param int|string $id What to call the contents in the cache 211 * @param mixed $data The contents to store in the cache 212 * @param string $group Where to group the cache contents 213 * @param int $expire Not Used 214 * @return bool Always returns true 215 */ 216 function set($id, $data, $group = 'default', $expire = '') { 217 if (empty ($group)) 218 $group = 'default'; 219 220 if (NULL === $data) 221 $data = ''; 222 223 if ( is_object($data) ) 224 $data = wp_clone($data); 225 226 $this->cache[$group][$id] = $data; 227 228 if(isset($this->non_existant_objects[$group][$id])) 229 unset ($this->non_existant_objects[$group][$id]); 230 231 return true; 232 } 233 234 /** 235 * Echos the stats of the caching. 236 * 237 * Gives the cache hits, and cache misses. Also prints every cached group, 238 * key and the data. 239 * 240 * @since 2.0.0 241 */ 242 function stats() { 243 echo "<p>"; 244 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; 245 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; 246 echo "</p>"; 247 248 foreach ($this->cache as $group => $cache) { 249 echo "<p>"; 250 echo "<strong>Group:</strong> $group<br />"; 251 echo "<strong>Cache:</strong>"; 252 echo "<pre>"; 253 print_r($cache); 254 echo "</pre>"; 255 } 256 } 257 258 function add_global_groups( $groups ) 259 { 260 return true; 261 } 262 263 function add_non_persistent_groups( $groups ) 264 { 265 return true; 266 } 267 268 /** 269 * PHP4 constructor; Calls PHP 5 style constructor 270 * 271 * @since 2.0.0 272 * 273 * @return WP_Object_Cache 274 */ 275 function WP_Object_Cache() { 276 return self::__construct(); 277 } 278 279 /** 280 * Sets up object properties; PHP 5 style constructor 281 * 282 * @since 2.0.8 283 * @return null|WP_Object_Cache If cache is disabled, returns null. 284 */ 285 function __construct() { 286 /** 287 * @todo This should be moved to the PHP4 style constructor, PHP5 288 * already calls __destruct() 289 */ 290 register_shutdown_function(array(&$this, "__destruct")); 291 } 292 293 /** 294 * Will save the object cache before object is completely destroyed. 295 * 296 * Called upon object destruction, which should be when PHP ends. 297 * 298 * @since 2.0.8 299 * 300 * @return bool True value. Won't be used by PHP 301 */ 302 function __destruct() { 303 return true; 304 } 305 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 26 01:01:01 2024 | Cross-referenced by PHPXref 0.7.1 |