Automattic / jetpack

Security, performance, marketing, and design tools — Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
https://jetpack.com/
Other
1.59k stars 800 forks source link

How to programatically clear cache #25504

Open wgstjf opened 5 years ago

wgstjf commented 5 years ago

Hi guys,

A bespoke site we've built for a client has some global site settings, such as company address etc, which are use on various pages throughout the site. We need to be able to clear the site's cache when these settings are updated/saved so the changes are reflected in the frontend.

We can hook into the save process so just need to know what function to call to delete the cache. Any help here would be greatly appreciated.

Cheers,

Will

stodorovic commented 5 years ago

There are two possibilities:

global $file_prefix;
wp_cache_clean_cache( $file_prefix, true );

or wp_cache_clear_cache( get_current_blog_id() );

I think that first way is more common (wp-cli and REST api use this way), but changes in menu runs function wp_cache_clear_cache. You could test both and see which is better in your case.

Also you should check if function exists before calling because it could be fatal error if WPSC is deactivated. Example:

if ( function_exists( 'wp_cache_clean_cache' ) ) {
  global $file_prefix;
  wp_cache_clean_cache( $file_prefix, true );
}
wgstjf commented 5 years ago

Perfect! Thanks for the quick response. Really appreciated.

johnwc commented 3 years ago

We have a plugin that polls every hour for updated listing from a REST provider. In our cron function we have a function that finds all the posts/pages that are using our shortcode. We need to be able to tell WP Super Cache to clear the cache for only those pages/posts. At the end of the cron, we run the following loop. Is this good, or should we be calling something else? We tried using wp_cache_clear_cache, but it gave an error on pages.

foreach($pages as $p) {
    $permlink = get_permalink($p->ID);
    echo "Clearing WP Super Cache: $permlink<br />";
    wp_cache_post_change($p->ID);
}
lkraav commented 3 years ago

"but it gave an error on pages"

"An error" is about as unspecific as it can get and blocks people from giving accurate feedback.

johnwc commented 3 years ago

"but it gave an error on pages"

"An error" is about as unspecific as it can get and blocks people from giving accurate feedback.

I wasn't reporting the error nor was I asking about it, I was asking a question about the recommended way of clearing the cache programmatically for a single page/post. If the contributers think there should not be an error, then I can create a new issue related to that error...

lkraav commented 3 years ago

https://github.com/Automattic/jetpack/issues/25504 seems to be pretty clear about wp_cache_clean_cache(), but I have not personally tried it yet.

johnwc commented 3 years ago

https://github.com/Automattic/jetpack/issues/25504 seems to be pretty clear about wp_cache_clean_cache(), but I have not personally tried it yet.

wp_cache_clean_cache expects a path and not a post/page id. Are you a developer/contributer to this plugin?

hirasso commented 1 year ago

I was successful with this function:

wpsc_delete_url_cache(get_permalink($post_id))