Configuring WP Rocket to optimise your site's cache

Procédure

What is WP Rocket?

WP Rocket is a powerful caching system that can dramatically improve the performance of a Wordpress site. Like all caching plugins, it can store pages that have already been loaded. This reduces page load times andimproves the speed of your site.

However, you should be aware that WP Rocket's settings can also have a negative impact on a website if they are incorrectly configured.

This guide will help you to define the ideal settings for optimum performance.

How does the WP Rocket caching system work?

By default, WP Rocket activates cache preloading. This creates the cache files for each page before they are even opened.

The advantage of this is that the page will no longer need to be generated when it is loaded by the web browser and will therefore improve the display speed of your site.

Problems encountered with WP Rocket cache preloading

1. Preloading performed by WP CRON

The WP Rocket cache is preloaded when WP Cron is run, which is used to launch the various Wordpress events. However, this poses a problem in its use, as WP Cron is loaded each time a page is opened. As a result, the cache is preloaded while another page is loading.

This is very problematic. Instead of loading a single page, WordPress will load several pages and the page load time will increase.

What's more, this will also put a huge strain on the I/O of your hosting's storage space whenever a page is loaded.

2. WP CRON timeout

Another problem encountered in our tests is that WP Cron is limited by WP_CRON_LOCK_TIMEOUT. This is a variable which allows you to define the time allocated to WP CRON to carry out the tasks it has to perform. This is defined in the wp-config.php file

As a result, with a limited time for WP CRON, the cache preload will only generate a few pages, then resume when the next page is loaded.

However, the next time a page is loaded, WP Rocket will be forced to reshuffle its entire cache and this will create a significant load on the server's I/O, causing a drop in the site's overall speed.

3. WP Rocket combined with another cache system

If you use a caching system such as Varnish, NGINX, LSCache, etc., you will have periods when the visitor never reaches a single page.

To put it simply, the interactivation between WP Rocket and the cache server will disrupt the preloading of the cache, which will not work at all. It will be suspended, waiting for a WordPress PHP file to be opened, at which point the cache preload will be executed upstream, and a very intense wave of pre-planned operations will be executed, risking saturating the server and blocking the loading of this page.

How can I remedy these problems and optimise my Wordpress site?

1. Increase the cache lifetime

Once the cache is fully preloaded, preloading is no longer a problem as it no longer does anything. However, the preloaded cache may expire and this will restart preloading.

Ideally, the cache should be set to an unlimited lifetime. This is a good idea as it can be configured in WP Rocket. What's more, this plugin is well designed because it deletes the cache of a page, article or product (Woocommerce) if there has been a modification to it. So there's no need to recreate the cache regularly and you can set the lifetime to unlimited. To do this, follow these steps:

  • In the left-hand menu, go to Settings and then click on WP Rocket.

Configuring WP Rocket to optimise your site's cache

  • Once you're in the WP Rocket plugin, go to the "Cache" menu and configure the last item, "Cache clean-up time", by specifying 0 hours to configure unlimited cache time.

Configuring WP Rocket to optimise your site's cache

  • Click Save Changes

Configuring WP Rocket to optimise your site's cache

2. Run a cron job to preload the cache and disable it when WordPress pages load

This will solve the various problems listed above, namely :

  • WP Cron is no longer loaded while a page is loading, so preloading no longer affects page loading times.
  • WP Cron is no longer limited by max_execution_time and WP_CRON_LOCK_TIMEOUT.
  • The cache can be preloaded in a single block, so there's no need to restart the cache and no disk overhead (I/O).
  • WP Cron always executes, whether the page is loaded from PHP execution or from the cache server used (Varnish/NGINX/...).

To do this, two actions need to be carried out:

  • Modify the wp-config.php file

This file is located at the root of your site. You will need to go to the code in this file to add the line "define( 'DISABLE_WP_CRON', true );".

Example :

