• Resolved midwestE

    (@edouble74)


    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    Pages are interlinked so we can’t simply flush the cache for the particular URL but we need to do it for all categories, the index, taxonomies, etc. etc. so one flush would result into 20ish requests. That would be far from optimal. Without Cloudflare this works much better and we do perform smart purge where we can be sure we know all the affected URLs and when it makes sense.

    Thread Starter midwestE

    (@edouble74)

    Understood on the interlinked pages. I do understand that the amount of urls is highly variable, but as an example, for our shop, updating a product seems to clear 50-80 related urls. Per the cloudflare purge urls endpoint, it can purge 30 urls per request so for our limited example, thats only two requests – well worth it.

    Consider that when using the cloudflare wordpress plugin in tandem with sgo, the related pages are already cleared on a specific url basis, see method getPostRelatedLinks

    https://github.com/cloudflare/Cloudflare-WordPress/blob/55017a3c33ac3db6183ca6fbfe7a64e892e27c1b/src/WordPress/Hooks.php#L188

    If used with the cloudflare plugin, the sgo plugin could piggy back the filter ‘cloudflare_purge_by_url’ adding any related links that sgo has determined internally and that do not exist already in the list cloudflare is compiling.

    There’s a filter for modifying the specific urls that cloudflare is going to purge

    https://github.com/cloudflare/Cloudflare-WordPress/blob/55017a3c33ac3db6183ca6fbfe7a64e892e27c1b/src/WordPress/Hooks.php#L150

    When users don’t have the cloudflare module installed – the method getPostRelatedLinks looks to be purely wordpress functions without referring to external methods and could be used directly in the sgo plugin per license https://github.com/cloudflare/Cloudflare-WordPress/blob/master/LICENSE.md

    Appreciate the good work sg has put in making the sgo plugin!

    Thread Starter midwestE

    (@edouble74)

    Any consideration here as to future development?

    Thread Starter midwestE

    (@edouble74)

    The main reason I am interested to participate in the development of this fix is that I know Siteground hosts many many smaller sites and there are so many customers with smaller sites that could really benefit from these changes.

    In our case, we were updating our products on average 15-20 times per day and clearing the cache that often gave us a cache efficiency in the 50-60% range on good days. It routinely operates at 90%+ when only clearing related content.

    For our site alone over the last 7 day period: 28GB served by cloudflare at a 92% hit rate, and at 32% higher cache efficiency, a savings of 8GB that did not have to be served from SG. 418GB less SG traffic over a year. That’s a lot of energy and capacity saved across the SG site portfolio.

    https://i.snipboard.io/8ntP7W.jpg

    Would be more than happy to discuss the details over chat/phone.

    Thread Starter midwestE

    (@edouble74)

    Following up here. Hoping there has been some discussion to improve/fix this issue.

    Thread Starter midwestE

    (@edouble74)

    Following up here again. Problem still persists in most recent version of this plugin.

    Plugin Support Plamen M

    (@plamenm)

    Hello @edouble74 ,

    Thank you for the detailed suggestion. It is already sent to the attention of our plugin developer. I can not guarantee if this feature would be implemented. The final decision which suggestions would be selected for development is theirs and is based on popular demand and the future development plans for the plugin. Currently, you are the only user of the plugin who requested such a feature.

    I would advise you to keep the plugin always up to date and check its changelog to follow for new improvements related to the management of the Cloudflare cache.

    Regards,
    Plamen.M
    Tech Support Team
    SiteGround.com

    Thread Starter midwestE

    (@edouble74)

    Thank you. To be clear, this is a bug, not a feature

    Plugin Support Plamen M

    (@plamenm)

    It would had been a bug, if the purge function did no work. It actually purges the CloudFlare cache properly, however, your suggestion is that it should purge only the cache for the currently edited product / post. This is therefore a feature request, not a bug.

    In all cases, it is sent to the attention of the developers and they will decide if this is in line with the future plans for the plugin.

    Thank you. Regards,
    Plamen.M
    Tech Support Team
    SiteGround.com

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Incorrect “purge_everything” for Cloudflare’ is closed to new replies.