Changeset 1201
- Timestamp:
- 03/10/08 18:05:28 (9 months ago)
- Files:
-
- trunk/wp-includes/cache.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/wp-includes/cache.php
r1187 r1201 80 80 } 81 81 82 function key($key, $group) {83 global $ blog_id;82 function maybe_localize_group( $group ) { 83 global $wpdb; 84 84 85 85 if ( empty($group) ) 86 86 $group = 'default'; 87 87 88 if (false !== array_search($group, $this->global_groups)) 89 $prefix = ''; 90 else 91 $prefix = $blog_id . '-'; 92 93 if( '' != $key ) 94 $key = '-' . $key; 95 96 return "$prefix$group$key"; 97 } 98 88 $pre_test = substr( $group, 0, strpos( $group, '-' ) ); 89 if( $pre_test == intval( $pre_test ) ) 90 return $group; // already localized. 91 $prefix = ''; 92 if (false === array_search($group, $this->global_groups)) 93 $prefix = $wpdb->blogid . '-'; 94 95 return "$prefix$group"; 96 } 99 97 100 98 function add($id, $data, $group = 'default', $expire = '') { … … 109 107 110 108 function delete($id, $group = 'default', $force = false) { 111 $hash = $this->key($id, $group);112 109 if (empty ($group)) 113 110 $group = 'default'; 111 $group = $this->maybe_localize_group( $group ); 114 112 115 113 if (!$force && false === $this->get($id, $group, false)) 116 114 return false; 117 115 118 unset ($this->cache[$ hash]);119 $this->non_existant_objects[$ hash] = true;120 $this->dirty_objects[$ this->key( '', $group )][] = $id;116 unset ($this->cache[$group][$id]); 117 $this->non_existant_objects[$group][$id] = true; 118 $this->dirty_objects[$group][] = $id; 121 119 return true; 122 120 } … … 142 140 if (empty ($group)) 143 141 $group = 'default'; 144 145 $group_key = $this->key( '', $group ); 146 $hash = $this->key($id, $group_key); 147 148 if (isset ($this->cache[$hash])) { 142 $group = $this->maybe_localize_group( $group ); 143 144 if (isset ($this->cache[$group][$id])) { 149 145 if ($count_hits) 150 146 $this->warm_cache_hits += 1; 151 return $this->cache[$ hash];152 } 153 154 if (isset ($this->non_existant_objects[$ hash]))147 return $this->cache[$group][$id]; 148 } 149 150 if (isset ($this->non_existant_objects[$group][$id])) 155 151 return false; 156 152 157 153 // If caching is not enabled, we have to fall back to pulling from the DB. 158 154 if (!$this->cache_enabled) { 159 if (!isset ($this->cache[$group _key]))160 $this->load_group_from_db($group _key);161 162 if (isset ($this->cache[$ hash])) {155 if (!isset ($this->cache[$group])) 156 $this->load_group_from_db($group); 157 158 if (isset ($this->cache[$group][$id])) { 163 159 $this->cold_cache_hits += 1; 164 return $this->cache[$ hash];165 } 166 167 $this->non_existant_objects[$ hash] = true;160 return $this->cache[$group][$id]; 161 } 162 163 $this->non_existant_objects[$group][$id] = true; 168 164 $this->cache_misses += 1; 169 165 return false; 170 166 } 171 167 172 $cache_file = $this->cache_dir.$this->get_group_dir($group _key)."/".$this->hash($hash).'.php';168 $cache_file = $this->cache_dir.$this->get_group_dir($group)."/".$this->hash($id).'.php'; 173 169 if (!file_exists($cache_file)) { 174 $this->non_existant_objects[$ hash] = true;170 $this->non_existant_objects[$group][$id] = true; 175 171 $this->cache_misses += 1; 176 172 return false; … … 186 182 } 187 183 188 $this->cache[$ hash] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER))));189 if (false === $this->cache[$ hash])190 $this->cache[$ hash] = '';184 $this->cache[$group][$id] = unserialize(base64_decode(substr(@ file_get_contents($cache_file), strlen(CACHE_SERIAL_HEADER), -strlen(CACHE_SERIAL_FOOTER)))); 185 if (false === $this->cache[$group][$id]) 186 $this->cache[$group][$id] = ''; 191 187 192 188 $this->cold_cache_hits += 1; 193 return $this->cache[$ hash];189 return $this->cache[$group][$id]; 194 190 } 195 191 … … 211 207 function load_group_from_db($group) { 212 208 return; 209 global $wpdb; 210 211 if ('category' == $group) { 212 $this->cache['category'] = array (); 213 if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) { 214 foreach ($dogs as $catt) 215 $this->cache['category'][$catt->cat_ID] = $catt; 216 } 217 } 218 213 219 } 214 220 … … 287 293 288 294 function set($id, $data, $group = 'default', $expire = '') { 289 $group_key = $this->key( '', $group );290 $hash = $this->key($id, $group_key);291 295 if (empty ($group)) 292 296 $group = 'default'; 293 294 if (NULL === $data) 297 $group = $this->maybe_localize_group( $group ); 298 299 if (NULL == $data) 295 300 $data = ''; 296 301 297 $this->cache[$ hash] = $data;298 unset ($this->non_existant_objects[$ hash]);299 $this->dirty_objects[$group _key][] = $id;302 $this->cache[$group][$id] = $data; 303 unset ($this->non_existant_objects[$group][$id]); 304 $this->dirty_objects[$group][] = $id; 300 305 301 306 return true; … … 334 339 $errors = 0; 335 340 foreach ($this->dirty_objects as $group => $ids) { 336 if ( in_array($group, $this->non_persistent_groups) )337 continue;338 339 341 $group_dir = $this->make_group_dir($group, $dir_perms); 340 342 341 343 $ids = array_unique($ids); 342 344 foreach ($ids as $id) { 343 $hash = $this->key($id, $group); 344 $cache_file = $group_dir.$this->hash($hash).'.php'; 345 $cache_file = $group_dir.$this->hash($id).'.php'; 345 346 346 347 // Remove the cache file if the key is not set. 347 if (!isset ($this->cache[$ hash])) {348 if (!isset ($this->cache[$group][$id])) { 348 349 if (file_exists($cache_file)) 349 350 @ unlink($cache_file); … … 352 353 353 354 $temp_file = tempnam($group_dir, 'tmp'); 354 $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$ hash])).CACHE_SERIAL_FOOTER;355 $serial = CACHE_SERIAL_HEADER.base64_encode(serialize($this->cache[$group][$id])).CACHE_SERIAL_FOOTER; 355 356 $fd = @fopen($temp_file, 'w'); 356 357 if ( false === $fd ) { … … 381 382 function stats() { 382 383 echo "<p>"; 383 echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br />";384 echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br />";385 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";384 echo "<strong>Cold Cache Hits:</strong> {$this->cold_cache_hits}<br/>"; 385 echo "<strong>Warm Cache Hits:</strong> {$this->warm_cache_hits}<br/>"; 386 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br/>"; 386 387 echo "</p>"; 387 388 388 389 foreach ($this->cache as $group => $cache) { 389 390 echo "<p>"; 390 echo "<strong>Group:</strong> $group<br />";391 echo "<strong>Group:</strong> $group<br/>"; 391 392 echo "<strong>Cache:</strong>"; 392 393 echo "<pre>";
