Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wildcard cache key deletion for device type cache purge #203

Merged
merged 2 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 41 additions & 9 deletions admin/class-phpredis-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function purge_url( $url, $feed = true ) {
/**
* Filters the URL to be purged.
*
* @since 2.1.0
* @since 2.0.4
chandrapatel marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $url URL to be purged.
*/
Expand All @@ -120,12 +120,44 @@ public function purge_url( $url, $feed = true ) {

$prefix = $nginx_helper_admin->options['redis_prefix'];
$_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'];
$is_purged = $this->delete_single_key( $_url_purge_base );

if ( $is_purged ) {
$this->log( '- Purged URL | ' . $url );
/**
* To delete device type caches such as `<URL>--mobile`, `<URL>--desktop`, `<URL>--lowend`, etc.
* This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache.
*
* For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL.
* Add this filter in separate plugin or simply in theme's function.php file:
* ```
* add_filter( 'rt_nginx_helper_purge_url', function( $url ) {
* $url = $url . '--*';
* return $url;
* });
* ```
*
* Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted.
*
* @since 2.0.4
*/
if ( strpos( $_url_purge_base, '*' ) === false ) {

$status = $this->delete_single_key( $_url_purge_base );

if ( $status ) {
$this->log( '- Purge URL | ' . $_url_purge_base );
chandrapatel marked this conversation as resolved.
Show resolved Hide resolved
} else {
$this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' );
}

} else {
$this->log( '- Cache Not Found | ' . $url, 'ERROR' );

$status = $this->delete_keys_by_wildcard( $_url_purge_base );

if ( $status ) {
$this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' );
chandrapatel marked this conversation as resolved.
Show resolved Hide resolved
} else {
$this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' );
}

}

$this->log( '* * * * *' );
Expand Down Expand Up @@ -166,9 +198,9 @@ public function custom_purge_urls() {
$status = $this->delete_single_key( $purge_url );

if ( $status ) {
$this->log( '- Purge URL | ' . $purge_url );
$this->log( '- [phpredis] Purge URL | ' . $purge_url );
} else {
$this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' );
$this->log( '- [phpredis] Cache Not Found | ' . $purge_url, 'ERROR' );
chandrapatel marked this conversation as resolved.
Show resolved Hide resolved
}

} else {
Expand All @@ -177,9 +209,9 @@ public function custom_purge_urls() {
$status = $this->delete_keys_by_wildcard( $purge_url );

if ( $status ) {
$this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' );
$this->log( '- [phpredis] Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' );
} else {
$this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' );
$this->log( '- [phpredis] Cache Not Found | ' . $purge_url, 'ERROR' );
}

}
Expand Down
40 changes: 39 additions & 1 deletion admin/class-predis-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,45 @@ public function purge_url( $url, $feed = true ) {

$prefix = $nginx_helper_admin->options['redis_prefix'];
$_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'];
$this->delete_single_key( $_url_purge_base );

/**
* To delete device type caches such as `<URL>--mobile`, `<URL>--desktop`, `<URL>--lowend`, etc.
* This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache.
*
* For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL.
* Add this filter in separate plugin or simply in theme's function.php file:
* ```
* add_filter( 'rt_nginx_helper_purge_url', function( $url ) {
* $url = $url . '--*';
* return $url;
* });
* ```
*
* Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted.
*
* @since 2.0.4
*/
if ( strpos( $_url_purge_base, '*' ) === false ) {

$status = $this->delete_single_key( $_url_purge_base );

if ( $status ) {
$this->log( '- [predis] Purge URL | ' . $_url_purge_base );
} else {
$this->log( '- [predis] Cache Not Found | ' . $_url_purge_base, 'ERROR' );
chandrapatel marked this conversation as resolved.
Show resolved Hide resolved
}

} else {

$status = $this->delete_keys_by_wildcard( $_url_purge_base );

if ( $status ) {
$this->log( '- [predis] Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' );
} else {
$this->log( '- [predis] Cache Not Found | ' . $_url_purge_base, 'ERROR' );
}

}

}

Expand Down