May 11, 2023

How to properly clean the Varnish Cache in a WordPress blog.

A snippet of PHP code to integrate into your theme to clean your Varnish Cache in a fast, versatile and selective way.

Varnish Cache is an extremely powerful and flexible software technology that is widely used as an HTTP accelerator. This tool acts as an intermediary between the server and the client, storing data (or "caching") to reduce the load on the server and provide faster responses to clients.

Varnish Cache Reverse Proxy Mechanism

The benefits of Varnish Cache.

The benefits of Varnish Cache are many. First of all, it significantly increases website performance by reducing page loading time and saving server resources. Secondly, it improves the scalability of websites by allowing them to handle more requests at the same time. Finally, it offers unparalleled flexibility thanks to its VCL (Varnish Configuration Language) configuration language, which allows you to define specific caching rules for each case.

Varnish and the TTFB

Varnish Cache has a significant impact on Time to First Byte (TTFB), a key indicator of website performance. TTFB is the time between when a client (such as a web browser) sends an HTTP request and when it receives the first byte of data from the server. Varnish Cache drastically reduces TTFB by storing server responses and quickly serving them to clients, avoiding the need to reprocess requests.

A lower TTFB means content is delivered to users faster, improving the user experience and perception of site speed. But it's not just the user experience that benefits: search engines like Google also appreciate the faster response times.

Varnish and Google's Budget crawl

Google uses a limited resource, known as a "crawl budget," to index websites. This budget is the number of pages that Google is able and willing to crawl in a given period of time. A lower TTFB means Google can crawl and index more pages within this budget, optimizing the use of its crawling resources.

Improving your crawl budget can have a direct impact on Google's ability to index your website in a timely manner. This, in turn, can positively influence your site's ranking in search results, as more comprehensive and timely indexing gives Google a more accurate view of your site and its content. In this way, the use of Varnish Cache can help improve both the speed of the site and its ranking in search results.

Stats Google Crawl Response Time

High profile websites like The New York Times, BBC, Wikipedia have already adopted Varnish Cache to improve their performance and ensure a smooth and responsive user experience.

Varnish and WordPress

WordPress is the most popular blogging platform in the world and Varnish Cache fits its needs perfectly. However, managing Varnish Cache in a WordPress environment can be tricky. Fortunately, there are several plugins available that can help with this process, including W3 Total Cache e Varnish HTTP Purge.

Despite their usefulness, these plugins can present serious problems.

W3 Total Cache, for example, can be extremely slow at clearing the cache, especially when dealing with multiple categories. This can lead to significant delays in publishing posts, interfering with editorial activity and potentially causing errors such as Gateway Timeout.

NGINX Gateway Timeout

Varnish HTTP Purge, while being more streamlined and focused on cleaning the cache only, has proven to be unreliable on several occasions. Instead of selectively and granularly cleaning the cache, it often cleans the entire cache, which can have a significant impact on site performance.

To solve these problems, we propose a snippet of PHP code to insert in the functions.php file of your active WordPress theme. This code takes care of clearing the Varnish cache every time a post is published (except for drafts), ensuring that the home page, the article itself and all related categories are correctly updated.

The proposed code does not purport to deal with WooCommerce-installed WordPress installations, product taxonomies, AMP tags, or APIs; however, it is quite simple to understand and easily extendable to adapt to the most disparate needs.

PHP Code for WordPress to PURGE Varnish.

// Funzione che pulisce la Cache Varnish tramite metodo PURGE su socket RAW senza l'utilizzo di cURL


function purgeURL($URL) {

$varnishIP = "127.0.0.1";
$varnishPORT = "80";

$hostwp = trim(str_replace( array( 'http://', 'https://' ), '', get_option('siteurl')), '/' );

// Open the socket
$errno = ( integer) "";
$errstr = ( string) "";
$varnish_sock = fsockopen( "127.0.0.1", "$varnishPORT", $errno, $errstr, 10);

// Prepare command to send
$cmd = "PURGE $URL HTTP/1.0\r\n";
$cmd .= "Host: $hostwp\r\n";
$cmd .= "Connection: Close\r\n";
$cmd .= "\r\n";

// Send the request
fwrite( $varnish_sock, $cmd);

// Close the socket
fclose( $varnish_sock);
}

// In fase di salvataggio POST recupero gli slug delle categorie che contengono il post, ed invoco la pulizia della cache Varnish
// tramite la funziona purgeURL

function pulisci_cache_categorie ($post_id){

if ( wp_is_post_revision( $post_id ) ) {
return;
}

$categories = get_the_category($post_id);

// Inizializzare un array vuoto per i permalink
$relative_permalinks = array();

// Iterare su ogni categoria
foreach ($categories as $category) {
// Ottenere il permalink della categoria e convertirlo in un URL relativo
$relative_permalink = wp_make_link_relative(get_category_link($category->term_id));

// Aggiungere l'URL relativo all'array
$relative_permalinks[] = $relative_permalink;
}

foreach($relative_permalinks as $relative_permalink) {
purgeURL ($relative_permalink);
}

}

add_action( 'save_post', 'pulisci_cache_categorie' );

The provided code is written in PHP and is part of the file functions.php of a WordPress theme. This code defines two functions: purgeURL() e pulisci_cache_categorie().

  • purgeURL($URL): This feature is designed to interface with a Varnish server, a very powerful web application accelerator, to invalidate or "purge" a particular URL from the cache. The Varnish server is configured to reside locally (“127.0.0.1”) and communicate on port 80. The function creates a socket connection with the Varnish server and sends a “PURGE” command for the specified URL. This command tells Varnish to remove any cache corresponding to that URL, forcing Varnish to fetch a fresh copy of the page the next time it's requested. After sending the command, the function closes the socket connection.
  • pulisci_cache_categorie($post_id): This function is linked to the 'save_post' WordPress action, which fires every time a post is saved. When a post is saved, this function is called with the post ID as an argument. If the post is a review (that is, it's not the "live" version of the post), the feature terminates immediately. Otherwise, it fetches all the categories the post belongs to and builds an array of relative URLs corresponding to these categories. For each of these URLs, call the function purgeURL(), causing these pages to be removed from the Varnish cache. This ensures that when a post is edited, all category pages that include that post are updated in the cache, thus reflecting the edits to the post.

These features work together to make sure that post changes in WordPress are reflected in your category pages in a timely manner, while retaining the performance benefits of a web cache like Varnish.

A focus on performance thanks to fsockopen().

In the context of network calls in PHP, fsockopen() and cURL are two popular methods of making HTTP requests. While cURL is known for its versatility and wide range of configuration options, fsockopen() may offer speed advantages in certain scenarios.

fsockopen() opens a direct socket to a server, allowing for faster and less resource intensive low-level communication. This can be especially beneficial when you are making many simultaneous requests or have very low latency needs. Furthermore, fsockopen() may be faster in situations where the server's response is not needed or can be ignored, since there is no need to wait for the response to be returned and processed as it would be with cURL.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

JUST A MOMENT !

Would you like to see how your WooCommerce runs on our systems without having to migrate anything? 

Enter the address of your WooCommerce site and you will get a navigable demonstration, without having to do absolutely anything and completely free.

No thanks, my customers prefer the slow site.
Back to top