/
object-cache.php
executable file
·431 lines (332 loc) · 10.9 KB
/
object-cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
function wp_cache_add($key, $data, $flag = '', $expire = 0) {
global $wp_object_cache;
return $wp_object_cache->add($key, $data, $flag, $expire);
}
function wp_cache_incr($key, $n = 1, $flag = '') {
global $wp_object_cache;
return $wp_object_cache->incr($key, $n, $flag);
}
function wp_cache_decr($key, $n = 1, $flag = '') {
global $wp_object_cache;
return $wp_object_cache->decr($key, $n, $flag);
}
function wp_cache_close() {
global $wp_object_cache;
return $wp_object_cache->close();
}
function wp_cache_delete($id, $flag = '') {
global $wp_object_cache;
return $wp_object_cache->delete($id, $flag);
}
function wp_cache_flush() {
global $wp_object_cache;
return $wp_object_cache->flush();
}
function wp_cache_get($id, $flag = '') {
global $wp_object_cache;
return $wp_object_cache->get($id, $flag);
}
function wp_cache_init() {
global $wp_object_cache;
if ( !is_object($wp_object_cache) )
$wp_object_cache = new object_cache;
}
function wp_cache_replace($key, $data, $flag = '', $expire = 0) {
global $wp_object_cache;
return $wp_object_cache->replace($key, $data, $flag, $expire);
}
function wp_cache_set($key, $data, $flag = '', $expire = 0) {
global $wp_object_cache;
if ( !defined('WP_INSTALLING') )
return $wp_object_cache->set($key, $data, $flag, $expire);
else
return $wp_object_cache->delete($key, $flag);
}
function wp_cache_add_global_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_global_groups($groups);
}
function wp_cache_add_non_persistent_groups( $groups ) {
global $wp_object_cache;
$wp_object_cache->add_non_persistent_groups($groups);
}
class object_cache {
var $global_groups = array('users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss');
var $no_mc_groups = array('comment', 'counts');
var $autoload_groups = array('options');
var $cache = array();
var $mc = array();
var $default_expiration = 0;
var $flush = false;
var $cache_hits = 0;
var $cache_misses = 0;
function add($id, $data, $group = 'default', $expire = 0) {
$key = $this->key($id, $group);
if ( in_array($group, $this->no_mc_groups) || $this->flush && $group != 'transient' ) {
$this->cache[$key] = $data;
return true;
} elseif ( isset($this->cache[$key]) && $this->cache[$key] !== false ) {
return false;
}
$mc =& $this->get_mc($group);
$expire = ($expire == 0) ? $this->default_expiration : $expire;
# http://core.trac.wordpress.org/ticket/11431
if ( $group == 'terms' && !is_numeric($id) && !$expire )
$expire = 86400;
$result = $mc->add($key, $data, false, $expire);
if ( false !== $result )
$this->cache[$key] = $data;
return $result;
}
function add_global_groups($groups) {
if ( ! is_array($groups) )
$groups = (array) $groups;
$this->global_groups = array_merge($this->global_groups, $groups);
$this->global_groups = array_unique($this->global_groups);
}
function add_non_persistent_groups($groups) {
if ( ! is_array($groups) )
$groups = (array) $groups;
$this->no_mc_groups = array_merge($this->no_mc_groups, $groups);
$this->no_mc_groups = array_unique($this->no_mc_groups);
}
function incr($id, $n, $group) {
$key = $this->key($id, $group);
$mc =& $this->get_mc($group);
return $mc->increment($key, $n);
}
function decr($id, $n, $group) {
$key = $this->key($id, $group);
$mc =& $this->get_mc($group);
return $mc->decrement($key, $n);
}
function close() {
foreach ( $this->mc as $bucket => $mc )
$mc->close();
}
function delete($id, $group = 'default') {
$key = $this->key($id, $group);
if ( in_array($group, $this->no_mc_groups) ) {
unset($this->cache[$key]);
return true;
}
$mc =& $this->get_mc($group);
$result = $mc->delete($key);
if ( false !== $result )
unset($this->cache[$key]);
return $result;
}
function flush() {
static $done = false;
if ( $done )
return false;
# can't flush if WP isn't loaded
if ( !function_exists('get_option') )
return false;
$done = true;
global $wpdb;
$this->flush = true;
if ( method_exists('static_cache', 'disable') )
static_cache::disable();
# flush posts
$posts = $wpdb->get_results("SELECT ID, post_title, post_name, post_date, post_type, post_status, post_author, post_parent FROM $wpdb->posts WHERE post_status IN ('publish', 'private') OR post_type = 'attachment'");
# force WP widgets to flush
wp_cache_delete('widget_recent_posts', 'widget');
wp_cache_delete('recent_comments', 'widget');
wp_cache_delete('get_calendar', 'calendar');
wp_cache_delete('wp_get_archives', 'general');
wp_cache_delete('all_page_ids', 'posts');
wp_cache_delete('get_pages', 'posts');
$post_ids = array();
foreach ( $posts as $post ) {
$post_ids[] = $post->ID;
$this->delete($post->ID, 'posts');
$this->delete($post->ID, 'post_meta');
clean_object_term_cache($post->ID, 'post');
do_action('clean_post_cache', $post->ID);
# fill a temporary bucket so as to handle permalinks
$key = $this->key($post->ID, 'posts');
$this->cache[$key] = $post;
if ( class_exists('sem_cache') && $post->post_type == 'post' ) {
sem_cache::do_flush_author($post->post_author);
sem_cache::do_flush_date($post->post_date);
}
}
unset($posts);
if ( class_exists('sem_cache') ) {
# flush get_permalink() intensive stuff before flushing terms
foreach ( $post_ids as $post_id )
sem_cache::do_flush_post($post_id);
# flush home
sem_cache::do_flush_home();
}
# flush terms
$terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE count > 0");
$taxonomies = array();
$term_ids = array();
if ( class_exists('sem_cache') ) {
foreach ( $terms as $term )
sem_cache::do_flush_term($term->term_id, $term->taxonomy);
}
foreach ( $terms as $term ) {
$taxonomies[] = $term->taxonomy;
$term_ids[] = $term->term_id;
$this->delete($term->term_id, $term->taxonomy);
}
$taxonomies = array_unique($taxonomies);
foreach ( $taxonomies as $taxonomy ) {
$this->delete('all_ids', $taxonomy);
$this->delete('get', $taxonomy);
delete_option("{$taxonomy}_children");
do_action('clean_term_cache', $term_ids, $taxonomy);
}
$this->delete('last_changed', 'terms');
unset($terms);
# flush users
$user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users");
foreach ( $user_ids as $user_id )
$this->delete($user_id, 'users');
unset($user_ids);
# backup key transients
$transients = array(
'update_core',
'update_plugins',
'update_themes',
'sem_memberships',
'sem_update_plugins',
'sem_update_themes',
);
$extra = array(
'feed_220431e2eb0959fa9c7fcb07c6e22632', # sem news
'feed_mod_220431e2eb0959fa9c7fcb07c6e22632', # sem_news timeout
);
foreach ( array_merge($transients, $extra) as $var )
$$var = get_transient($var);
# flush options
$options = $wpdb->get_col("SELECT option_name FROM $wpdb->options");
foreach ( $options as $option ) {
if ( !in_array($option, array_merge($transients, $extra)) ) {
if ( preg_match("/^_transient_/", $option) )
delete_option($option);
else
$this->delete($option, 'options');
}
}
$this->delete('notoptions', 'options');
$this->delete('alloptions', 'options');
unset($options);
# restore key transients
foreach ( $transients as $var ) {
if ( $$var !== false )
set_transient($var, $$var);
}
if ( $feed_220431e2eb0959fa9c7fcb07c6e22632 !== false ) {
$var = 'feed_220431e2eb0959fa9c7fcb07c6e22632';
set_transient($var, $$var, min(3600, cache_timeout));
$var = 'feed_mod_220431e2eb0959fa9c7fcb07c6e22632';
set_transient($var, time(), min(3600, cache_timeout));
}
return true;
}
function get($id, $group = 'default') {
$key = $this->key($id, $group);
$mc =& $this->get_mc($group);
if ( isset($this->cache[$key]) ) {
$value = $this->cache[$key];
$this->cache_hits++;
} elseif ( in_array($group, $this->no_mc_groups) ) {
$value = false;
} else {
$value = $mc->get($key);
if ( $value !== false || ( $id == 'notoptions' && $group == 'options') )
$this->cache_hits++;
else
$this->cache_misses++;
}
if ( is_null($value) )
$value = false;
$this->cache[$key] = $value;
if ( 'checkthedatabaseplease' === $value )
$value = false;
return $value;
}
function key($key, $group) {
if ( empty($group) )
$group = 'default';
if ( false !== array_search($group, $this->global_groups) )
$prefix = $this->global_prefix;
else
$prefix = $this->blog_prefix;
return preg_replace('/\s+/', '', "$prefix$group:$key");
}
function replace($id, $data, $group = 'default', $expire = 0) {
$key = $this->key($id, $group);
if ( in_array($group, $this->no_mc_groups) || $this->flush && $group != 'transient' ) {
$this->cache[$key] = $data;
return true;
}
$expire = ($expire == 0) ? $this->default_expiration : $expire;
$mc =& $this->get_mc($group);
$result = $mc->replace($key, $data, false, $expire);
if ( false !== $result )
$this->cache[$key] = $data;
return $result;
}
function set($id, $data, $group = 'default', $expire = 0) {
$key = $this->key($id, $group);
if ( isset($this->cache[$key]) && ('checkthedatabaseplease' === $this->cache[$key]) )
return false;
$this->cache[$key] = $data;
if ( in_array($group, $this->no_mc_groups) || $this->flush && $group != 'transient' )
return true;
$expire = ($expire == 0) ? $this->default_expiration : $expire;
$mc =& $this->get_mc($group);
$result = $mc->set($key, $data, false, $expire);
return $result;
}
function &get_mc($group) {
if ( isset($this->mc[$group]) )
return $this->mc[$group];
return $this->mc['default'];
}
function failure_callback($host, $port) {
//error_log("Connection failure for $host:$port\n", 3, '/tmp/memcached.txt');
}
public function __construct() {
global $memcached_servers;
if ( isset($memcached_servers) )
$buckets = $memcached_servers;
else
$buckets = array('127.0.0.1');
reset($buckets);
if ( is_int(key($buckets)) )
$buckets = array('default' => $buckets);
foreach ( $buckets as $bucket => $servers ) {
$this->mc[$bucket] = new Memcache();
foreach ( $servers as $server ) {
$server = explode(':', $server);
if ( count($server) == 2 ) {
list($node, $port) = $server;
} else {
$node = current($server);
$port = false;
}
if ( !$port )
$port = ini_get('memcache.default_port');
$port = intval($port);
if ( !$port )
$port = 11211;
$this->mc[$bucket]->addServer($node, $port, true, 1, 1, 15, true, array($this, 'failure_callback'));
$this->mc[$bucket]->setCompressThreshold(20000, 0.2);
}
}
global $table_prefix;
$this->blog_prefix = DB_NAME . ':' . $table_prefix;
if ( function_exists('is_site_admin') || defined('CUSTOM_USER_TABLE') && defined('CUSTOM_USER_META_TABLE') )
$this->global_prefix = DB_NAME;
else
$this->global_prefix = $this->blog_prefix;
}
} # object_cache
$wp_object_cache = new object_cache;