简体   繁体   中英

clear drupal cache - php script

I'm writing a php script which will enable people to change the theme of their drupal website. So far, so good but one last thing I couldn't figure out. Every time when I submit the form, the database is changed but the theme doesn't change. Apparently, I have to clear the cache as well. I found this on the Drupal website:

<?php include_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); drupal_flush_all_caches(); ?>

I should make a little file 'clear.php' with this script, and every time I want to clear the cache, I should go to this file and the cache shout be cleared...

But their is my problem. I don't know how to call this page in my script. Sure I can make a button which will redirect the user to this page, but I'ld like it in one script.

Any idea's? Or are their other ways to flush the Drupal cache using php?

Thanks in advance!

some tips:

  • Why not calling drupal_flush_allcaches(); in the _submit hook of your form? then you don't need to bootstrap.
  • Also you can create a menu entry with a function callback in a custom module with hook_menu and then put there your snippet. Again without need to bootstrap.

  • If really needs to be a separate script, put it in the root folder of your installation and then call mysite.com/clear.php . If you put it somewhere else, you should change the path to bootstrap.inc (because it's a relative path)

  • Don't flush all the caches, when you only need to flush 1 of them: Try cache_clear_all('theme_registry', 'cache', TRUE);

Tadaa, did it :)

Just paste this piece op php in my script:

$deletecachesql = "DELETE FROM cache"; $deletecachequery = mysql_query($deletecachesql) or die ("error").mysql_error(); $deletecacheresult = mysql_fetch_array($deletecachequery);

The script does clear the cache, but I'm not sure it's a good thing to do. The website also told me to delete:

  • DELETE FROM cache;
  • DELETE FROM cache_menu;
  • DELETE FROM cache_filters;
  • DELETE FROM cache_page;
  • DELETE FROM watchdog;

Is it a wise thing to do? To clear (delete) the cache like this?

Be careful with clearing your cache programmatically with mysql-statements.

This might break your entire drupal registry and you might end up with a blank page on drupal bootstrap so even drush might fail.

The only way fixing this is again is using "registry_rebuild" from here: https://drupal.org/project/registry_rebuild

I tried to clear the cache manually yesterday and ended up that way. I strongly suggest to keep using things like:

cache_clear_all(NULL, 'cache_page');
OR
drupal_flush_all_caches();

Open settings.php (/sites/default/settings.php) in any plain text editor. Add this line to the end of the file and save it:

$settings['rebuild_access'] = TRUE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM