Sensei: Overly aggressive object cache flush causing performance problems [L]

Created on 28 Jun 2016  路  6Comments  路  Source: Automattic/sensei

Every call to WooThemes_Sensei_Utils::user_complete_course causing a complete object cache flush. It looks like a single item is being updated, you should be able to just delete the keys that have been changed. This is especially painful on a multi-site.

#0 /site/wp-content/object-cache.php(83): WP_Object_Cache->flush()
#1 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-utils.php(185): wp_cache_flush()
#2 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-utils.php(1840): WooThemes_Sensei_Utils::sensei_log_activity(Array)
#3 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-utils.php(1551): WooThemes_Sensei_Utils::update_course_status(28306, '397', 'in-progress', Array)
#4 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-course.php(100): WooThemes_Sensei_Utils::user_complete_course('397', 28306)
#5 [internal function]: WooThemes_Sensei_Course->update_status_after_lesson_change(28306, 426)
#6 /wordpress-4.5.3/wp-includes/plugin.php(525): call_user_func_array(Array, Array)
#7 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-utils.php(805): do_action('sensei_user_les...', 28306, 426)
#8 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-frontend.php(831): WooThemes_Sensei_Utils::sensei_start_lesson(426, 28306, true)
#9 [internal function]: WooThemes_Sensei_Frontend->sensei_complete_lesson('')
#10 /wordpress-4.5.3/wp-includes/plugin.php(525): call_user_func_array(Array, Array)
#11 /site/wp-content/plugins/woothemes-sensei/templates/single-lesson/lesson-meta.php(21): do_action('sensei_complete...')
#12 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-frontend.php(285): include('/dom15657/wp-co...')
#13 /site/wp-content/plugins/woothemes-sensei/inc/woothemes-sensei-template.php(264): WooThemes_Sensei_Frontend->sensei_get_template('single-lesson/l...')
#14 [internal function]: lesson_single_meta('')
#15 /wordpress-4.5.3/wp-includes/plugin.php(525): call_user_func_array('lesson_single_m...', Array)
#16 /site/wp-content/plugins/woothemes-sensei/templates/content-single-lesson.php(75): do_action('sensei_lesson_s...')
#17 /wordpress-4.5.3/wp-includes/template.php(574): require('/site/wp-co...')
#18 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-frontend.php(261): load_template('/dom15657/wp-co...', false)
#19 /site/wp-content/plugins/woothemes-sensei/classes/class-woothemes-sensei-frontend.php(1078): WooThemes_Sensei_Frontend->sensei_get_template_part('content', 'single-lesson')
#20 [internal function]: WooThemes_Sensei_Frontend->sensei_single_main_content('')
#21 /wordpress-4.5.3/wp-includes/plugin.php(525): call_user_func_array(Array, Array)
#22 /site/wp-content/plugins/woothemes-sensei/templates/single-lesson.php(29): do_action('sensei_single_m...')
#23 /wordpress-4.5.3/wp-includes/template-loader.php(75): include('/site/wp-co...')
#24 /wordpress-4.5.3/wp-blog-header.php(19): require_once('/wordpress-4.5....')
#25 /wordpress-4.5.3/index.php(17): require('/wordpress-4.5....')
#26 {main}
Performance [Pri] Normal [Status] Accepted [Type] Bug

Most helpful comment

@lkraav @donnapep Submitted a pull request with the fix :)

All 6 comments

I started looking into this tonight, and blame shows wp_cache_flush() may be legacy cruft from years back.

There are 3 such calls in class-sensei-utils.php - I will patch remove them on staging first and see how it goes.

@lkraav Did you ever put together a patch for this?

I did, and have been running live with it for a while with no apparent ill effects.

Can file a PR next.

Quick copy-paste here, maybe helps things move along

commit befbed6988f1489d409eca0e3692d46b40c4f024
Author: leho <[email protected]>
Date:   Tue Mar 26 16:28:45 2019 +0200

    wip Drop legacy `wp_cache_flush()` performance pain [1]

    [1]: https://github.com/Automattic/sensei/issues/1496

diff --git a/includes/class-sensei-utils.php b/includes/class-sensei-utils.php
index 2644cdcb..ae1c62a5 100755
--- a/includes/class-sensei-utils.php
+++ b/includes/class-sensei-utils.php
@@ -109,16 +109,12 @@ class Sensei_Utils {

                do_action( 'sensei_log_activity_before', $args, $data );

-               $flush_cache = false;
-
                // Custom Logic
                // Check if comment exists first
                $comment_id = $wpdb->get_var( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND user_id = %d AND comment_type = %s ", $args['post_id'], $args['user_id'], $args['type'] ) );
                if ( ! $comment_id ) {
                        // Add the comment
                        $comment_id = wp_insert_comment( $data );
-
-                       $flush_cache = true;
                } elseif ( isset( $args['action'] ) && 'update' == $args['action'] ) {
                        // Update the comment if an update was requested
                        $data['comment_ID'] = $comment_id;
@@ -127,14 +123,8 @@ class Sensei_Utils {
                                $data['comment_date'] = current_time( 'mysql' );
                        }
                        wp_update_comment( $data );
-                       $flush_cache = true;
                } // End If Statement

-               // Manually Flush the Cache
-               if ( $flush_cache ) {
-                       wp_cache_flush();
-               }
-
                do_action( 'sensei_log_activity_after', $args, $data, $comment_id );

                if ( 0 < $comment_id ) {
@@ -296,8 +286,6 @@ class Sensei_Utils {
                                        $dataset_changes = wp_delete_comment( intval( $value->comment_ID ), true );
                                } // End If Statement
                        } // End For Loop
-                       // Manually flush the cache
-                       wp_cache_flush();
                } // End If Statement
                return $dataset_changes;
        } // End sensei_delete_activities()
@@ -333,7 +321,6 @@ class Sensei_Utils {
                                                continue;
                                        }
                                        $dataset_changes = wp_delete_comment( intval( $activity->comment_ID ), true );
-                                       wp_cache_flush();
                                }
                        }
                }

@lkraav @donnapep Submitted a pull request with the fix :)

I now have solid reason to believe removing these flush calls solves https://github.com/Automattic/sensei/issues/1553 for people having that issue.

After removal, my "double enrollment status" database alert has gone silent for days.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

richardmtl picture richardmtl  路  5Comments

kraftbj picture kraftbj  路  4Comments

danjjohnson picture danjjohnson  路  5Comments

cycomarcooo picture cycomarcooo  路  5Comments

gaurav1984 picture gaurav1984  路  4Comments