-
Notifications
You must be signed in to change notification settings - Fork 24
/
advanced-cache.php
701 lines (575 loc) · 19.5 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
<?php
/**
* Redis Cache Dropin for WordPress
*
* Create a symbolic link to this file from your wp-content directory and
* enable page caching in your wp-config.php.
*/
if ( ! defined( 'ABSPATH' ) )
die();
class Redis_Page_Cache {
private static $redis;
private static $redis_host = '127.0.0.1';
private static $redis_port = 6379;
private static $redis_db = 0;
private static $redis_auth = '';
private static $redis_persistent = false;
private static $ttl = 300;
private static $max_ttl = 3600;
private static $unique = array();
private static $headers = array();
private static $ignore_cookies = array( 'wordpress_test_cookie' );
private static $ignore_request_keys = array( 'utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign' );
private static $whitelist_cookies = null;
private static $bail_callback = false;
private static $debug = false;
private static $gzip = true;
private static $lock = false;
private static $cache = false;
private static $request_hash = '';
private static $debug_data = false;
private static $fcgi_regenerate = false;
// Flag requests and expire/delete them efficiently.
private static $flags = array();
private static $flags_expire = array();
private static $flags_delete = array();
/**
* Runs during advanced-cache.php
*/
public static function cache_init() {
// Clear caches in bulk at the end.
register_shutdown_function( array( __CLASS__, 'maybe_clear_caches' ) );
header( 'X-Pj-Cache-Status: miss' );
if ( function_exists( 'add_action' ) ) {
add_action( 'clean_post_cache', array( __CLASS__, 'clean_post_cache' ) );
add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ), 100 );
} else {
self::_add_action_compat();
}
// Parse configuration.
self::maybe_user_config();
// Make sure TEST_COOKIE is always set on a wp-login.php POST request
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-login.php' ) === 0 && strtoupper( $_SERVER['REQUEST_METHOD'] ) == 'POST' ) {
$_COOKIE['wordpress_test_cookie'] = 'WP Cookie check';
}
// Some things just don't need to be cached.
if ( self::maybe_bail() )
return;
// Clean up request variables.
self::clean_request();
$request_hash = array(
'request' => self::parse_request_uri( $_SERVER['REQUEST_URI'] ),
'host' => ! empty( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '',
'https' => ! empty( $_SERVER['HTTPS'] ) ? $_SERVER['HTTPS'] : '',
'method' => $_SERVER['REQUEST_METHOD'],
'unique' => self::$unique,
'cookies' => self::parse_cookies( $_COOKIE ),
);
// Make sure requests with Authorization: headers are unique.
if ( ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) {
$request_hash['unique']['pj-auth-header'] = $_SERVER['HTTP_AUTHORIZATION'];
}
if ( self::$debug ) {
self::$debug_data = array( 'request_hash' => $request_hash );
}
// Convert to an actual hash.
self::$request_hash = md5( serialize( $request_hash ) );
unset( $request_hash );
if ( self::$debug ) {
header( 'X-Pj-Cache-Key: ' . self::$request_hash );
}
$redis = self::get_redis();
if ( ! $redis )
return;
// Look for an existing cache entry by request hash.
list( $cache, $lock ) = $redis->mGet( array(
sprintf( 'pjc-%s', self::$request_hash ),
sprintf( 'pjc-%s-lock', self::$request_hash ),
) );
// Something is in cache.
if ( is_array( $cache ) && ! empty( $cache ) ) {
$serve_cache = true;
if ( self::$debug ) {
header( 'X-Pj-Cache-Time: ' . $cache['updated'] );
header( 'X-Pj-Cache-Flags: ' . implode( ' ', $cache['flags'] ) );
}
$redis->multi();
$redis->zRangeByScore( 'pjc-expired-flags', $cache['updated'], '+inf', array( 'withscores' => true ) );
$redis->zRangeByScore( 'pjc-deleted-flags', $cache['updated'], '+inf', array( 'withscores' => true ) );
list( $expired_flags, $deleted_flags ) = $redis->exec();
$expired = $cache['updated'] + self::$ttl < time();
$deleted = false;
if ( ! empty( $cache['flags'] ) ) {
// Check whether any flags have been deleted.
if ( ! empty( $deleted_flags ) &&
count( array_intersect( $cache['flags'], array_keys( $deleted_flags ) ) ) > 0 ) {
$deleted = true;
$serve_cache = false;
}
// Check whether any flags have been expired.
if ( ! $expired && ! $deleted && ! empty( $expired_flags ) &&
count( array_intersect( $cache['flags'], array_keys( $expired_flags ) ) ) > 0 ) {
$expired = true;
}
}
// This entry is very old, consider it deleted.
if ( $serve_cache && $cache['updated'] + self::$max_ttl < time() ) {
$serve_cache = false;
$deleted = true;
}
// Cache is outdated or set to expire.
if ( $expired && $serve_cache ) {
// If it's not locked, lock it for regeneration and don't serve from cache.
if ( ! $lock ) {
$lock = $redis->set( sprintf( 'pjc-%s-lock', self::$request_hash ), true, array( 'nx', 'ex' => 30 ) );
if ( $lock ) {
if ( self::can_fcgi_regenerate() ) {
// Well, actually, if we can serve a stale copy but keep the process running
// to regenerate the cache in background without affecting the UX, that will be great!
$serve_cache = true;
self::$fcgi_regenerate = true;
} else {
$serve_cache = false;
}
}
}
}
if ( $serve_cache && $cache['gzip'] ) {
if ( function_exists( 'gzuncompress' ) && self::$gzip ) {
if ( self::$debug ) {
header( 'X-Pj-Cache-Gzip: true' );
}
$cache['output'] = gzuncompress( $cache['output'] );
} else {
$serve_cache = false;
}
}
if ( $serve_cache ) {
// If we're regenareting in background, let everyone know.
$status = ( self::$fcgi_regenerate ) ? 'expired' : 'hit';
header( 'X-Pj-Cache-Status: ' . $status );
if ( self::$debug )
header( sprintf( 'X-Pj-Cache-Expires: %d', self::$ttl - ( time() - $cache['updated'] ) ) );
// Output cached status code.
if ( ! empty( $cache['status'] ) )
http_response_code( $cache['status'] );
// Output cached headers.
if ( is_array( $cache['headers'] ) && ! empty( $cache['headers'] ) )
foreach ( $cache['headers'] as $header )
header( $header );
echo $cache['output'];
// If we can regenerate in the background, do it.
if ( self::$fcgi_regenerate ) {
fastcgi_finish_request();
pj_sapi_headers_clean();
} else {
exit;
}
}
}
// Cache it, smash it.
ob_start( array( __CLASS__, 'output_buffer' ) );
}
/**
* Returns true if we can regenerate the request in background.
*/
private static function can_fcgi_regenerate() {
return ( php_sapi_name() == 'fpm-fcgi' && function_exists( 'fastcgi_finish_request' ) && function_exists( 'pj_sapi_headers_clean' ) );
}
/**
* Initialize and/or return a Redis object.
*/
public static function get_redis() {
if ( isset( self::$redis ) )
return self::$redis;
self::$redis = false;
if ( ! class_exists( 'Redis' ) )
return self::$redis;
try {
$redis = new Redis;
if ( self::$redis_persistent ) {
$connect = $redis->pconnect( self::$redis_host, self::$redis_port );
} else {
$connect = $redis->connect( self::$redis_host, self::$redis_port );
}
if ( ! empty( self::$redis_auth ) )
$redis->auth( self::$redis_auth );
if ( ! empty( self::$redis_db ) )
$redis->select( self::$redis_db );
if ( true === $connect ) {
$redis->setOption( Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP );
self::$redis = $redis;
}
} catch ( \Exception $e ) {
error_log( $e->getMessage() );
self::$redis = false;
}
return self::$redis;
}
/**
* Take a request uri and remove ignored request keys.
*/
private static function parse_request_uri( $request_uri ) {
// Prefix the request URI with a host to avoid breaking on requests that start
// with a // which parse_url() would treat as containing the hostname.
$request_uri = 'http://null' . $request_uri;
$parsed = parse_url( $request_uri );
if ( ! empty( $parsed['query'] ) )
$query = self::remove_query_args( $parsed['query'], self::$ignore_request_keys );
$request_uri = ! empty( $parsed['path'] ) ? $parsed['path'] : '';
if ( ! empty( $query ) )
$request_uri .= '?' . $query;
return $request_uri;
}
/**
* Take some cookies and remove ones we don't care about.
*/
private static function parse_cookies( $cookies ) {
foreach ( $cookies as $key => $value ) {
if ( in_array( strtolower( $key ), self::$ignore_cookies ) ) {
unset( $cookies[ $key ] );
continue;
}
// Skip cookies beginning with _
if ( substr( $key, 0, 1 ) === '_' ) {
unset( $cookies[ $key ] );
continue;
}
}
return $cookies;
}
/**
* Remove query arguments from a query string.
*
* @param string $query_string The input query string, such as foo=bar&baz=qux
* @param array $args An array of keys to remove.
*
* @return string The resulting query string.
*/
private static function remove_query_args( $query_string, $args ) {
$regex = '#^(?:' . implode( '|', array_map( 'preg_quote', $args ) ) . ')(?:=|$)#i';
$query = explode( '&', $query_string );
foreach ( $query as $key => $value )
if ( preg_match( $regex, $value ) )
unset( $query[ $key ] );
$query_string = implode( '&', $query );
return $query_string;
}
/**
* Clean up the current request variables.
*/
private static function clean_request() {
// Strip ETag and If-Modified-Since headers.
unset( $_SERVER['HTTP_IF_NONE_MATCH'] );
unset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
// Remove ignored query vars.
if ( ! empty( $_SERVER['QUERY_STRING'] ) )
$_SERVER['QUERY_STRING'] = self::remove_query_args( $_SERVER['QUERY_STRING'], self::$ignore_request_keys );
if ( ! empty( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], '?' ) ) {
$parts = explode( '?', $_SERVER['REQUEST_URI'], 2 );
$_SERVER['REQUEST_URI'] = $parts[0];
$query_string = self::remove_query_args( $parts[1], self::$ignore_request_keys );
if ( ! empty( $query_string ) )
$_SERVER['REQUEST_URI'] .= '?' . $query_string;
}
foreach ( self::$ignore_request_keys as $key ) {
unset( $_GET[ $key ] );
unset( $_REQUEST[ $key ] );
}
// If we have a whitelist set, clear out everything that does not
// match the list, unless we're in the wp-admin (but not in admin-ajax.php).
$is_wp_admin = defined( 'WP_ADMIN' ) && WP_ADMIN;
$is_admin_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
if ( ! empty( self::$whitelist_cookies ) && ( ! $is_wp_admin || $is_admin_ajax ) ) {
foreach ( $_COOKIE as $key => $value ) {
$whitelist = false;
foreach ( self::$whitelist_cookies as $part ) {
if ( strpos( $key, $part ) === 0 ) {
$whitelist = true;
break;
}
}
if ( ! $whitelist ) {
unset( $_COOKIE[ $key ] );
}
}
}
}
/**
* Check some conditions where pages should never be cached or served from cache.
*/
private static function maybe_bail() {
// Allow an external configuration file to append to the bail method.
if ( self::$bail_callback && is_callable( self::$bail_callback ) ) {
$callback_result = call_user_func( self::$bail_callback );
if ( is_bool( $callback_result ) )
return $callback_result;
}
// Don't cache CLI requests
if ( php_sapi_name() == 'cli' )
return true;
// Don't cache POST requests.
if ( strtolower( $_SERVER['REQUEST_METHOD'] ) == 'post' )
return true;
if ( self::$ttl < 1 )
return true;
foreach ( $_COOKIE as $key => $value ) {
$key = strtolower( $key );
// Don't cache anything if these cookies are set.
foreach ( array( 'wp', 'wordpress', 'comment_author' ) as $part ) {
if ( strpos( $key, $part ) === 0 && ! in_array( $key, self::$ignore_cookies ) ) {
return true;
}
}
}
return false; // Don't bail.
}
/**
* Parse config from pj-user-config.php or $redis_page_cache_config global.
*/
private static function maybe_user_config() {
global $redis_page_cache_config;
$pj_user_config = function_exists( 'pj_user_config' ) ? pj_user_config() : array();
$keys = array(
'redis_host',
'redis_port',
'redis_auth',
'redis_db',
'redis_persistent',
'ttl',
'unique',
'ignore_cookies',
'ignore_request_keys',
'whitelist_cookies',
'bail_callback',
'debug',
'gzip',
);
foreach ( $keys as $key ) {
if ( isset( $pj_user_config['page_cache'][ $key ] ) ) {
self::$$key = $pj_user_config['page_cache'][ $key ];
} elseif ( isset( $redis_page_cache_config[ $key ] ) ) {
self::$$key = $redis_page_cache_config[ $key ];
}
}
}
/**
* Runs when the output buffer stops.
*/
public static function output_buffer( $output ) {
$cache = true;
$data = array(
'output' => $output,
'headers' => array(),
'flags' => array(),
'status' => http_response_code(),
'gzip' => false,
);
// Don't cache 5xx errors.
if ( $data['status'] >= 500 )
$cache = false;
$data['flags'] = self::$flags;
$data['flags'][] = 'url:' . self::get_url_hash();
$data['flags'] = array_unique( $data['flags'] );
// Compression.
if ( self::$gzip && function_exists( 'gzcompress' ) ) {
$data['output'] = gzcompress( $data['output'] );
$data['gzip'] = true;
}
// Clean up headers he don't want to store.
foreach ( headers_list() as $header ) {
list( $key, $value ) = explode( ':', $header, 2 );
$value = trim( $value );
// For set-cookie headers make sure we're not passing through the
// ignored cookies for this request, but if we encounter a non-ignored
// cookie being set, then don't cache this request at all.
if ( strtolower( $key ) == 'set-cookie' ) {
$cookie = explode( ';', $value, 2 );
$cookie = trim( $cookie[0] );
$cookie = wp_parse_args( $cookie );
foreach ( $cookie as $cookie_key => $cookie_value ) {
if ( ! in_array( strtolower( $cookie_key ), self::$ignore_cookies ) ) {
$cache = false;
break;
}
}
continue;
}
// Never store X-Pj-Cache-* headers in cache.
if ( strpos( strtolower( $key ), 'x-pj-cache' ) !== false )
continue;
$data['headers'][] = $header;
}
if ( self::$debug ) {
$data['debug'] = self::$debug_data;
}
$data['updated'] = time();
if ( $cache || self::$fcgi_regenerate ) {
$redis = self::get_redis();
if ( ! $redis )
return $output;
$redis = $redis->multi();
if ( $cache ) {
// Okay to cache.
$redis->set( sprintf( 'pjc-%s', self::$request_hash ), $data );
} else {
// Not okay, so delete any stale entry.
$redis->del( sprintf( 'pjc-%s', self::$request_hash ) );
}
$redis->del( sprintf( 'pjc-%s-lock', self::$request_hash ) );
$redis->exec();
}
// If this is a background task there's no need to return anything.
if ( self::$fcgi_regenerate )
return;
return $output;
}
/**
* Essentially an md5 cache for domain.com/path?query used to
* bust caches by URL when needed.
*/
private static function get_url_hash( $url = false ) {
if ( ! $url )
return md5( $_SERVER['HTTP_HOST'] . self::parse_request_uri( $_SERVER['REQUEST_URI'] ) );
$parsed = parse_url( $url );
$request_uri = ! empty( $parsed['path'] ) ? $parsed['path'] : '';
if ( ! empty( $parsed['query'] ) )
$request_uri .= '?' . $parsed['query'];
return md5( $parsed['host'] . self::parse_request_uri( $request_uri ) );
}
/**
* Schedule an expiry on transition of published posts.
*/
public static function transition_post_status( $new_status, $old_status, $post ) {
if ( $new_status != 'publish' && $old_status != 'publish' )
return;
self::clear_cache_by_post_id( $post->ID, false );
}
/**
* Runs during template_redirect, steals some post ids and flag our caches.
*/
public static function template_redirect() {
$blog_id = get_current_blog_id();
if ( is_singular() )
self::flag( sprintf( 'post:%d:%d', $blog_id, get_queried_object_id() ) );
if ( is_feed() )
self::flag( sprintf( 'feed:%d', $blog_id ) );
}
/**
* A post has changed so attempt to clear some cached pages.
*/
public static function clean_post_cache( $post_id ) {
$post = get_post( $post_id );
if ( empty( $post->post_status ) || $post->post_status != 'publish' )
return;
self::clear_cache_by_post_id( $post_id, false );
}
/**
* Add a flag to this request.
*
* @param string $flag Keep these short and unique, don't overuse.
*/
public static function flag( $flag ) {
self::$flags[] = $flag;
}
/**
* Clear cache by URLs.
*
* @param string|array $urls A string or array of URLs to flush.
* @param bool $expire Expire cache by default, or delete if set to false.
*/
public static function clear_cache_by_url( $urls, $expire = true ) {
if ( is_string( $urls ) )
$urls = array( $urls );
foreach ( $urls as $url ) {
$flag = 'url:' . self::get_url_hash( $url );
if ( $expire ) {
self::$flags_expire[] = $flag;
} else {
self::$flags_delete[] = $flag;
}
}
}
/**
* Clear cache by flag or flags.
*
* @param string|array $flags A string or array of flags to expire.
* @param bool $expire Expire cache by default, or delete if set to false.
*/
public static function clear_cache_by_flag( $flags, $expire = true ) {
if ( is_string( $flags ) )
$flags = array( $flags );
foreach ( $flags as $flag ) {
if ( $expire ) {
self::$flags_expire[] = $flag;
} else {
self::$flags_delete[] = $flag;
}
}
}
/**
* Runs during shutdown, set some flags to expire.
*/
public static function maybe_clear_caches() {
$sets = array();
if ( ! empty( self::$flags_expire ) )
$sets['pjc-expired-flags'] = self::$flags_expire;
if ( ! empty( self::$flags_delete ) )
$sets['pjc-deleted-flags'] = self::$flags_delete;
if ( empty( $sets ) )
return;
$redis = self::get_redis();
if ( ! $redis )
return;
foreach ( $sets as $key => $flags ) {
$flags = array_unique( $flags );
$timestamp = time();
$args = array( $key );
foreach ( $flags as $flag )
array_push( $args, $timestamp, $flag );
$redis->multi();
call_user_func_array( array( $redis, 'zAdd' ), $args );
$redis->expire( $key, self::$ttl );
$redis->zRemRangeByScore( $key, '-inf', $timestamp - self::$ttl );
$redis->zCard( $key );
list( $_, $_, $r, $count ) = $redis->exec();
// Hard-limit the data size.
if ( $count > 256 ) {
$redis->ZRemRangeByRank( $key, 0, $count - 256 - 1 );
}
}
}
/**
* Expire caches by post id.
*
* @param int $post_id The post ID to expire.
* @param bool $expire Expire cache by default, or delete if set to false.
*/
public static function clear_cache_by_post_id( $post_id, $expire = true ) {
$blog_id = get_current_blog_id();
$home = get_option( 'home' );
// Todo, perhaps flag these and expire by home:blog_id flag.
self::clear_cache_by_url( array(
trailingslashit( $home ),
$home,
), $expire );
self::clear_cache_by_flag( array(
sprintf( 'post:%d:%d', $blog_id, $post_id ),
sprintf( 'feed:%d', $blog_id ),
), $expire );
}
/**
* Pre 4.7 add_action() compatibility.
*/
public static function _add_action_compat() {
// Filters are not yet available, so hi-jack the $wp_filter global to add our actions.
$GLOBALS['wp_filter']['clean_post_cache'][10]['pj-page-cache'] = array(
'function' => array( __CLASS__, 'clean_post_cache' ), 'accepted_args' => 1 );
$GLOBALS['wp_filter']['transition_post_status'][10]['pj-page-cache'] = array(
'function' => array( __CLASS__, 'transition_post_status' ), 'accepted_args' => 3 );
$GLOBALS['wp_filter']['template_redirect'][100]['pj-page-cache'] = array(
'function' => array( __CLASS__, 'template_redirect' ), 'accepted_args' => 1 );
}
}
Redis_Page_Cache::cache_init();