<?php /** * The basic configuration of your WordPress installation * * This file is used by the script that creates wp-config.php during * the installation process. You don't have to use the website, you * can simply rename this file to "wp-config.php" and fill in the * values. * * This file contains the following configuration settings: * * MySQL settings * Table prefix * Secret keys * Language used * ABSPATH * * @link https://fr.wordpress.org/support/article/editing-wp-config-php/. * * @package WordPress */ // ** MySQL settings - Your host must provide you with this information. // /** WordPress database name. */ define( 'DB_NAME', "xxxxxxxxx" ); /** MySQL database user. */ define( 'DB_USER', "xxxxxxxxx" ); /** MySQL database password. */ define( 'DB_PASSWORD', "xxxxxxxx" ); /** MySQL hosting address. */ define( 'DB_HOST', "xxx.xxx.xxx.xxx" ); /** Character set to be used by the database when creating tables. */ define( 'DB_CHARSET', 'utf8' ); /** * Database collation type. * Only touch this if you know what you're doing. */ define( 'DB_COLLATE', '' ); /**#@+ * Unique authentication and salting keys. * * Replace default values with unique phrases!
 * You can generate random phrases using * {@link https://api.wordpress.org/secret-key/1.1/salt/ the WordPress.org secret key service}. * You can change these phrases at any time, to invalidate all existing cookies. * This will also force all users to log in again.
 * * @since 2.6.0 */ define( 'AUTH_KEY', 'set a unique phrase here' ); define( 'SECURE_AUTH_KEY', 'set a unique phrase here' ); define( 'LOGGED_IN_KEY', 'set a unique phrase here' ); define( 'NONCE_KEY', 'set a unique phrase here' ); define( 'AUTH_SALT', 'set a unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put a unique phrase here' ); define( 'LOGGED_IN_SALT', 'put a unique phrase here' ); define( 'NONCE_SALT', 'put a unique phrase here' ); /**#@-*/ /** * Database prefix for WordPress tables.
 * You can install several WordPress tables on a single database * if you give each one a unique prefix. * Use only numbers, non-accented letters and underscores! */ $table_prefix = 'wp_'; /** * For developers: WordPress debug mode * * By setting the following value to "true", you activate the display of * error notifications during your tests.
 * It is strongly recommended that extension and * theme developers use WP_DEBUG in their * development environment. * * For more information on the other constants that can be used * for debugging, visit the Codex. * * @link https://fr.wordpress.org/support/article/debugging-in-wordpress/ */ define( 'WP_DEBUG', false ); /* That's all, don't touch the following! Happy publishing. */ /** Absolute path to the WordPress folder. */ if ( ! defined( 'ABSPATH' ) ) define( 'ABSPATH', dirname( __FILE__ ) . '/' ); /** Setting variables for WordPress and its included files. */ require_once( ABSPATH . 'wp-settings.php' ); /** Disabling WP_CRON on page load **/ define( 'DISABLE_WP_CRON', true );</code></pre> <p> </p> <ul> <li>Set up a CRON task to override WP_CRON disabling</li> </ul> <p>To do this we'll need to run this command line via a CRON task:</p> <pre> <code class="language-bash">flock /path/to/site/wp-cron.lock php /path/to/site/wp-cron.php</code></pre> <p>For information, <span style="font-size:10pt; font-variant:normal; white-space:pre-wrap"><span style="font-family:Arial"><span style="color:#000000"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none">Flock will prevent two wp-cron.php running at the same time.</span></span></span></span></span></span></p> <p><span style="font-size:10pt; font-variant:normal; white-space:pre-wrap"><span style="font-family:Arial"><span style="color:#000000"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none">If, for example, there are several PHP interpreters on ISPConfig, "php" should be replaced by "php7.3", "php7.4", "/usr/local/php-7.3.8/bin/php", ... depending on the most appropriate interpreter.</span></span></span></span></span></span></span><br /> </p> <p>To set up the CRON task on shared hosting linked to the LWS Panel, simply follow <a href="https://help.lws-hosting.com/en/cron-stain" target="_blank">this documentation</a> and indicate as the script address:</p> <pre> <code>http://votresite.tld/wp-cron.php (replace yourresite.tld with your doamine name)</code></pre> <p> </p> <p>To find out how to set up this CRON task on cPanel hosting, I invite you to follow <a href="https://help.lws-hosting.com/en/program-a-Cron-task-in-cPanel" target="_blank">this documentation</a></p> <p>To find out how to set up this CRON task on an ISPConfig VPS server, I invite you to follow <a href="https://help.lws-hosting.com/en/create-a-CRON-task-from-ISPconfig" target="_blank">this documentation</a></p> <p>To find out how to set up this CRON task on a VPS server via SSH command, I invite you to follow <a href="https://aide.lws.co.uk/a/442" target="_blank">this documentation</a></p> <h2>Useful links</h2> <p>If you'd like to find out more about WP Rocket, I invite you to read this <a href="https://blog.lws-hosting.com/creation-de-sites-web/wp-rocket-presentation-de-ce-plugin-de-cache-wordpress" target="_blank">blog article</a></p>

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

1mn reading

How does Yoast SEO work?

4mn reading

How can you optimise your Wordpress site?

1mn reading

How do I use Gutenberg on Wordpress?

2mn reading

How do I disable the Gutenberg editor on Wordpress?


Ask the LWS team and its community a question