Jump to content
intershop

clear smarty cache automatically ?

Recommended Posts

i have a site of 4 giga .  i have a hosting of 10 giga .  my cache of prestashop if i forget to clear after 10 days is 11 giga so i have problems with the hosting company . if i clear smarty cache and autoload cache then it goes again to 4 giga .  CAN I do this automaticaly for example every day or every week ?

Share this post


Link to post
Share on other sites

What I would do is create a php script that would execute the clear cache functions, the same as would be done when you click "clear cache" in the back office.

 

The script should work assuming you place the script in the root folder of your store

<?php

require(dirname(__FILE__).'/config/config.inc.php');

//change 'abc123' to any value you wish to use
$token = Tools::getValue('token');
if (!$token || $token != 'abc123')
     die();

Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
PrestaShopAutoload::getInstance()->generateIndex();

Then create a new cronjob that would execute your php script at the interval you desire, and you can test this from a browser to ensure it works properly

http://www.yourdomain.com/yourscript.php?token=abc123

 

 

Edit 5/16: Updated script to include a token

Edited by bellini13 (see edit history)
  • Like 4

Share this post


Link to post
Share on other sites

i would suggest securing this file so that bots and other users do not stumble onto it and start clearing your cache randomly on you

Share this post


Link to post
Share on other sites

How do you "secure" it?

 

Also, when I run it how do I know for sure it's actually working and clearing the cache?

Edited by lateral (see edit history)

Share this post


Link to post
Share on other sites

What version are you using there? We do not have the cache problem here.

Share this post


Link to post
Share on other sites

I don't understand your question...are you asking me what version of Prestashop I'm using...1.6.0.9.

 

I have needed to Clear the cache on a number of occasions to resolve issues and I though that I would set up an automatic Cron Job task to run a Clear Cache on a regular basis...

Share this post


Link to post
Share on other sites

Ok thank you work good

is possible detele also , class_index.php  in root/cache

thank you

Share this post


Link to post
Share on other sites

to clear cache...advance parameters---performance...top right corner....clear cache.

Share this post


Link to post
Share on other sites

Hi Bellini13

 

I created the PHP file and executed it from the browser to test it but it seems to run very quickly compared to when I manually clear the cache from the back office.

 

For example, from the browser it takes 1-2 seconds, from the back office it always takes 14-15 seconds.

 

Do I need to also create a security key and use that from the browser???

 

How can I visually confirm if the cache has been cleared? Is there a set of files I can "see" if they have bee deleted?

 

Thanks

Regards

Greg

Share this post


Link to post
Share on other sites

to clear cache...advance parameters---performance...top right corner....clear cache.

reading is fundamental...  they asked if they can run a script to automate the process.  I guess if you are Homer Simpson, you could use a pencil attached to a lever to click a button every 24 hours...  for the rest of us, we create scripts and run them via cron so we can walk away from the computer

  • Like 1
  • Haha 2

Share this post


Link to post
Share on other sites

Do I need to also create a security key and use that from the browser???

No, but its not a bad idea

 

How can I visually confirm if the cache has been cleared? Is there a set of files I can "see" if they have bee deleted?

yes, under /cache/smarty/cache and /cache/smarty/compile

 

The sub files and folders should be removed

  • Like 1

Share this post


Link to post
Share on other sites

Hi Bellini13,

 

It's working!

 

I ran some tests and looked a the folders under the cache folder and they are being deleted!

 

What is the best and easiest way to secure the file?

 

Thanks again.

 

Also, regarding the use of a security key, are you saying that I don't really need to use a key because I'm not accessing the database?

 

Regards

Greg

Edited by lateral (see edit history)

Share this post


Link to post
Share on other sites

The only thing the script is doing is clearing the cache on the file system.  The only reason to secure the file is to prevent a bot from indexing it and clearing the cache on you, or from someone stumbling onto it and clearing your cache. 

 

Using a token would help prevent those things from happening if they do not know what the token value is.

 

I've updated the original post to add that logic

Share this post


Link to post
Share on other sites

Hi Bellini13

 

Can you please do me a really big favour and look a this other post of mine regarding running an Update query from a script that is run as a Cron task?

 

https://www.prestashop.com/forums/topic/441190-run-database-mysql-update-query-from-prestashop-cron-task-or-sql-function-in-backend/?do=findComment&comment=2049896

 

