cloudflare / Cloudflare-WordPress

A Cloudflare plugin for WordPress
https://www.cloudflare.com/wordpress/
BSD 3-Clause "New" or "Revised" License
216 stars 83 forks source link

Allow clearing cache for array of post ids or single post id #469

Closed midweste closed 2 years ago

midweste commented 2 years ago

I have a use case that would have required many extraneous requests to clear related urls from the cache.

Use case: Woocommerce coupon is updated. Products that are valid for this coupon and their related links need to be cleared from cache.

Problem: Cloudflare module allows me to clear related links based on a single post id. In my case, there is significant overlap between the related posts. Causes many extra requests to clear urls.

Solution: Allow for either a postId or postIds array to be process by Hooks purgeCacheByRelevantURLs. Aggregate all related links and apply array_unique to only clear overlapping urls once.

Results: image

For 24 products (posts) that need their related links cleared, pull request reduces the amount of API requests from 31 to 7 (442% decrease) and the amount of individuals chunked urls that need to be cleared from 902 to 192. This is for a forced https site, non forced https sites would double that amount.

midweste commented 2 years ago

An added bonus imo, is that the hooks still run so that will allow users to determine if a url should be added or not. Added in array_unique to remove any duplicates added by user hooks

midweste commented 2 years ago

@jacobbednarz And by all means, keep commenting and offering direction. I'm far more interested in code that is robust vs quickly published.

jacobbednarz commented 2 years ago

other than the small naming change, i think we're good here. thanks for the contribution!

midweste commented 2 years ago

other than the small naming change, i think we're good here. thanks for the contribution!

Thanks for helping me through it!