Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1945 cache race condition with multiple servers #2147

Merged
merged 5 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 37 additions & 7 deletions inc/classes/Buffer/class-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,7 @@ public function maybe_process_buffer( $buffer ) {
}
}

// Save the cache file.
rocket_put_content( $cache_filepath, $buffer . $footprint );

if ( function_exists( 'gzencode' ) ) {
rocket_put_content( $cache_filepath . '_gzip', gzencode( $buffer . $footprint, apply_filters( 'rocket_gzencode_level_compression', 3 ) ) );
}

$this->write_cache_file( $cache_filepath, $buffer . $footprint );
$this->maybe_create_nginx_mobile_file( $cache_dir_path );

// Send headers with the last modified time of the cache file.
Expand All @@ -334,6 +328,42 @@ public function maybe_process_buffer( $buffer ) {
return $buffer . $footprint;
}

/**
* Writes the cache file(s)
*
* @since 3.5
* @author Remy Perona
*
* @param string $cache_filepath Absolute path to the cache file.
* @param string $content Content to write in the cache file.
* @return void
*/
private function write_cache_file( $cache_filepath, $content ) {
$gzip_filepath = $cache_filepath . '_gzip';
$temp_filepath = $cache_filepath . '_temp';
$temp_gzip_filepath = $gzip_filepath . '_temp';

if ( rocket_direct_filesystem()->exists( $temp_filepath ) ) {
return;
}

// Save the cache file.
rocket_put_content( $temp_filepath, $content );
rocket_direct_filesystem()->move( $temp_filepath, $cache_filepath );

if ( function_exists( 'gzencode' ) ) {
/**
* Filters the Gzip compression level to use for the cache file
*
* @param int $compression_level Compression level between 0 and 9.
*/
$compression_level = apply_filters( 'rocket_gzencode_level_compression', 3 );

rocket_put_content( $temp_gzip_filepath, gzencode( $content, $compression_level ) );
rocket_direct_filesystem()->move( $temp_gzip_filepath, $gzip_filepath );
}
}

/**
* Get the path to the cache file.
*
Expand Down
10 changes: 9 additions & 1 deletion inc/classes/subscriber/Media/class-webp-subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,15 @@ public function convert_to_webp( $html ) {
}

if ( ! $http_accept || false === strpos( $http_accept, 'webp' ) ) {
return $html;
$user_agent = isset( $this->server['HTTP_USER_AGENT'] ) ? $this->server['HTTP_USER_AGENT'] : '';

if ( $user_agent && preg_match( '#Firefox/(?<version>[0-9]{2,})#i', $this->server['HTTP_USER_AGENT'], $matches ) ) {
if ( 66 >= (int) $matches['version'] ) {
return $html;
}
} else {
return $html;
}
}

$extensions = $this->get_extensions();
Expand Down