I feel I'm almost there but need an expert to tell me what's wrong...

 

Thanks

 

Regards

Greg

Edited by lateral (see edit history)

Share this post


Link to post
Share on other sites

Hi, I'd like to use script.

 

How can I use token function?

 

If i set $token != 'abc123'

where i should use it? thanks

Share this post


Link to post
Share on other sites

Hi

 

Very useful thread! Thanks Bellini13 for the script!

 

Will it work with PS v 1.6.1.3 as well?

 

Thank you.

Share this post


Link to post
Share on other sites

What I would do is create a php script that would execute the clear cache functions, the same as would be done when you click "clear cache" in the back office.

 

The script should work assuming you place the script in the root folder of your store

<?php

require(dirname(__FILE__).'/config/config.inc.php');

//change 'abc123' to any value you wish to use
$token = Tools::getValue('token');
if (!$token || $token != 'abc123')
     die();

Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
PrestaShopAutoload::getInstance()->generateIndex();

Then create a new cronjob that would execute your php script at the interval you desire, and you can test this from a browser to ensure it works properly

http://www.yourdomain.com/yourscript.php?token=abc123

 

 

Edit 5/16: Updated script to include a token

 

Hi bellini13,

 

Thanks for the script. I have upload the php file and setup the cronjob, that look to be ruining fine. But cache seems not to be clearing/deleting.

 

Natebc

Share this post


Link to post
Share on other sites

Hello,

 

has any one had issue with is script? We have been told by our website hosting company says that is dose not work. We are in need of this in a big way. We have 2-4 GB upload every day.

 

If any one can help up please let us know.

 

Natebc

Share this post


Link to post
Share on other sites

Hello,

 

has any one had issue with is script? We have been told by our website hosting company says that is dose not work. We are in need of this in a big way. We have 2-4 GB upload every day.

 

If any one can help up please let us know.

 

Natebc

what version of Prestashop are you using? 

 

what exactly 'does not work'

Share this post


Link to post
Share on other sites

what version of Prestashop are you using? 

 

what exactly 'does not work'

Hi Bellini13,

 

We are using 1.6.1.1. We use Total import pro to automatically update 15-16k products twice a day. This leaves 4-5GB in cache. We have copy and past the script and place that file in to root folder. Then set up a cron job. But it will not empty any cache in the folders.

 

Natebc

 

Share this post


Link to post
Share on other sites

As the original post states, this functions exactly like clicking the "clear cache" button in the back office performance page.  It clears

Smarty Cache

XML Cache

Media Cache

 

So the questions is... when you click the "Clear cache" button in your back office, does this "cache" you refer to also get cleared?  My guess is that the answer is no.

Share this post


Link to post
Share on other sites

Hello Bellini

 

I'm also facing same problem - prestashop version 1.6.1.4

 

i've created the  file task.php and save it in to the root folder

 

<?php
require(dirname(__FILE__).'/config/config.inc.php');
$token = Tools::getValue('token');
if (!$token || $token != 'crontask')
     die();
Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
PrestaShopAutoload::getInstance()->generateIndex();

 

 

and tested my site following  url format

 

https://www.test.com/task.php

 

not working

 

 

when i click the "Clear cache" button in  back office it's working

 

Regards

Ramesh

Edited by rameshuv (see edit history)

Share this post


Link to post
Share on other sites

@Ramesh: You did not pass a token in the URL.  go back through the replies

Share this post


Link to post
Share on other sites

did you update the script to check for your token value 'crontask'

 

what exactly does 'not working' mean?

Share this post


Link to post
Share on other sites

did you update the script to check for your token value 'crontask'

 

what exactly does 'not working' mean?

Yes i updated as mentioned below 

<?php

require(dirname(__FILE__).'/config/config.inc.php');

$token = Tools::getValue('token');

if (!$token || $token != 'crontask')

     die();

Tools::clearSmartyCache();

Tools::clearXMLCache();

Media::clearCache();

PrestaShopAutoload::getInstance()->generateIndex();

 

It's not deleting clear smarty cache files automatically with this script.

 

I can do it manually from back office "Clear cache" button its perfectly working.

 

Regards

Ramesh

Edited by rameshuv (see edit history)

Share this post


Link to post
Share on other sites

What makes this class ?

