kovshenin / surge

Surge is a very simple and fast page caching plugin for WordPress.
GNU General Public License v3.0
157 stars 11 forks source link

Invalidating Cache Entries (or entire cache programmatically) #29

Closed MadtownLems closed 9 months ago

MadtownLems commented 1 year ago

We have a custom plugin called "Content Blocks" (which are basically re-usable Blocks from 10 years before the block editor was even a thing). It's a custom post type, and they can be included on other Pages and Posts.

I noticed that when someone updates a Content Block, the cached entries that use it don't become invalidated.

Surge will automatically invalidate cached entries on various different events, so you shouldn’t have to worry about that.

Is there any way to hook into Surge's behavior to modify it so that affected cache entries are invalidated?

Alternatively, is there any way we can programmatically just clear all of Surge's cache?

Given how much content across our sites can be impacted when ANYTHING changes (content blocks, other plugins that list recent posts, widgets, etc), we actually just clear our own custom cache stuff every time ANY post is updated, and that works well for us. If possible, I'd simply add a call to surge_clear_complete_cache() to that same function.

I do realize I could probably use deactive_plugin() and activate_plugin(), but that seems like a lot of unnecessary overhead for when I'm just trying to clear the cache.

Thanks as always!

kovshenin commented 1 year ago

@MadtownLems you can use Surge\flag() to set specific flags when a page is being rendered, for example if it's rendered using block-1, block-2 and block-3, you can add those flags to the request, then use Surge\expire() to expire block-1 when it's updated, which would invalidate all pages with that flag.

There are some examples in the existing invalidation routines: https://github.com/kovshenin/surge/blob/main/include/invalidate.php

Hope that helps!

MadtownLems commented 1 year ago

Hope that helps!

I wish it did 😅

I've taken a look at invalidate.php many times now (as well as https://konstantin.blog/2021/wordpress-cache-invalidation-with-flags/ ) and still have no idea how I might use Surge\flag().

We have Surge network activated on a MultiSite network, and what I'm really trying to do is figure out a way to clear a single subsite's entire cache on certain events.

I've also been looking at /wp-content/cache/surge/flags.json.php and am trying to understand how the flags stored in this file differentiate between sites in the multisite 🤔

Any insights you can provide would be much appreciated. 🙏

kovshenin commented 12 months ago

@MadtownLems here's an example:

// Flag each entry with the current blog id.
add_action( 'template_redirect', function() {
    Surge\flag( sprintf( 'blogid:%d', get_current_blog_id() ) );
} );

// Expire all subsite cache on certain events.
add_action( 'certain_event', function() {
    Surge\expire( sprintf( 'blogid:%d', get_current_blog_id() ) );
} );
MadtownLems commented 12 months ago

here's an example:

Perfect! Exactly what I was looking for. I had no idea that "blogid:" was a valid flag, as I didn't see it anywhere else in invalidate.php.

Question: Are the available flags some standard thing that I can read about somewhere?

kovshenin commented 12 months ago

Nice! blogid is just an arbitrary string, there's no standard way, you can really flag a cached entry with any string you like, as long as it can be successfully encoded to an decoded from JSON, it should be ok.