| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
function wp_cache_add($key, $data, $flag = '', $expire = 0) { |
|---|
| 16 |
global $wp_object_cache; |
|---|
| 17 |
|
|---|
| 18 |
return $wp_object_cache->add($key, $data, $flag, $expire); |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
function wp_cache_incr($key, $n = 1, $flag = '') { |
|---|
| 22 |
global $wp_object_cache; |
|---|
| 23 |
|
|---|
| 24 |
return $wp_object_cache->incr($key, $n, $flag); |
|---|
| 25 |
} |
|---|
| 26 |
|
|---|
| 27 |
function wp_cache_decr($key, $n = 1, $flag = '') { |
|---|
| 28 |
global $wp_object_cache; |
|---|
| 29 |
|
|---|
| 30 |
return $wp_object_cache->decr($key, $n, $flag); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
function wp_cache_close() { |
|---|
| 34 |
global $wp_object_cache; |
|---|
| 35 |
|
|---|
| 36 |
return $wp_object_cache->close(); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
function wp_cache_delete($id, $flag = '') { |
|---|
| 40 |
global $wp_object_cache; |
|---|
| 41 |
|
|---|
| 42 |
return $wp_object_cache->delete($id, $flag); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
function wp_cache_flush() { |
|---|
| 46 |
global $wp_object_cache; |
|---|
| 47 |
|
|---|
| 48 |
return $wp_object_cache->flush(); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
function wp_cache_get($id, $flag = '') { |
|---|
| 52 |
global $wp_object_cache; |
|---|
| 53 |
|
|---|
| 54 |
return $wp_object_cache->get($id, $flag); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
function wp_cache_init() { |
|---|
| 58 |
global $wp_object_cache; |
|---|
| 59 |
|
|---|
| 60 |
$wp_object_cache = new WP_Object_Cache(); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
function wp_cache_replace($key, $data, $flag = '', $expire = 0) { |
|---|
| 64 |
global $wp_object_cache; |
|---|
| 65 |
|
|---|
| 66 |
return $wp_object_cache->replace($key, $data, $flag, $expire); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
function wp_cache_set($key, $data, $flag = '', $expire = 0) { |
|---|
| 70 |
global $wp_object_cache; |
|---|
| 71 |
|
|---|
| 72 |
if ( defined('WP_INSTALLING') == false ) |
|---|
| 73 |
return $wp_object_cache->set($key, $data, $flag, $expire); |
|---|
| 74 |
else |
|---|
| 75 |
return true; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
function wp_cache_add_global_groups( $groups ) { |
|---|
| 79 |
global $wp_object_cache; |
|---|
| 80 |
|
|---|
| 81 |
$wp_object_cache->add_global_groups($groups); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
function wp_cache_add_non_persistent_groups( $groups ) { |
|---|
| 85 |
global $wp_object_cache; |
|---|
| 86 |
|
|---|
| 87 |
$wp_object_cache->add_non_persistent_groups($groups); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
class WP_Object_Cache { |
|---|
| 91 |
var $global_groups = array ('users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss'); |
|---|
| 92 |
|
|---|
| 93 |
var $no_mc_groups = array( 'comment', 'counts' ); |
|---|
| 94 |
|
|---|
| 95 |
var $autoload_groups = array ('options'); |
|---|
| 96 |
|
|---|
| 97 |
var $cache = array(); |
|---|
| 98 |
var $mc = array(); |
|---|
| 99 |
var $stats = array(); |
|---|
| 100 |
var $group_ops = array(); |
|---|
| 101 |
|
|---|
| 102 |
var $cache_enabled = true; |
|---|
| 103 |
var $default_expiration = 0; |
|---|
| 104 |
|
|---|
| 105 |
function add($id, $data, $group = 'default', $expire = 0) { |
|---|
| 106 |
$key = $this->key($id, $group); |
|---|
| 107 |
|
|---|
| 108 |
if ( in_array($group, $this->no_mc_groups) ) { |
|---|
| 109 |
$this->cache[$key] = $data; |
|---|
| 110 |
return true; |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
$mc =& $this->get_mc($group); |
|---|
| 114 |
$expire = ($expire == 0) ? $this->default_expiration : $expire; |
|---|
| 115 |
$result = $mc->add($key, $data, false, $expire); |
|---|
| 116 |
@ ++$this->stats['add']; |
|---|
| 117 |
$this->group_ops[$group][] = "add $id"; |
|---|
| 118 |
|
|---|
| 119 |
if ( false !== $result ) |
|---|
| 120 |
$this->cache[$key] = $data; |
|---|
| 121 |
return $result; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
function add_global_groups($groups) { |
|---|
| 125 |
if ( ! is_array($groups) ) |
|---|
| 126 |
$groups = (array) $groups; |
|---|
| 127 |
|
|---|
| 128 |
$this->global_groups = array_merge($this->global_groups, $groups); |
|---|
| 129 |
$this->global_groups = array_unique($this->global_groups); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
function add_non_persistent_groups($groups) { |
|---|
| 133 |
if ( ! is_array($groups) ) |
|---|
| 134 |
$groups = (array) $groups; |
|---|
| 135 |
|
|---|
| 136 |
$this->no_mc_groups = array_merge($this->no_mc_groups, $groups); |
|---|
| 137 |
$this->no_mc_groups = array_unique($this->no_mc_groups); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
function incr($id, $n, $group) { |
|---|
| 141 |
$key = $this->key($id, $group); |
|---|
| 142 |
$mc =& $this->get_mc($group); |
|---|
| 143 |
|
|---|
| 144 |
return $mc->increment($key, $n); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
function decr($id, $n, $group) { |
|---|
| 148 |
$key = $this->key($id, $group); |
|---|
| 149 |
$mc =& $this->get_mc($group); |
|---|
| 150 |
|
|---|
| 151 |
return $mc->decrement($key, $n); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
function close() { |
|---|
| 155 |
|
|---|
| 156 |
foreach ( $this->mc as $bucket => $mc ) |
|---|
| 157 |
$mc->close(); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
function delete($id, $group = 'default') { |
|---|
| 161 |
$key = $this->key($id, $group); |
|---|
| 162 |
|
|---|
| 163 |
if ( in_array($group, $this->no_mc_groups) ) { |
|---|
| 164 |
unset($this->cache[$key]); |
|---|
| 165 |
return true; |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
$mc =& $this->get_mc($group); |
|---|
| 169 |
|
|---|
| 170 |
$result = $mc->delete($key); |
|---|
| 171 |
|
|---|
| 172 |
@ ++$this->stats['delete']; |
|---|
| 173 |
$this->group_ops[$group][] = "delete $id"; |
|---|
| 174 |
|
|---|
| 175 |
if ( false !== $result ) |
|---|
| 176 |
unset($this->cache[$key]); |
|---|
| 177 |
|
|---|
| 178 |
return $result; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
function flush() { |
|---|
| 182 |
return true; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
function get($id, $group = 'default') { |
|---|
| 186 |
$key = $this->key($id, $group); |
|---|
| 187 |
$mc =& $this->get_mc($group); |
|---|
| 188 |
|
|---|
| 189 |
if ( isset($this->cache[$key]) ) |
|---|
| 190 |
$value = $this->cache[$key]; |
|---|
| 191 |
else if ( in_array($group, $this->no_mc_groups) ) |
|---|
| 192 |
$value = false; |
|---|
| 193 |
else |
|---|
| 194 |
$value = $mc->get($key); |
|---|
| 195 |
|
|---|
| 196 |
@ ++$this->stats['get']; |
|---|
| 197 |
$this->group_ops[$group][] = "get $id"; |
|---|
| 198 |
|
|---|
| 199 |
if ( NULL === $value ) |
|---|
| 200 |
$value = false; |
|---|
| 201 |
|
|---|
| 202 |
$this->cache[$key] = $value; |
|---|
| 203 |
|
|---|
| 204 |
if ( 'checkthedatabaseplease' == $value ) |
|---|
| 205 |
$value = false; |
|---|
| 206 |
|
|---|
| 207 |
return $value; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
function get_multi( $groups ) { |
|---|
| 211 |
|
|---|
| 212 |
format: $get['group-name'] = array( 'key1', 'key2' ); |
|---|
| 213 |
*/ |
|---|
| 214 |
$return = array(); |
|---|
| 215 |
foreach ( $groups as $group => $ids ) { |
|---|
| 216 |
$mc =& $this->get_mc($group); |
|---|
| 217 |
foreach ( $ids as $id ) { |
|---|
| 218 |
$key = $this->key($id, $group); |
|---|
| 219 |
if ( isset($this->cache[$key]) ) { |
|---|
| 220 |
$return[$key] = $this->cache[$key]; |
|---|
| 221 |
continue; |
|---|
| 222 |
} else if ( in_array($group, $this->no_mc_groups) ) { |
|---|
| 223 |
$return[$key] = false; |
|---|
| 224 |
continue; |
|---|
| 225 |
} else { |
|---|
| 226 |
$return[$key] = $mc->get($key); |
|---|
| 227 |
} |
|---|
| 228 |
} |
|---|
| 229 |
if ( $to_get ) { |
|---|
| 230 |
$vals = $mc->get_multi( $to_get ); |
|---|
| 231 |
$return = array_merge( $return, $vals ); |
|---|
| 232 |
} |
|---|
| 233 |
} |
|---|
| 234 |
@ ++$this->stats['get_multi']; |
|---|
| 235 |
$this->group_ops[$group][] = "get_multi $id"; |
|---|
| 236 |
$this->cache = array_merge( $this->cache, $return ); |
|---|
| 237 |
return $return; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
function key($key, $group) { |
|---|
| 241 |
global $blog_id; |
|---|
| 242 |
|
|---|
| 243 |
if ( empty($group) ) |
|---|
| 244 |
$group = 'default'; |
|---|
| 245 |
|
|---|
| 246 |
if (false !== array_search($group, $this->global_groups)) |
|---|
| 247 |
$prefix = ''; |
|---|
| 248 |
else |
|---|
| 249 |
$prefix = $blog_id . ':'; |
|---|
| 250 |
|
|---|
| 251 |
return preg_replace('/\s+/', '', "$prefix$group:$key"); |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
function replace($id, $data, $group = 'default', $expire = 0) { |
|---|
| 255 |
$key = $this->key($id, $group); |
|---|
| 256 |
$expire = ($expire == 0) ? $this->default_expiration : $expire; |
|---|
| 257 |
$mc =& $this->get_mc($group); |
|---|
| 258 |
$result = $mc->replace($key, $data, false, $expire); |
|---|
| 259 |
if ( false !== $result ) |
|---|
| 260 |
$this->cache[$key] = $data; |
|---|
| 261 |
return $result; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
function set($id, $data, $group = 'default', $expire = 0) { |
|---|
| 265 |
$key = $this->key($id, $group); |
|---|
| 266 |
if ( isset($this->cache[$key]) && ('checkthedatabaseplease' == $this->cache[$key]) ) |
|---|
| 267 |
return false; |
|---|
| 268 |
$this->cache[$key] = $data; |
|---|
| 269 |
|
|---|
| 270 |
if ( in_array($group, $this->no_mc_groups) ) |
|---|
| 271 |
return true; |
|---|
| 272 |
|
|---|
| 273 |
$expire = ($expire == 0) ? $this->default_expiration : $expire; |
|---|
| 274 |
$mc =& $this->get_mc($group); |
|---|
| 275 |
$result = $mc->set($key, $data, false, $expire); |
|---|
| 276 |
|
|---|
| 277 |
return $result; |
|---|
| 278 |
} |
|---|
| 279 |
|
|---|
| 280 |
function colorize_debug_line($line) { |
|---|
| 281 |
$colors = array( |
|---|
| 282 |
'get' => 'green', |
|---|
| 283 |
'set' => 'purple', |
|---|
| 284 |
'add' => 'blue', |
|---|
| 285 |
'delete' => 'red'); |
|---|
| 286 |
|
|---|
| 287 |
$cmd = substr($line, 0, strpos($line, ' ')); |
|---|
| 288 |
|
|---|
| 289 |
$cmd2 = "<span style='color:{$colors[$cmd]}'>$cmd</span>"; |
|---|
| 290 |
|
|---|
| 291 |
return $cmd2 . substr($line, strlen($cmd)) . "\n"; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
function stats() { |
|---|
| 295 |
echo "<p>\n"; |
|---|
| 296 |
foreach ( $this->stats as $stat => $n ) { |
|---|
| 297 |
echo "<strong>$stat</strong> $n"; |
|---|
| 298 |
echo "<br/>\n"; |
|---|
| 299 |
} |
|---|
| 300 |
echo "</p>\n"; |
|---|
| 301 |
echo "<h3>Memcached:</h3>"; |
|---|
| 302 |
foreach ( $this->group_ops as $group => $ops ) { |
|---|
| 303 |
if ( !isset($_GET['debug_queries']) && 500 < count($ops) ) { |
|---|
| 304 |
$ops = array_slice( $ops, 0, 500 ); |
|---|
| 305 |
echo "<big>Too many to show! <a href='" . add_query_arg( 'debug_queries', 'true' ) . "'>Show them anyway</a>.</big>\n"; |
|---|
| 306 |
} |
|---|
| 307 |
echo "<h4>$group commands</h4>"; |
|---|
| 308 |
echo "<pre>\n"; |
|---|
| 309 |
$lines = array(); |
|---|
| 310 |
foreach ( $ops as $op ) { |
|---|
| 311 |
$lines[] = $this->colorize_debug_line($op); |
|---|
| 312 |
} |
|---|
| 313 |
print_r($lines); |
|---|
| 314 |
echo "</pre>\n"; |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
if ( $this->debug ) |
|---|
| 318 |
var_dump($this->memcache_debug); |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
function &get_mc($group) { |
|---|
| 322 |
if ( isset($this->mc[$group]) ) |
|---|
| 323 |
return $this->mc[$group]; |
|---|
| 324 |
return $this->mc['default']; |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
function failure_callback($host, $port) { |
|---|
| 328 |
|
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
function WP_Object_Cache() { |
|---|
| 332 |
global $memcached_servers; |
|---|
| 333 |
|
|---|
| 334 |
if ( isset($memcached_servers) ) |
|---|
| 335 |
$buckets = $memcached_servers; |
|---|
| 336 |
else |
|---|
| 337 |
$buckets = array('default' => array('127.0.0.1:11211')); |
|---|
| 338 |
|
|---|
| 339 |
foreach ( $buckets as $bucket => $servers) { |
|---|
| 340 |
$this->mc[$bucket] = new Memcache(); |
|---|
| 341 |
foreach ( $servers as $server ) { |
|---|
| 342 |
list ( $node, $port ) = explode(':', $server); |
|---|
| 343 |
$this->mc[$bucket]->addServer($node, $port, true, 1, 1, 15, true, array($this, 'failure_callback')); |
|---|
| 344 |
$this->mc[$bucket]->setCompressThreshold(20000, 0.2); |
|---|
| 345 |
} |
|---|
| 346 |
} |
|---|
| 347 |
} |
|---|
| 348 |
} |
|---|
| 349 |
?> |
|---|
| 350 |
|
|---|