6

Is there any way to have an individual node's cache flushed if it gets updated? (instead of having to flush drupal's entire cache to show the changes to anonymous users immediately? (Rules or a module maybe?)

1
  • This is technically possible. I did this in Drupal 6 with the Boost module. Boost in Drupal 7 hasn't had as much development done so you are out of luck in this case...
    – mikeytown2
    Oct 30, 2012 at 23:09

3 Answers 3

8

If you are using node_save, cache for the particular node will be cleared automatically. If you are using any other method to save the node then use the following,

entity_get_controller('node')->resetCache(array($node->nid));
3
  • Sorry, I need a little more handholding. What do I do with this code? Oct 17, 2012 at 18:53
  • Then you need to specify how you're updating the node? Oct 18, 2012 at 9:13
  • Does this clear the page cache for the node, or some other cache? Which cache bin is affected? Jul 16, 2018 at 17:32
6

This worked for me. The entity_get_controller('node') call provided in the other answer didn't work.

// Clear field cache
$entity->type = 'node';
$entity->id  = 54321;
cache_clear_all("field:$entity->type:$entity->id", 'cache_field');
1
  • I got an error for creating an object out of thin air. But you don't need to. $entitytype = 'node'; $entityid = 54321; cache_clear_all("field:$entitytype:$entityid", 'cache_field');
    – As If
    Apr 27, 2019 at 23:17
1

I believe Views Custom Cache module could be your solution.

Install "Views Custom Cache" module as usual.

Views settings:

Your first argument is Node ID.

Add second argument Custom Global: NULL argument - which is ignored and will not efect the view in any particular way - exept caching!

Ensure that this argument is not present when view is generated so that "When the filter value is NOT available" rule will be triggered.

If your view has path for example "node/nid", nid is available to you as arg(1).

Note: Adjust arg() value (0, 1, 2...) to match your use case in code snippet below.

In Global Null argument: When the filter value is NOT available / Provide default value / PHP code enter:

$node = node_load(arg(1));
return $node->changed;

Now your second argument (Global Null argument) will always return fresh node updated date (timestamp), and you can use "Custom cache: view's all arguments" cashe option.

Global Null argument will be nicely ignored and will not take any efect on view rendering, but it will make effect on cache - making it unique for specified Node ID + Node updated date pair. If node is updated, new Node updated date (timestamp) will be returned as second argument to the view which will cause automatic View cache refresh for specified Node ID.

Answer reference: https://www.drupal.org/node/2559607#comment-10436015

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Dec 4, 2015 at 22:32
  • Thanx for advice @Елин Й. I have improved the answer as suggested.
    – devad
    Dec 6, 2015 at 13:17

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.