Skip to content

Commit 1202356

Browse files
committed
preloader improvements: lazyload + defer the post queue on updates
1 parent eeeffd5 commit 1202356

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

includes/classes/Preloader.php

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PoweredCache\Async\CachePreloader;
1111
use function PoweredCache\Utils\permalink_structure_has_trailingslash;
1212
use function PoweredCache\Utils\site_cache_dir;
13+
use const PoweredCache\Constants\DEFERRED_PRELOAD_QUEUE_CRON_NAME;
1314

1415
/**
1516
* Class Preloader
@@ -67,7 +68,7 @@ public function setup() {
6768
return;
6869
}
6970

70-
$this->cache_preloader = CachePreloader::factory();
71+
$this->get_preloader();
7172

7273
/**
7374
* Page cache needs to be activated to get benefits of preloading
@@ -80,8 +81,9 @@ public function setup() {
8081
add_action( 'admin_post_powered_cache_preload_cache', [ $this, 'start_preload' ] );
8182
add_action( 'powered_cache_purge_all_cache', [ $this, 'setup_preload_queue' ] );
8283
add_action( 'powered_cache_clean_site_cache_dir', [ $this, 'setup_preload_queue' ] );
83-
add_action( 'powered_cache_advanced_cache_purge_post', [ $this, 'add_purged_urls_to_preload_queue' ], 10, 2 );
84+
add_action( 'powered_cache_advanced_cache_purge_post', [ $this, 'deferred_preload_queue' ], 10, 2 );
8485
add_action( 'powered_cache_expired_files_deleted', [ $this, 'add_expired_urls_to_preload_queue' ], 10, 2 );
86+
add_action( DEFERRED_PRELOAD_QUEUE_CRON_NAME, [ $this, 'add_purged_urls_to_preload_queue' ], 10, 2 );
8587
add_action( 'shutdown', [ $this, 'dispatch_preload_queue' ], 0 );
8688
}
8789

@@ -111,6 +113,20 @@ public function admin_bar_menu( $wp_admin_bar ) {
111113
);
112114
}
113115

116+
117+
/**
118+
* Get the instance of CachePreloader
119+
*
120+
* @return CachePreloader
121+
*/
122+
private function get_preloader() {
123+
if ( ! $this->cache_preloader ) {
124+
$this->cache_preloader = CachePreloader::factory();
125+
}
126+
127+
return $this->cache_preloader;
128+
}
129+
114130
/**
115131
* Add preloading items to queue
116132
*/
@@ -144,12 +160,8 @@ public function start_preload() {
144160
* Setup preload
145161
*/
146162
public function setup_preload_queue() {
147-
if ( ! $this->cache_preloader ) {
148-
$this->cache_preloader = CachePreloader::factory();
149-
}
150-
151163
// cancel existing process before populating
152-
$this->cache_preloader->cancel_process();
164+
$this->get_preloader()->cancel_process();
153165

154166
if ( POWERED_CACHE_IS_NETWORK ) {
155167
$sites = get_sites( [ 'fields' => 'ids' ] );
@@ -227,6 +239,26 @@ protected function populate_preload_queue() {
227239
}
228240
}
229241

242+
/**
243+
* Add URLs to preload queue with a delay
244+
*
245+
* @param int $post_id Post ID
246+
* @param array $urls The URL list of the related pages that will be preloaded
247+
*
248+
* @since 3.6
249+
*/
250+
public function deferred_preload_queue( $post_id, $urls ) {
251+
\PoweredCache\Utils\log( sprintf( 'Post ID %d purged from cache, adding related URLs to preload queue with a delay.', $post_id ) );
252+
wp_schedule_single_event(
253+
time() + 10,
254+
DEFERRED_PRELOAD_QUEUE_CRON_NAME,
255+
[
256+
'post_id' => $post_id,
257+
'urls' => $urls,
258+
]
259+
);
260+
}
261+
230262

231263
/**
232264
* Add related pages to preload queue when the cache got cleared
@@ -693,7 +725,7 @@ protected function add_url_to_preload_queue( $url ) {
693725
$do_preload = apply_filters( 'powered_cache_preload_add_url_to_queue', true, $url );
694726

695727
if ( $do_preload ) {
696-
$this->cache_preloader->push_to_queue( $url );
728+
$this->get_preloader()->push_to_queue( $url );
697729
$this->queue_dirty = true;
698730
\PoweredCache\Utils\log( sprintf( 'URL added to preload queue : %s', $url ) );
699731
} else {
@@ -712,13 +744,8 @@ public function dispatch_preload_queue() {
712744
return;
713745
}
714746

715-
// Make sure our background process instance exists
716-
if ( ! $this->cache_preloader ) {
717-
$this->cache_preloader = CachePreloader::factory();
718-
}
719-
720747
// This will serialize the queue into the DB and fire off the async request
721-
$this->cache_preloader->save()->dispatch();
748+
$this->get_preloader()->save()->dispatch();
722749

723750
\PoweredCache\Utils\log( 'Dispatched preload queue.' );
724751
}

includes/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
const SETTING_OPTION = 'powered_cache_settings';
1414
const PURGE_CACHE_CRON_NAME = 'powered_cache_delete_expired_cache';
1515
const PURGE_FO_CRON_NAME = 'powered_cache_delete_expired_min_files'; // file optimizer cron
16+
const DEFERRED_PRELOAD_QUEUE_CRON_NAME = 'powered_cache_deferred_preload_queue';
1617
const POST_META_DISABLE_CACHE_KEY = 'powered_cache_disable_cache';
1718
const POST_META_DISABLE_LAZYLOAD_KEY = 'powered_cache_disable_lazyload';
1819
const POST_META_DISABLE_CRITICAL_CSS_KEY = 'powered_cache_disable_critical_css';

0 commit comments

Comments
 (0)