Help with Running Cronjob to Purge Homepage on PrestaShop with Litespeed

#1
Hi,
I am seeking assistance with configuring a cron job to automatically purge the homepage cache on a PrestaShop website hosted with Litespeed. Specifically, I would like to set up a cron job that runs regularly to ensure that the homepage is refreshed and served with the most up-to-date content.

Here are the details:
  1. I am using PrestaShop version1.7.8.7
  2. The website is hosted on a Litespeed server.
  3. I need the cron job to trigger the purge of the homepage cache periodically (e.g., every hour).

Could someone guide me on how to set this up? If you can provide the correct cron job command or any specific settings within the Litespeed admin panel, it would be greatly appreciated!

Thanks in advance for your help!
 

serpent_driver

Well-Known Member
#2
Name the file as you like it. The code allows only to be executed in console or by cron. If executed only the cache generated by LScache plugin for PrestaShop is purged.

Setup a cron job with intervall of your choice:
Code:
php /absolute_path_to_purge_file/filename_of_your_choice.php
PHP:
<?php

header('x-litespeed-cache-control: no-cache');
if (php_sapi_name() === 'cli') {
    $tag = dirname(__FILE__);
    $tag = str_split(md5($tag));
    $tag = 'PS' . implode('', array_slice($tag, 0, 5));
    header("X-LiteSpeed-Purge:" . $tag);
}else{
    exit();
}
Provided by https://www.cachecrawler.com Kitt - The ulimate Cache Warmup Crawler for LiteSpeed LSache
 
Last edited:
#3
Hi serpent_driver,

Thank you so much for the clear and helpful explanation, as well as for providing the code. Although I'm not a developer, with the help of ChatGPT, I was able to set up the cron job successfully.

I will be testing it in the coming days, and I'll let you know how it goes.

Thanks again for your great support!
 
#6
Hi,

First of all, thanks again for your help! The cron job is working perfectly, and I appreciate the clear explanation and code.

To answer the question about why I need to purge the homepage, here’s my case study:
I’m using the Warehouse template for my PrestaShop store, and my shop imports new products from a supplier via a plugin. The homepage features an area displaying the new arrivals and other category highlights, ensuring visitors can see the latest products at a glance.

My product settings are configured as follows:
Default order by: Product add date
Default order method: Descending

Currently, I have the following cron jobs in place:
  1. A cron job for LiteSpeed that purges the homepage cache to reflect new arrivals.
  2. A cron job that reindexes products to ensure newly imported items appear on the front-end, not just in the back office.
  3. A cron job to clear the site cache to maintain performance and ensure updated content.
  4. A cron job for sitemap generation, which is mainly relevant for SEO purposes.
My question is whether this workflow is necessary or if I am overcomplicating things. Everything seems to be working, but I’d love to hear any suggestions for optimizing this setup.

Thanks in advance for your guidance!
 

serpent_driver

Well-Known Member
#7
A cron job to clear the site cache to maintain performance and ensure updated content.
If site cache is the Smarty cache you shouldn't use this kind of cache as it isn't compatible with LScache.

A cron job for LiteSpeed that purges the homepage cache to reflect new arrivals.
Good idea, but you should warmup the cache after the cache is purged. With the Kitt Cache Crawler for PrestaShop you don't need an extra cron for purging the cache since Kitt can do this job before cache warmup.
https://www.cachecrawler.com/Downlo...LiteSpeed-LScache-PrestaShop-Plugin::115.html
 
#8
Thank you for your helpful response. Based on your suggestion, I wanted to confirm: should I disable the Smarty cache to avoid compatibility issues with LScache?

Also, I’ve attached a screenshot of my current setup for reference. Could you please take a look and let me know if everything is set up correctly or if any adjustments are needed?

Looking forward to your feedback, and thanks again for your support!

screencapture.png
 

serpent_driver

Well-Known Member
#15
FYI: You don't need 4 or 3 not synchronized cron jobs, but only 1 cron job. The first cron job for product reindex starts the next process if finished and starts the process for sitemap generation and if finished it starts the process to purge the cache. This procedure needs only 1 cron job and can be done with a single line of code.

Benefit: All processes are running synchronized without delay.
 
#16
Hi serpent_driver,
Thanks for your suggestion on synchronizing the cron jobs into one. Based on your advice, I’ve created the following single cron job using ChatGPT:

0 7,17 * * * curl "https://persiani.domain.com/module/...67021028d1ddbe68d_shop=1&action=index-missing" >/dev/null 2>&1 && curl "https://persiani.domain.com/admin12...en=aKffTtPqP12YZydwJB9_w78fH6cXOk_rbs1LjMZaFs" >/dev/null 2>&1 && curl "https://persiani.domain.com/modules/sitemappro/cron.php?id_shop=1" >/dev/null 2>&1 && curl "https://persiani.domain.com/lite_speed_purge_home/purge_home.php?id_shop=1" >/dev/null 2>&1 && echo "Cron job completed successfully" | mail -s "Cron Job Completed Successfully" info@domain.com || echo "Cron job failed" | mail -s "Cron Job Failure Alert" info@domain.com

This job is set to run at 7:00 AM and 5:00 PM every day.
Could you please confirm if this looks correct to you and if it will synchronize all processes as intended?

Thanks again!
 
#17
Unfortunately, ChatGPT did not do a good job because ChatGPT misunderstood "synchronize". In the specific context, synchronize does not mean that all cron jobs should be executed at the same time to turn 4 cron jobs into just 1. The generated code does exactly that and that is not helpful.

ChatGPT also suggests curl. This requires a network connection. Since curl is an HTTP client, this is comparable to starting the cron job via a browser request. If there is a network failure, the cron job in question is aborted. All in all, ChatGPT did solve a task, but the result was poor and you should not use it for many reasons. ChatGPT would have solved the task better if you had given ChatGPT more details. When using ChatGPT, always remember that ChatGPT only does what you tell ChatGPT to do. No more, but also no less! ChatGPT is AI, but this AI is not yet mature enough to question a user request or to formulate it differently. ChatGPT thinks in a solution-oriented way, not a problem-oriented way. That's why ChatGPT is only as good as the amount of details you give ChatGPT.

The correct solution is to extend each script by 1 line of code. This means that after one process has finished, the next process is started automatically and you only need 1 cron job for this. All processes run synchronized, means one after the other without delay between each process.

The first code starts the cron job.
PHP:
/opt/alt/php83/usr/bin/php /absolut_server_path_to_relevant_script_file.php

Add each different code to every script to start the next process if the current process is finished.

PHP:
shell_exec("/opt/alt/php83/usr/bin/php /absolut_server_path_to_relevant_script_file_2.php");

shell_exec("/opt/alt/php83/usr/bin/php /absolut_server_path_to_relevant_script_file_3.php");

shell_exec("/opt/alt/php83/usr/bin/php /absolut_server_path_to_relevant_script_file_4.php");

....
FYI: Your current hosting plan at A2 supports it.
 
Top