Incorrect “purge_everything” for Cloudflare
-
The siteground optimizer module imo incorrectly purges the complete cloudflare cache upon update or save post. Individual pages should be cleared as clearing the cache fully causes the efficiency to suffer.
In summary, when a woocommerce product (or other post) is saved, Supercacher->purge_queue is ran. When the site is setup to use cloudflare caching, instead of clearing only affected pages, supercacher purges the complete cloudflare edge cache
/wp-content/plugins/sg-cachepress/core/Supercacher/Supercacher.php lines 415-417
// Flush the Cloudflare cache if the optimization is enabled. if ( 1 === intval( get_option( 'siteground_optimizer_cloudflare_optimization', 0 ) ) ) { Cloudflare::get_instance()->purge_cache(); }
I have written a fix that will clear only the pages that are purged in the purge_queue method. Cache efficiency is now over 90% with added changes.
In summary:
1) Added a new method to Cloudflare integration
/wp-content/plugins/sg-cachepress/core/DNS/Cloudflare.php
/** * Purge some files. * https://api.cloudflare.com/#zone-purge-files-by-url * * @since * * @return mixed Request response. */ public function purge_urls(array $urls) { // have to renumber with array_values, otherwise request json_encode will return files as an object $urls = array_values(array_filter($urls)); // Bail if the email or the api key is empty. if (empty($urls) || false === $this->prepare()) { return; } $chunks = array_chunk($urls, 30); foreach ($chunks as $chunk) { // Make the request. $response = $this->request( $this->get_path('purge'), 'POST', array( 'files' => $chunk, ) ); } }
2) Changed line 415-417 in /wp-content/plugins/sg-cachepress/core/Supercacher/Supercacher.php to use new selective purge method above
// Flush the Cloudflare cache if the optimization is enabled. if (1 === intval(get_option('siteground_optimizer_cloudflare_optimization', 0))) { Cloudflare::get_instance()->purge_urls($queue); }
Can provide a git patch if that is more convenient.
- The topic ‘Incorrect “purge_everything” for Cloudflare’ is closed to new replies.