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?)
3 Answers
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));
-
Sorry, I need a little more handholding. What do I do with this code? Commented Oct 17, 2012 at 18:53
-
-
Does this clear the page cache for the node, or some other cache? Which cache bin is affected? Commented Jul 16, 2018 at 17:32
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');
-
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 IfCommented Apr 27, 2019 at 23:17
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
-
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– Elin Y.Commented Dec 4, 2015 at 22:32
-
Thanx for advice @Елин Й. I have improved the answer as suggested.– devadCommented Dec 6, 2015 at 13:17