Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Delete transients when cache is enabled

## 2.7.0

- Preserve key TTL when calling (in|de)crement methods
Expand Down
29 changes: 29 additions & 0 deletions includes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@

add_action( 'wp_head', [ $this, 'register_shutdown_hooks' ] );

add_action( 'redis_object_cache_enable', [ $this, 'maybe_delete_transients' ] );

add_filter( 'qm/collectors', [ $this, 'register_qm_collector' ], 25 );
add_filter( 'qm/outputter/html', [ $this, 'register_qm_output' ] );

Expand Down Expand Up @@ -1204,6 +1206,33 @@
}
}

/**
* Delete all transients if the cache was enabled successfully.
* Callback for `redis_object_cache_enable` action.
*
* @param bool $should_delete
* @return void
*/
public function maybe_delete_transients( $should_delete ) {
global $wpdb;

if ( ! $result ) {

Check failure on line 1219 in includes/class-plugin.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2; Relay 0.7.0)

Undefined variable: $result

Check failure on line 1219 in includes/class-plugin.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4; Relay 0.7.0)

Undefined variable: $result
return;
}

$wpdb->query( $wpdb->prepare(

Check warning on line 1223 in includes/class-plugin.php

View workflow job for this annotation

GitHub Actions / Plugin Check

WordPress.DB.DirectDatabaseQuery.NoCaching

Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().

Check warning on line 1223 in includes/class-plugin.php

View workflow job for this annotation

GitHub Actions / Plugin Check

WordPress.DB.DirectDatabaseQuery.DirectQuery

Use of a direct database call is discouraged.
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
'_transient_%',
'_site_transient_%'
) );

if ( is_multisite() ) {
$wpdb->query( $wpdb->prepare(

Check warning on line 1230 in includes/class-plugin.php

View workflow job for this annotation

GitHub Actions / Plugin Check

WordPress.DB.DirectDatabaseQuery.NoCaching

Direct database call without caching detected. Consider using wp_cache_get() / wp_cache_set() or wp_cache_delete().

Check warning on line 1230 in includes/class-plugin.php

View workflow job for this annotation

GitHub Actions / Plugin Check

WordPress.DB.DirectDatabaseQuery.DirectQuery

Use of a direct database call is discouraged.
"DELETE FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s", '_site_transient_%'
) );
}
}

/**
* Displays the redis cache html comment
*
Expand Down
8 changes: 8 additions & 0 deletions includes/cli/class-commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public function enable() {
FS_CHMOD_FILE
);

/**
* Fires on cache enable event
*
* @since 1.3.5
* @param bool $result Whether the filesystem event (copy of the `object-cache.php` file) was successful.
*/
do_action( 'redis_object_cache_enable', $copy );

if ( $copy ) {
WP_CLI::success( __( 'Object cache enabled.', 'redis-cache' ) );
} else {
Expand Down
Loading