Skip to content

Commit f4dfbd1

Browse files
author
David Evbodaghe
committed
Fix performance issue: Cache background sync job queries and skip on frontend
1 parent 3c5e2c2 commit f4dfbd1

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

includes/Framework/Utilities/BackgroundJobHandler.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ protected function is_queue_empty() {
229229
protected function invalidate_queue_cache() {
230230
delete_transient( $this->queue_empty_cache_key );
231231
delete_transient( $this->sync_in_progress_cache_key );
232+
// Also clear the is_sync_in_progress cache used by Sync class
233+
delete_transient( 'wc_facebook_sync_in_progress' );
232234
}
233235

234236

@@ -543,9 +545,8 @@ public function get_jobs( $args = [] ) {
543545
$args = wp_parse_args(
544546
$args,
545547
[
546-
'order' => 'DESC',
547-
'orderby' => 'option_id',
548-
'use_cache' => true,
548+
'order' => 'DESC',
549+
'orderby' => 'option_id',
549550
]
550551
);
551552

@@ -554,19 +555,6 @@ public function get_jobs( $args = [] ) {
554555
return null; // Return no jobs on frontend to avoid query
555556
}
556557

557-
// For status=processing queries, use cache to avoid expensive queries
558-
$cache_key = null;
559-
if ( $args['use_cache'] && ! empty( $args['status'] ) ) {
560-
$statuses = (array) $args['status'];
561-
if ( count( $statuses ) === 1 && 'processing' === $statuses[0] ) {
562-
$cache_key = $this->sync_in_progress_cache_key;
563-
$cached = get_transient( $cache_key );
564-
if ( false !== $cached ) {
565-
return 'has_jobs' === $cached ? [ (object) [ 'cached' => true ] ] : null;
566-
}
567-
}
568-
}
569-
570558
$replacements = [ $this->identifier . '_job_%' ];
571559
$status_query = '';
572560

@@ -596,10 +584,6 @@ public function get_jobs( $args = [] ) {
596584
$results = $wpdb->get_col( $query );
597585

598586
if ( empty( $results ) ) {
599-
// Cache the empty result for processing status queries - invalidated when job status changes
600-
if ( $cache_key ) {
601-
set_transient( $cache_key, 'no_jobs', 0 );
602-
}
603587
return null;
604588
}
605589

@@ -614,11 +598,6 @@ public function get_jobs( $args = [] ) {
614598
$jobs[] = $job;
615599
}
616600

617-
// Cache the result for processing status queries - invalidated when job status changes
618-
if ( $cache_key ) {
619-
set_transient( $cache_key, 'has_jobs', 0 );
620-
}
621-
622601
return $jobs;
623602
}
624603

includes/Products/Sync.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,30 @@ private function get_product_index( $product_id ) {
263263
* @return bool
264264
*/
265265
public static function is_sync_in_progress() {
266+
// On frontend, skip the check entirely - visitors don't need sync status
267+
if ( ! is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() ) {
268+
return false;
269+
}
270+
271+
// Check cache first
272+
$cache_key = 'wc_facebook_sync_in_progress';
273+
$cached = get_transient( $cache_key );
274+
if ( false !== $cached ) {
275+
return 'yes' === $cached;
276+
}
277+
278+
// Query for processing jobs
266279
$jobs = facebook_for_woocommerce()->get_products_sync_background_handler()->get_jobs(
267280
array(
268281
'status' => 'processing',
269282
)
270283
);
271284

272-
return ! empty( $jobs );
285+
$in_progress = ! empty( $jobs );
286+
287+
// Cache the result - invalidated when job status changes
288+
set_transient( $cache_key, $in_progress ? 'yes' : 'no', 0 );
289+
290+
return $in_progress;
273291
}
274292
}

includes/Utilities/DebugTools.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ public function clean_up_old_background_sync_options() {
7171
// Delete job entries (but not cache transients which use different pattern)
7272
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'wc_facebook_background_product_sync_job_%'" );
7373

74-
// Invalidate the queue cache since we deleted jobs directly from the database
74+
// Invalidate all sync-related caches since we deleted jobs directly from the database
7575
delete_transient( 'wc_facebook_background_product_sync_queue_empty' );
7676
delete_transient( 'wc_facebook_background_product_sync_sync_in_progress' );
77+
delete_transient( 'wc_facebook_sync_in_progress' );
7778

7879
return __( 'Background sync jobs have been deleted.', 'facebook-for-woocommerce' );
7980
}

0 commit comments

Comments
 (0)