PrestaShopAutoload::getInstance()->generateIndex();

Share this post


Link to post
Share on other sites

Could anyone help a dummy and provide the full php file and where to put it in the root?

 

I created a file with the code provided as clear_cache.php and created the cron job for it but it aint working

Share this post


Link to post
Share on other sites

No, but its not a bad idea

 

yes, under /cache/smarty/cache and /cache/smarty/compile

 

The sub files and folders should be removed

Hi,

I have presta 1.6.1.10, i tried to clear cache by code, but modules folders in cache/smarty/cache are not removed,

You know why?

Thanks.

Edited by tarek.fellah (see edit history)

Share this post


Link to post
Share on other sites

Hello,

i need to make a cron job to clean the cache. In this thread there is a script from 2014. We are in 2017 and the prestashop version is about 1.6.1.x

Is this script still valid ? I want to try it and post here the results but how can i be sure that the script gets the work done ?

Share this post


Link to post
Share on other sites

Hello,

i need to make a cron job to clean the cache. In this thread there is a script from 2014. We are in 2017 and the prestashop version is about 1.6.1.x

Is this script still valid ? I want to try it and post here the results but how can i be sure that the script gets the work done ?

<?php

require(dirname(__FILE__).'/config/config.inc.php');

//change 'abc123' to any value you wish to use
$token = Tools::getValue('token');
if (!$token || $token != 'abc123') {
echo "Wrong token";
die();	
}
     
Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();
echo "Cache cleared";
?>

this works, but you need to keep attention. It seems it doesn't work if you have cache on /tmp mounted on tmpfs and PrivateTmp on systemd config for php-fpm is set to true...

so, or you keep the tmp on disk or you need to set PrivateTmp to false on /usr/lib/systemd/system/php-fpm.service (here on CentOS 7) and then reload the systemd daemon and restart php-fpm. So, tmp must be set as usual and not on systemd-private-***/tmp/...

that's all

Edited by mmsh (see edit history)

Share this post


Link to post
Share on other sites

Hello!

 

I've added a new Category but is not shown, if i go to Advanced>Performance>Clear Cache when the page is reloaded the new category appears.

I need to clear the cache by code. I am currently executing this code:

<?php
ini_set('memory_limit', '1024M');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require('/home/h32w38t0/wear.mixndream.com/config/config.inc.php');

Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();
echo "Cache cleared";

?>

I had to remove  the code below because it would say "Wrong token", $token is empty.

$token = Tools::getValue('token');
if (!$token || $token != 'abc123') {
echo "Wrong token";
die();	
}

 My version is 1.7.4.2.

Can you help me?

 

Thanks!

Share this post


Link to post
Share on other sites

I know this is an old thread, but is the clear cache equivalence to removing /var/cache folder? If this is correct then, we can just setup a script to remove /var/cache folder at specific time. No need to make the script as it's not best security practice to send argument to execute php script

Share this post


Link to post
Share on other sites

After gone through some testing, I found out that in the latest prestashop 1.7, when you clear cache, the only cache folders that is being used is in /var/cache/*

There are 2 folders inside that /var/cache/ called Prod and Dev,

If you enable debug mode, the cache folder will go inside /var/cache/dev and if you disable debug, it will go into /var/cache/prod. Also it has been mentioned that prestashop 1.7 doesn't use /cache/* folders: here

So, with this information, the solution to delete prestashop cache in prestashop 1.7 manually is to write a script to delete all files inside /var/cache/* and this script can run X time.

Search about prestashop cronjob (maybe they have ready module) + how to delete folder in php script.

 

 

 

 

Share this post


Link to post
Share on other sites
Le 30/06/2020 à 7:28 PM, arafatx a dit :

Search about prestashop cronjob (maybe they have ready module) + how to delete folder in php script.

hi

I haven't tested it

 

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)
6 hours ago, solsol69 said:

hi

I haven't tested it

 

Yes, you can use this module. The method it uses to clear cache is the same that @mmsh describes in his post in this thread.

It has two execution methods available: through own module (you can set execution time at settings), or through a CRON task (you'll get an URL you can use to program it at CRON job).

I posted the module in Spanish forum, but it's also in english, so you can use it and settings will appear in english.

Edited by Prestafan33 (see edit history)

Share this post


Link to post
Share on other sites
On 7/5/2020 at 3:12 AM, Prestafan33 said:

Yes, you can use this module. The method it uses to clear cache is the same that @mmsh describes in his post in this thread.

It has two execution methods available: through own module (you can set execution time at settings), or through a CRON task (you'll get an URL you can use to program it at CRON job).

I posted the module in Spanish forum, but it's also in english, so you can use it and settings will appear in english.

Since you are the developer of this module,  I have a question:

Does deleting the /var/cache folder manually equivalent to cleaning everything using this module ?

Share this post


Link to post
Share on other sites
Posted (edited)

It should be the same, but it also deletes XML cache and theme cache.

This module only makes the same you were talking about in this thread, cleaning cache using this:

Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();

It also adds the ability to set a time for cleaning repeatedly. But the way it works is the same as other people were posting here.

You can check at Tools.php and Media.php files the content of those methods:

/**
* Clear Smarty cache and compile folders.
*/
public static function clearSmartyCache()
{
	$smarty = Context::getContext()->smarty;
	Tools::clearCache($smarty);
	Tools::clearCompile($smarty);
}

