10

We have a custom services module and a site in Drupal 7, and I got the task to delete the cache after the editor adds new content or update the existing ones.

I've read about a module called Cache Actions, but, as i said, i need to create the function on my custom module.

Also, I've started reading about Drupal hooks, but still don't have a clue of how to implement this task. Any suggestions? Thanks.

  • This could be implemented in a few ways. How often does your editor update/delete/add new content? – Peter Oct 23 '12 at 22:30
  • Very often, because we use drupal as the manager of the data to make web services for mobile apps. I couldn't tell you exactly how frequently we add or modify data, but the main request is to clear the cache every time a change happens. – Erzei Oct 24 '12 at 1:00
11

It sounds like you've already got a custom module, but just in case you haven't see the Module Developer's Guide.

The hooks you're looking for are hook_node_insert() and hook_node_update(), and the function to clear the cache will either be cache_clear_all() or drupal_flush_all_caches. The former will let you clear specific cache bins (and even specific cache items), and the latter will clear absolutely everything.

For example:

function MYMODULE_node_insert($node) {
  drupal_flush_all_caches();
}

or to clear only the static page cache after a node has been updated:

function MYMODULE_node_update($node) {
  $nodeurl = url('node/'. $node->nid, array('absolute' => TRUE));
  cache_clear_all($nodeurl, 'cache_page');
}
  • Ok, i understand it. Just one more question, do i have to add the code in my .module file or in the node module file of drupal? – Erzei Oct 24 '12 at 1:14
  • 2
    It goes in your own module file, hacking core files is a big no-no in Drupal :) – Clive Oct 24 '12 at 23:32
1

You can easily clear specific node cache after updating them. Ex. :

function MYMODULE_node_update($node) {
  $nodeurl = url('node/'. $node->nid, array('absolute' => TRUE));
  cache_clear_all($nodeurl, 'cache_page');
}

Please note that you must need to specify array('absolute' => TRUE).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.