Procédure
Why speed up your site's loading speed?
It's good to know that the loading speed of your site is crucial to its success. For a company, for example, it gives a first impression to visitors. What's more, if your site takes too long to load , this can have an impact on its ranking on the main search engines, and will not provide sufficient exposure to keep your site alive.
There is no limit to the loading time of your web pages. However, most websites take less than three seconds to load.
Speed up the loading of your pages by taking advantage of the advanced cache offered by LWS Cache, a system designed to smooth requests via NGINX, considerably reduce the execution time of scripts and lighten your servers. Do you have a WordPress site? Manage this cache with LWS Optimize, recognised as the best Wordpress cache plugin optimised to improve overall performance. It is available for free on the official directory as the best free Wordpress cache plugin.
What is LWS Cache and what is it used for?
The LWS Cache tool is a system designed and developed by LWS tooptimise the loading performance of your website through the use of advanced caching mechanisms configured at server level. The tool uses technologies provided by NGINX.
NGINX is a performance-oriented web server that can handle many more requests than Apache (see our blog post entitled "Apache VS Nginx: Performance testing").
With the right configurations in place, NGINX can handle more requests to your website, speeding up page load times while reducing CPU and memory consumption.
How does the LWS Cache plugin work?
1. Working without a cache
To get a better understanding of how it works, we'll start by looking at how it works without a cache plugin so that visitors can see your site.

Processing an HTTP request without LWS Cache
1. The visitor requests the page from the web server. Example: index.php
2. The web server executes the necessary scripts (PHP, Perl, NodeJS, etc.)
3. The web server receives the result of the execution.
4. The web server sends the HTML page resulting from the script execution.
2. Operation with the LWS Cache plugin
When LWS Cache is activated, a cache server is inserted between the visitor and the web server.
The aim is to reduce the number of script executions required by storing the result of the execution in memory for future requests requiring the same response. This eliminates the need to run the same script several times to achieve the same result.
This eliminates the time spent waiting for the script to be executed on the page load time, and at the same time saves the resources used to execute the script.

Processing an HTTP request with LWS Cache
1. The visitor requests the page from the web server. Example: index.php
2. LWS Cache checks whether the page has already been generated and stored in the cache.
- If yes, the page is returned directly to the visitor without the need to access the web service and without executing the script.
- If not, the page is requested to be generated on the web service resulting in the execution of the script (PHP, NodeJS, Perl, Ruby, etc.).
3. Once the page has been generated, LWS Cache determines whether the page can be cached (via headers, URL, etc.).
- If yes, the page is saved in the cache and sent back to the visitor
- If not, the page is saved in the microcache (short-lived cache) and sent back to the visitor.
We can see that when a page is stored in the cache memory, processing by the web server and script execution are avoided.
Cacheable, non-cacheable and microcache
When the web service provides a new response to LWS Cache, it is analysed to determine whether or not it should be kept in cache memory for future use.
Some page content should not be cached, such as the result of a registration form, the result of a payment page, etc., as they contain data that varies according to users and events.
To determine whether a page can be cached or not, LWS Cache uses several mechanisms:
- HTTP requests using HTTP verbs other than HEAD and GET will be systematically excluded from the cache.
- If the web server response contains browser cache instructions (header cache-control), this will automatically be considered by LWS Cache and the page will be kept in the cache memory.
If an HTTP request is of the GET type and there is no specific browser cache instruction, the cache is kept in the microcache for a few seconds.
The microcache can therefore be used to overcome concerns about peaks in requests for non-cached pages. For example, this solves the problem of overloads and slowness in the event of a wave of searches for the same product on an ecommerce shop.
Interface presentation and plugin activation
LWS Cache appears as an icon in the LWS panel in the "Optimisation and Performance" section.

Once you are in the interface, select the "Activate" button and then "Submit". LWS Cache will be activated on your hosting within a few minutes.

Clearing the cache
In addition to the LWS Panel interface for purging the cache, you can also purge the cache using the following methods:
1. Clearing the cache with LWS Optimize for Wordpress
The LWS Optimize WordPress plugin lets you, among other optimisation options, automatically purge the dynamic cache (LWSCache) of your pages when you make changes to your site. To do this, go to the plugin settings and then to the 'Caching' tab.


Then activate the 'Dynamic cache' option to enable automatic purging.

If you want to clear the LWSCache cache manually, click on the "Clear cache" button to clear it completely.

2. Clearing the cache with LWS Cache for Wordpress
The Wordpress LWS Cache plugin lets you automatically purge the cache of your pages when you modify them or add/approve comments.
The plugin is provided in all automatic Wordpress installations via the LWS Panel.
To manage the plugin, once logged into your Wordpress admin console, go to the "LWS Cache" menu.

From the settings page, you can enable/disable automatic emptying, define when to automatically empty the LWS Cache and completely purge the cache.
Note that you need to tick the "Enable automatic flush" box for all the options to appear.

A button for emptying the entire cache can be found anywhere in the Wordpress admin console in the quick access bar at the top of the screen.

2. Clearing the cache via cURL
The authentication token required to execute these commands is available in your LWS Panel customer area.
Purge the cache of a page
curl -H 'lwsapitoken: ' 'http://mon-site-web.com/--api/cache-purge/exemple.php' This will purge the cache of the URL 'http://mon-site-web.com/exemple.php'.
Completely purge a site's cache
curl -H 'lwsapitoken: ' 'http://mon-site-web.com/--api/purge-cache/*'
3 return codes are possible:
HTTP code 200: the purge was successful with no errors.
HTTP code 404: no caching for the requested resource.
HTTP Code 403: The token specified is not valid for the domain.
3. Purge via PHP code
Purge URL: =$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/--api/cache-purge' . $_SERVER['REQUEST_URI'];?>
Result: =$result;?>
HTTP Code: =$httpCode;?>
This code is used to empty the cache of the PHP file in which it is located.