/**
* Clear XML cache folder.
*/
public static function clearXMLCache()
{
	foreach (scandir(_PS_ROOT_DIR_ . '/config/xml', SCANDIR_SORT_NONE) as $file) {
		$path_info = pathinfo($file, PATHINFO_EXTENSION);
		if (($path_info == 'xml') && ($file != 'default.xml')) {
			self::deleteFile(_PS_ROOT_DIR_ . '/config/xml/' . $file);
		}
	}
}

/**
* Clear theme cache.
*/
public static function clearCache()
{
	$files = array_merge(
		glob(_PS_THEME_DIR_ . 'assets/cache/*', GLOB_NOSORT),
		glob(_PS_THEME_DIR_ . 'cache/*', GLOB_NOSORT)
	);

	foreach ($files as $file) {
		if ('index.php' !== basename($file)) {
			Tools::deleteFile($file);
		}
	}

	$version = (int) Configuration::get('PS_CCCJS_VERSION');
	Configuration::updateValue('PS_CCCJS_VERSION', ++$version);
	$version = (int) Configuration::get('PS_CCCCSS_VERSION');
	Configuration::updateValue('PS_CCCCSS_VERSION', ++$version);
}

 

Edited by Prestafan33 (see edit history)

Share this post


Link to post
Share on other sites
Posted (edited)
22 hours ago, Prestafan33 said:

It should be the same, but it also deletes XML cache and theme cache.

This module only makes the same you were talking about in this thread, cleaning cache using this:


Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();

It also adds the ability to set a time for cleaning repeatedly. But the way it works is the same as other people were posting here.

You can check at Tools.php and Media.php files the content of those methods:


/**
* Clear Smarty cache and compile folders.
*/
public static function clearSmartyCache()
{
	$smarty = Context::getContext()->smarty;
	Tools::clearCache($smarty);
	Tools::clearCompile($smarty);
}

/**
* Clear XML cache folder.
*/
public static function clearXMLCache()
{
	foreach (scandir(_PS_ROOT_DIR_ . '/config/xml', SCANDIR_SORT_NONE) as $file) {
		$path_info = pathinfo($file, PATHINFO_EXTENSION);
		if (($path_info == 'xml') && ($file != 'default.xml')) {
			self::deleteFile(_PS_ROOT_DIR_ . '/config/xml/' . $file);
		}
	}
}

/**
* Clear theme cache.
*/
public static function clearCache()
{
	$files = array_merge(
		glob(_PS_THEME_DIR_ . 'assets/cache/*', GLOB_NOSORT),
		glob(_PS_THEME_DIR_ . 'cache/*', GLOB_NOSORT)
	);

	foreach ($files as $file) {
		if ('index.php' !== basename($file)) {
			Tools::deleteFile($file);
		}
	}

	$version = (int) Configuration::get('PS_CCCJS_VERSION');
	Configuration::updateValue('PS_CCCJS_VERSION', ++$version);
	$version = (int) Configuration::get('PS_CCCCSS_VERSION');
	Configuration::updateValue('PS_CCCCSS_VERSION', ++$version);
}

 

Thanks a lot for this information. I thought that in prestashop 1.7, the theme cache also went inside /var/cache folder. Now I understand.

Edited by arafatx (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More