Skip to content

Commit

Permalink
WP Super Cache: add constants to modify preload timings (#36246)
Browse files Browse the repository at this point in the history
* Add more constants to modify preload timing.

* changelog

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8198298813
  • Loading branch information
thingalon authored and matticbot committed Mar 8, 2024
1 parent ad2fd1f commit a226682
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This is an alpha version! The changes listed here are not final.

### Added
- Setup: Detect Jetpack Boost cache and suggest troubleshooting steps
- WP Super Cache: added WPSC_PRELOAD_POST_INTERVAL and WPSC_PRELOAD_LOOP_INTERVAL to modify preload timings

### Changed
- Code Modernization: Replace usage of strpos() with str_contains()
Expand Down
2 changes: 0 additions & 2 deletions wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* It has all the code for caching and serving requests.
*/

define( 'WPSC_PRELOAD_POST_COUNT', 10 );

// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- TODO: Fix or determine for sure that these should not be fixed.
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- TODO: Fix or determine for sure that these should not be fixed.

Expand Down
28 changes: 25 additions & 3 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@
unset( $wpsc_php_version );
}

/**
* Defines how many posts to preload per loop.
*/
if ( ! defined( 'WPSC_PRELOAD_POST_COUNT' ) ) {
define( 'WPSC_PRELOAD_POST_COUNT', 10 );
}

/**
* Defines the interval in seconds between preloading pages.
*/
if ( ! defined( 'WPSC_PRELOAD_POST_INTERVAL' ) ) {
define( 'WPSC_PRELOAD_POST_INTERVAL', 1 );
}

/**
* Defines the interval in seconds between preloading loops.
*/
if ( ! defined( 'WPSC_PRELOAD_LOOP_INTERVAL' ) ) {
define( 'WPSC_PRELOAD_LOOP_INTERVAL', 0 );
}

function wpsc_init() {
global $wp_cache_config_file, $wp_cache_config_file_sample, $wpsc_advanced_cache_dist_filename, $wp_cache_check_wp_config, $wpsc_advanced_cache_filename, $wpsc_promo_links;

Expand Down Expand Up @@ -3470,7 +3491,7 @@ function wp_cron_preload_cache() {
)
);
wp_cache_debug( "wp_cron_preload_cache: fetched $url" );
sleep( 1 );
sleep( WPSC_PRELOAD_POST_INTERVAL );

if ( ! wpsc_is_preload_active() ) {
wp_cache_debug( 'wp_cron_preload_cache: cancelling preload process.' );
Expand Down Expand Up @@ -3505,6 +3526,7 @@ function wp_cron_preload_cache() {

if ( $preload_more_taxonomies === true ) {
wpsc_schedule_next_preload();
sleep( WPSC_PRELOAD_LOOP_INTERVAL );
return true;
}
} elseif ( $c === 0 && $wp_cache_preload_email_me ) {
Expand Down Expand Up @@ -3594,7 +3616,7 @@ function wp_cron_preload_cache() {
wp_remote_get( $url, array('timeout' => 60, 'blocking' => true ) );
wp_cache_debug( "wp_cron_preload_cache: fetched $url", 5 );
++$count;
sleep( 1 );
sleep( WPSC_PRELOAD_POST_INTERVAL );
}

if ( $wp_cache_preload_email_me && ( $wp_cache_preload_email_volume === 'medium' || $wp_cache_preload_email_volume === 'many' ) ) {
Expand All @@ -3603,8 +3625,8 @@ function wp_cron_preload_cache() {
}

wpsc_schedule_next_preload();

wpsc_delete_files( get_supercache_dir() );
sleep( WPSC_PRELOAD_LOOP_INTERVAL );
} else {
$msg = '';
wpsc_reset_preload_counter();
Expand Down

0 comments on commit a226682

Please sign in to comment.