exclude caching for requests with cookie name(s) starting with a certain string

This commit is contained in:
Harold Kyle 2013-04-09 15:40:03 -04:00
parent d64f49958f
commit b1eb73dae4
4 changed files with 59 additions and 8 deletions

View file

@ -53,17 +53,34 @@ else
return false;
/* no cache for for logged in users normally, only if enabled */
if ( $wp_ffpc_config['cache_loggedin'] == 0 ) {
if ( $wp_ffpc_config['cache_loggedin'] == 0 || $wp_ffpc_config['nocache_cookies'] ) {
$nocache_cookies = array();
if( $wp_ffpc_config['nocache_cookies'] ){
$nocache_cookies = array_map('trim',explode(",", $wp_ffpc_config['nocache_cookies'] ) );
}
foreach ($_COOKIE as $n=>$v) {
// test cookie makes to cache not work!!!
if ($n == 'wordpress_test_cookie') continue;
// wp 2.5 and wp 2.3 have different cookie prefix, skip cache if a post password cookie is present, also
if ( (substr($n, 0, 14) == 'wordpressuser_' || substr($n, 0, 10) == 'wordpress_' || substr($n, 0, 12) == 'wp-postpass_') && !$wp_ffpc_config['cache_loggedin'] ) {
return false;
if ( $wp_ffpc_config['cache_loggedin'] == 0 ) {
if ( (substr($n, 0, 14) == 'wordpressuser_' || substr($n, 0, 10) == 'wordpress_' || substr($n, 0, 12) == 'wp-postpass_') && !$wp_ffpc_config['cache_loggedin'] ) {
return false;
}
}
/* check for any matches to user-added cookies to no-cache */
if ( ! empty( $nocache_cookies ) ){
foreach ( $nocache_cookies as $nocache_cookie ) {
if( strpos( $n, $nocache_cookie ) === 0 ) {
return false;
}
}
}
}
}
/* canonical redirect storage */
$wp_ffpc_redirect = null;
@ -146,7 +163,7 @@ if ( !empty( $wp_ffpc_values['meta']['pingback'] ) )
/* for debugging */
if ( $wp_ffpc_config['response_header'] )
header( 'X-Cache-Engine: WP-FFPC with ' . $wp_ffpc_config['cache_type'] );
header( 'X-Cache-Engine: WP-FFPC with ' . $wp_ffpc_config['cache_type'] .' via PHP');
/* HTML data */
echo $wp_ffpc_values['data'];

View file

@ -391,7 +391,7 @@ if ( ! class_exists( 'WP_FFPC' ) ) {
</dt>
<dd>
<input type="text" name="prefix_data" id="prefix_data" value="<?php echo $this->options['prefix_data']; ?>" />
<span class="description"><?php _e('Prefix for HTML content keys, can be used in nginx. If you are caching with nginx, you should update your nginx configuration and restart nginx after changing this value.', $this->plugin_constant); ?></span>
<span class="description"><?php _e('Prefix for HTML content keys, can be used in nginx.<br>If you are caching with nginx, you should update your nginx configuration and reload nginx after changing this value.', $this->plugin_constant); ?></span>
</dd>
<dt>
@ -420,7 +420,7 @@ if ( ! class_exists( 'WP_FFPC' ) ) {
</dt>
<dd>
<input type="checkbox" name="log_info" id="log_info" value="1" <?php checked($this->options['log_info'],true); ?> />
<span class="description"><?php _e('Enables INFO level messages; carefull, plugin is really talkative. Requires PHP syslog function.', $this->plugin_constant); ?></span>
<span class="description"><?php _e('Enables INFO level messages; careful, plugin is really talkative. Requires PHP syslog function.', $this->plugin_constant); ?></span>
</dd>
<dt>
@ -440,7 +440,7 @@ if ( ! class_exists( 'WP_FFPC' ) ) {
</dd>
<dt>
<label for="sync_protocols"><?php _e("Enable sync protocolls", $this->plugin_constant); ?></label>
<label for="sync_protocols"><?php _e("Enable sync protocols", $this->plugin_constant); ?></label>
</dt>
<dd>
<input type="checkbox" name="sync_protocols" id="sync_protocols" value="1" <?php checked($this->options['sync_protocols'],true); ?> />
@ -501,6 +501,14 @@ if ( ! class_exists( 'WP_FFPC' ) ) {
<input type="checkbox" name="nocache_page" id="nocache_page" value="1" <?php checked($this->options['nocache_page'],true); ?> />
<span class="description"><?php _e('Exclude pages from caching.', $this->plugin_constant); ?></span>
</dd>
<dt>
<label for="nocache_cookies"><?php _e("Don't cache cookies", $this->plugin_constant); ?></label>
</dt>
<dd>
<input type="text" name="nocache_cookies" id="nocache_cookies" value="<?php if(isset( $this->options['nocache_cookies'] ) ) echo $this->options['nocache_cookies']; ?>" />
<span class="description"><?php _e('Exclude cookies names starting with this from caching. Separate multiple cookies names with commas.<br>If you are caching with nginx, you should update your nginx configuration and reload nginx after changing this value.', $this->plugin_constant); ?></span>
</dd>
</dl>
</fieldset>
@ -519,7 +527,7 @@ if ( ! class_exists( 'WP_FFPC' ) ) {
</dt>
<dd>
<input type="checkbox" name="persistent" id="persistent" value="1" <?php checked($this->options['persistent'],true); ?> />
<span class="description"><?php _e('Make all memcache(d) connections persistent. Be carefull with this setting, always test the outcome.', $this->plugin_constant); ?></span>
<span class="description"><?php _e('Make all memcache(d) connections persistent. Be careful with this setting, always test the outcome.', $this->plugin_constant); ?></span>
</dd>
</dl>
</fieldset>
@ -784,6 +792,27 @@ if ( ! class_exists( 'WP_FFPC' ) ) {
else
$nginx = str_replace ( 'LOGGEDIN_EXCEPTION' , '' , $nginx );
/* nginx can skip caching for visitors with certain cookies specified in the options */
if( $this->options['nocache_cookies'] ) {
$cookies = str_replace( ",","|", $this->options['nocache_cookies'] );
$cookies = str_replace( " ","", $cookies );
$cookie_exception = '# avoid cache for cookies specified
if ($http_cookie ~* ' . $cookies . ' ) {
set $memcached_request 0;
}';
$nginx = str_replace ( 'COOKIES_EXCEPTION' , $cookie_exception , $nginx );
} else {
$nginx = str_replace ( 'COOKIES_EXCEPTION' , '' , $nginx );
}
/* add custom response header if specified in the options */
if( $this->options['response_header'] ){
$response_header = 'add_header X-Cache-Engine "WP-FFPC with ' . $this->options['cache_type'] .' via nginx";';
$nginx = str_replace ( 'RESPONSE_HEADER' , $response_header , $nginx );
} else{
$nginx = str_replace ( 'RESPONSE_HEADER' , '' , $nginx );
}
return $nginx;
}

View file

@ -144,7 +144,10 @@ MEMCACHED_SERVERS
LOGGEDIN_EXCEPTION
COOKIES_EXCEPTION
if ( $memcached_request = 1) {
RESPONSE_HEADER
memcached_pass memcached-servers;
error_page 404 = @rewrites;
}
@ -156,6 +159,7 @@ MEMCACHED_SERVERS
## rewrite rules
location @rewrites {
add_header X-Cache-Engine "";
rewrite ^ /index.php?$args last;
}

View file

@ -43,6 +43,7 @@ $wp_ffpc_defaults = array (
'nocache_archive' => false,
'nocache_single' => false,
'nocache_page' => false,
'nocache_cookies' => false,
'sync_protocols' => false,
'persistent' => false,
'response_header' => false,