Skip to content

Commit

Permalink
prevent unnecessary cache clearing (#223) (#224)
Browse files Browse the repository at this point in the history
* flush cache when comment is in database and not before
* flush when comment is or was approved
* move comment hooks to comment 'flush hooks'
* rename functions for better code comprehension
* removes user_register and edit_user_profile_update from flush total cache
  • Loading branch information
timse201 authored Mar 29, 2024
1 parent 053b59a commit ee5dafa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Smart, efficient cache solution for WordPress. Use DB, HDD, APC, Redis or Memcac
* If you don’t know how to install a plugin for WordPress, [here’s how](https://wordpress.org/support/article/managing-plugins/#installing-plugins).

### Requirements ###
* PHP 5.2.4 or greater
* WordPress 4.4 or greater
* PHP 5.6 or greater
* WordPress 4.7 or greater
* APC 3.1.4 or greater (optional)
* Memcached in Nginx (optional)
* Redis (optional, via the phpredis module)
Expand Down
81 changes: 64 additions & 17 deletions inc/class-cachify.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function __construct() {
add_action( 'post_updated', array( __CLASS__, 'save_update_trash_post' ), 10, 3 );
add_action( 'pre_post_update', array( __CLASS__, 'post_update' ), 10, 2 );
add_action( 'cachify_remove_post_cache', array( __CLASS__, 'remove_page_cache_by_post_id' ) );
add_action( 'comment_post', array( __CLASS__, 'new_comment' ), 99, 2 );
add_action( 'edit_comment', array( __CLASS__, 'comment_edit' ), 10, 2 );
add_action( 'transition_comment_status', array( __CLASS__, 'comment_status' ), 10, 3 );

/* Flush Hooks - third party */
add_action( 'woocommerce_product_set_stock', array( __CLASS__, 'flush_woocommerce' ) );
Expand All @@ -121,9 +124,6 @@ public function __construct() {

add_action( 'init', array( __CLASS__, 'process_flush_request' ) );

/* Flush (post) cache if comment is made from frontend or backend */
add_action( 'pre_comment_approved', array( __CLASS__, 'pre_comment' ), 99, 2 );

/* Add Cron for clearing the HDD Cache */
if ( self::METHOD_HDD === self::$options['use_apc'] ) {
add_filter( 'cron_schedules', array( __CLASS__, 'add_cron_cache_expiration' ) );
Expand Down Expand Up @@ -157,10 +157,6 @@ public function __construct() {

add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_dashboard_styles' ) );

add_action( 'transition_comment_status', array( __CLASS__, 'touch_comment' ), 10, 3 );

add_action( 'edit_comment', array( __CLASS__, 'edit_comment' ) );

add_filter( 'dashboard_glance_items', array( __CLASS__, 'add_dashboard_count' ) );

add_filter( 'plugin_row_meta', array( __CLASS__, 'row_meta' ), 10, 2 );
Expand Down Expand Up @@ -890,14 +886,33 @@ public static function flush_notice() {
*
* @since 0.1.0
* @since 2.1.2
*
* @deprecated 2.4.0 Use comment_edit($id, $comment) instead.
*/
public static function edit_comment( $id ) {
if ( self::$options['reset_on_comment'] ) {
self::flush_total_cache();
} else {
self::remove_page_cache_by_post_id(
get_comment( $id )->comment_post_ID
);
self::comment_edit( $id, array( 'comment_approved' => 1 ) );
}

/**
* Remove page from cache or flush on comment edit.
*
* @param integer $id Comment ID.
* @param array $comment Comment data.
*
* @since 2.4.0 Replacement for edit_comment($id) with additional comment parameter.
*/
public static function comment_edit( $id, $comment ) {
$approved = (int) $comment['comment_approved'];

/* Approved comment? */
if ( 1 === $approved ) {
if ( self::$options['reset_on_comment'] ) {
self::flush_total_cache();
} else {
self::remove_page_cache_by_post_id(
get_comment( $id )->comment_post_ID
);
}
}
}

Expand All @@ -911,18 +926,33 @@ public static function edit_comment( $id ) {
*
* @since 0.1
* @since 2.1.2
* @since 2.4.0 Replacement for edit_comment($id) with additional comment parameter.
*/
public static function pre_comment( $approved, $comment ) {
self::new_comment( $comment['comment_ID'], $approved );

return $approved;
}

/**
* Remove page from cache or flush on new comment
*
* @param integer|string $id Comment ID.
* @param integer|string $approved Comment status.
*
* @since 0.1.0
* @since 2.1.2
* @since 2.4.0 Renamed with ID parameter instead of comment array.
*/
public static function new_comment( $id, $approved ) {
/* Approved comment? */
if ( 1 === $approved ) {
if ( self::$options['reset_on_comment'] ) {
self::flush_total_cache();
} else {
self::remove_page_cache_by_post_id( $comment['comment_post_ID'] );
self::remove_page_cache_by_post_id( get_comment( $id )->comment_post_ID );
}
}

return $approved;
}

/**
Expand All @@ -934,9 +964,26 @@ public static function pre_comment( $approved, $comment ) {
*
* @since 0.1
* @since 2.1.2
*
* @deprecated 2.4.0 Use comment_status($new_status, $old_status, $comment) instead.
*/
public static function touch_comment( $new_status, $old_status, $comment ) {
if ( $new_status !== $old_status ) {
self::comment_status( $new_status, $old_status, $comment );
}

/**
* Remove page from cache or flush on comment edit.
*
* @param string $new_status New status.
* @param string $old_status Old status.
* @param WP_Comment $comment The comment.
*
* @since 0.1
* @since 2.1.2
* @since 2.4.0 Renamed from touch_comment().
*/
public static function comment_status( $new_status, $old_status, $comment ) {
if ( 'approved' === $old_status || 'approved' === $new_status ) {
if ( self::$options['reset_on_comment'] ) {
self::flush_total_cache();
} else {
Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Smart, efficient cache solution for WordPress. Use DB, HDD, APC, Redis or Memcac
* If you don’t know how to install a plugin for WordPress, [here’s how](https://wordpress.org/support/article/managing-plugins/#installing-plugins).

### Requirements ###
* PHP 5.2.4 or greater
* WordPress 3.8 or greater
* PHP 5.6 or greater
* WordPress 4.7 or greater
* APC 3.1.4 or greater (optional)
* Memcached in Nginx (optional)
* Redis (optional, via the phpredis module)
Expand Down

0 comments on commit ee5dafa

Please sign in to comment.