forked from Automattic/batcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced-cache.php
300 lines (233 loc) · 11.2 KB
/
advanced-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
<?php
// nananananananananananananananana BATCACHE!!!
function batcache_cancel() {
global $batcache;
if ( is_object($batcache) )
$batcache->cancel = true;
}
class batcache {
// This is the base configuration. You can edit these variables or move them into your wp-config.php file.
var $max_age = 300; // Expire batcache items aged this many seconds (zero to disable batcache)
var $remote = 0; // Zero disables sending buffers to remote datacenters (req/sec is never sent)
var $times = 2; // Only batcache a page after it is accessed this many times... (two or more)
var $seconds = 120; // ...in this many seconds (zero to ignore this and use batcache immediately)
var $group = 'batcache'; // Name of memcached group. You can simulate a cache flush by changing this.
var $unique = array(); // If you conditionally serve different content, put the variable values here.
var $headers = array(); // Add headers here. These will be sent with every response from the cache.
var $uncached_headers = array('transfer-encoding'); // These headers will never be cached. Apply strtolower.
var $debug = true; // Set false to hide the batcache info <!-- comment -->
var $cache_control = true; // Set false to disable Last-Modified and Cache-Control headers
var $cancel = false; // Change this to cancel the output buffer. Use batcache_cancel();
function batcache( $settings ) {
if ( is_array( $settings ) ) foreach ( $settings as $k => $v )
$this->$k = $v;
}
function status_header( $status_header ) {
$this->status_header = $status_header;
return $status_header;
}
function configure_groups() {
// Configure the memcached client
if ( ! $this->remote )
if ( function_exists('wp_cache_add_no_remote_groups') )
wp_cache_add_no_remote_groups(array($this->group));
if ( function_exists('wp_cache_add_global_groups') )
wp_cache_add_global_groups(array($this->group));
}
// Defined here because timer_stop() calls number_format_i18n()
function timer_stop($display = 0, $precision = 3) {
global $timestart, $timeend;
$mtime = microtime();
$mtime = explode(' ',$mtime);
$mtime = $mtime[1] + $mtime[0];
$timeend = $mtime;
$timetotal = $timeend-$timestart;
$r = number_format($timetotal, $precision);
if ( $display )
echo $r;
return $r;
}
function ob($output) {
if ( $this->cancel !== false )
return $output;
// PHP5 and objects disappearing before output buffers?
wp_cache_init();
// Remember, $wp_object_cache was clobbered in wp-settings.php so we have to repeat this.
$this->configure_groups();
// Do not batcache blank pages (usually they are HTTP redirects)
$output = trim($output);
if ( empty($output) )
return;
// Construct and save the batcache
$cache = array(
'output' => $output,
'time' => time(),
'timer' => $this->timer_stop(false, 3),
'status_header' => $this->status_header,
'version' => $this->url_version
);
if ( function_exists( 'apache_response_headers' ) ) {
$cache['headers'] = apache_response_headers();
if ( !empty( $this->uncached_headers ) ) foreach ( $cache['headers'] as $header => $value ) {
if ( in_array( strtolower( $header ), $this->uncached_headers ) )
unset( $cache['headers'][$header] );
}
}
wp_cache_set($this->key, $cache, $this->group, $this->max_age + $this->seconds + 30);
// Unlock regeneration
wp_cache_delete("{$this->url_key}_genlock", $this->group);
if ( $this->cache_control ) {
header('Last-Modified: ' . date('r', $cache['time']), true);
header("Cache-Control: max-age=$this->max_age, must-revalidate", false);
}
if ( !empty($this->headers) ) foreach ( $this->headers as $k => $v ) {
if ( is_array( $v ) )
header("{$v[0]}: {$v[1]}", false);
else
header("$k: $v", true);
}
// Add some debug info just before </head>
if ( $this->debug ) {
$tag = "<!--\n\tgenerated in " . $cache['timer'] . " seconds\n\t" . strlen(serialize($cache)) . " bytes batcached for " . $this->max_age . " seconds\n-->\n";
if ( false !== $tag_position = strpos($output, '</head>') ) {
$tag = "<!--\n\tgenerated in " . $cache['timer'] . " seconds\n\t" . strlen(serialize($cache)) . " bytes batcached for " . $this->max_age . " seconds\n-->\n";
$output = substr($output, 0, $tag_position) . $tag . substr($output, $tag_position);
}
}
// Pass output to next ob handler
return $output;
}
}
global $batcache;
// Pass in the global variable which may be an array of settings to override defaults.
$batcache = new batcache($batcache);
if ( ! defined( 'WP_CONTENT_DIR' ) )
return;
// Never batcache interactive scripts or API endpoints.
if ( in_array(
basename( $_SERVER['SCRIPT_FILENAME'] ),
array(
'wp-app.php',
'xmlrpc.php',
'ms-files.php',
) ) )
return;
// Never batcache WP javascript generators
if ( strstr( $_SERVER['SCRIPT_FILENAME'], 'wp-includes/js' ) )
return;
// Never batcache when POST data is present.
if ( ! empty( $GLOBALS['HTTP_RAW_POST_DATA'] ) || ! empty( $_POST ) )
return;
// Never batcache when cookies indicate a cache-exempt visitor.
if ( is_array( $_COOKIE) && ! empty( $_COOKIE ) )
foreach ( array_keys( $_COOKIE ) as $batcache->cookie )
if ( $batcache->cookie != 'wordpress_test_cookie' && ( substr( $batcache->cookie, 0, 2 ) == 'wp' || substr( $batcache->cookie, 0, 9 ) == 'wordpress' || substr( $batcache->cookie, 0, 14 ) == 'comment_author' ) )
return;
if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) )
return;
wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here.
if ( ! is_object( $wp_object_cache ) )
return;
// Now that the defaults are set, you might want to use different settings under certain conditions.
/* Example: if your documents have a mobile variant (a different document served by the same URL) you must tell batcache about the variance. Otherwise you might accidentally cache the mobile version and serve it to desktop users, or vice versa.
$batcache->unique['mobile'] = is_mobile_user_agent();
*/
/* Example: never batcache for this host
if ( $_SERVER['HTTP_HOST'] == 'do-not-batcache-me.com' )
return;
*/
/* Example: batcache everything on this host regardless of traffic level
if ( $_SERVER['HTTP_HOST'] == 'always-batcache-me.com' )
return;
*/
/* Example: If you sometimes serve variants dynamically (e.g. referrer search term highlighting) you probably don't want to batcache those variants. Remember this code is run very early in wp-settings.php so plugins are not yet loaded. You will get a fatal error if you try to call an undefined function. Either include your plugin now or define a test function in this file.
if ( include_once( 'plugins/searchterm-highlighter.php') && referrer_has_search_terms() )
return;
*/
// Disabled
if ( $batcache->max_age < 1 )
return;
// Make sure we can increment. If not, turn off the traffic sensor.
if ( ! method_exists( $GLOBALS['wp_object_cache'], 'incr' ) )
$batcache->times = 0;
// Necessary to prevent clients using cached version after login cookies set. If this is a problem, comment it out and remove all Last-Modified headers.
header('Vary: Cookie', false);
// Things that define a unique page.
if ( isset( $_SERVER['QUERY_STRING'] ) )
parse_str($_SERVER['QUERY_STRING'], $batcache->query);
$batcache->keys = array(
'host' => $_SERVER['HTTP_HOST'],
'path' => ( $batcache->pos = strpos($_SERVER['REQUEST_URI'], '?') ) ? substr($_SERVER['REQUEST_URI'], 0, $batcache->pos) : $_SERVER['REQUEST_URI'],
'query' => $batcache->query,
'extra' => $batcache->unique
);
$batcache->configure_groups();
// Generate the batcache key
$batcache->key = md5(serialize($batcache->keys));
// Generate the traffic threshold measurement key
$batcache->req_key = $batcache->key . '_req';
// Get the batcache
$batcache->cache = wp_cache_get($batcache->key, $batcache->group);
// Are we only caching frequently-requested pages?
if ( $batcache->seconds < 1 || $batcache->times < 2 ) {
$batcache->do = true;
} else {
// No batcache item found, or ready to sample traffic again at the end of the batcache life?
if ( !is_array($batcache->cache) || time() >= $batcache->cache['time'] + $batcache->max_age - $batcache->seconds ) {
wp_cache_add($batcache->req_key, 0, $batcache->group);
$batcache->requests = wp_cache_incr($batcache->req_key, 1, $batcache->group);
if ( $batcache->requests >= $batcache->times )
$batcache->do = true;
else
$batcache->do = false;
}
}
// Recreate the permalink from the URL
$batcache->permalink = 'http://' . $batcache->keys['host'] . $batcache->keys['path'] . ( isset($batcache->keys['query']['p']) ? "?p=" . $batcache->keys['query']['p'] : '' );
$batcache->url_key = md5($batcache->permalink);
$batcache->url_version = (int) wp_cache_get("{$batcache->url_key}_version", $batcache->group);
// If the document has been updated and we are the first to notice, regenerate it.
if ( $batcache->do !== false && isset($batcache->cache['version']) && $batcache->cache['version'] < $batcache->url_version )
$batcache->genlock = wp_cache_add("{$batcache->url_key}_genlock", 1, $batcache->group);
else $batcache->genlock = 0;
// Did we find a batcached page that hasn't expired?
if ( isset($batcache->cache['time']) && ! $batcache->genlock && time() < $batcache->cache['time'] + $batcache->max_age ) {
// Issue "304 Not Modified" only if the dates match exactly.
if ( $batcache->cache_control && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ) {
$since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
if ( $batcache->cache['time'] == $since ) {
header('Last-Modified: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'], true, 304);
exit;
}
}
// Use the batcache save time for Last-Modified so we can issue "304 Not Modified"
if ( $batcache->cache_control ) {
header('Last-Modified: ' . date('r', $batcache->cache['time']), true);
header('Cache-Control: max-age=' . ($batcache->max_age - time() + $batcache->cache['time']) . ', must-revalidate', true);
}
// Add some debug info just before </head>
if ( $batcache->debug ) {
if ( false !== $tag_position = strpos($batcache->cache['output'], '</head>') ) {
$tag = "<!--\n\tgenerated " . (time() - $batcache->cache['time']) . " seconds ago\n\tgenerated in " . $batcache->cache['timer'] . " seconds\n\tserved from batcache in " . $batcache->timer_stop(false, 3) . " seconds\n\texpires in " . ($batcache->max_age - time() + $batcache->cache['time']) . " seconds\n-->\n";
$batcache->cache['output'] = substr($batcache->cache['output'], 0, $tag_position) . $tag . substr($batcache->cache['output'], $tag_position);
}
}
if ( !empty($batcache->cache['headers']) ) foreach ( $batcache->cache['headers'] as $k => $v )
header("$k: $v", true);
if ( !empty($batcache->headers) ) foreach ( $batcache->headers as $k => $v ) {
if ( is_array( $v ) )
header("{$v[0]}: {$v[1]}", false);
else
header("$k: $v", true);
}
if ( !empty($batcache->cache['status_header']) )
header($batcache->cache['status_header'], true);
// Have you ever heard a death rattle before?
die($batcache->cache['output']);
}
// Didn't meet the minimum condition?
if ( !$batcache->do && !$batcache->genlock )
return;
$wp_filter['status_header'][10]['batcache'] = array( 'function' => array(&$batcache, 'status_header'), 'accepted_args' => 1 );
ob_start(array(&$batcache, 'ob'));
// It is safer to omit the final PHP closing tag.