Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal + page fragment caching

Tags:

caching

drupal

our drupal site has some user specific content. When we enable page caching the whole page is being cached. Isn't it possible to cache only fragments of the page. Or specify which fragments not to cache? Or even specify which pages not to cache. This way we can remove some stuff from the caches when logged in. Or don't use the cached versions when logged in.

I found this website already but it doesn't seem to work: http://www.jpstacey.info/blog/2009/03/03/how-to-not-cache-a-particular-drupal-page

But this doesn't seem to work.

kind regards, Daan

like image 481
Daan Poron Avatar asked Jan 21 '23 02:01

Daan Poron


2 Answers

First, I second Ran Bar-Ziks suggestion of putting the 'not to be cached' data in a separate block and set that block to BLOCK_NO_CACHE, as it is the simplest solution.

If that is not possible, you can disable caching of specific pages, but contrary to the suggestion from the link you posted, I'd do this by preventing the page from getting cached in the first place (rather than removing the cache entry afterwards).

To do this, you can manipulate the global cache config setting temporarily on the page in question:

// Disable caching for this request cycle
$GLOBALS['conf']['cache'] = FALSE;

Where you put this code depends on the pages you want to exclude from caching:

  • If it is a custom node type coming from your own module, you'd put it in hook_view.
  • If you want to do this for a node type coming from other modules, you can put it in 'view' operation part on a hook_nodeapi() implementation. ** This would also also work for individual nodes, if you add a check for the node id before disabling the cache.
  • If you need to do this based on paths, you could put it in a hook_init() implementation, checking for the path (or path alias) to decide whether to disable caching or not.

It should be obvious that you need to clear caches first for any of these approaches to work, as they depend on preventing the page from being cached at all. They will not remove an already paged entry from the cache.

like image 194
Henrik Opel Avatar answered Jan 23 '23 15:01

Henrik Opel


You can put the data that you don't want to be cached in block and use BLOCK_NO_CACHE to prevent this block from caching. It is very simple thing to do and explained in drupal.api: http://api.drupal.org/api/drupal/modules--block--block.module/constant/BLOCK_NO_CACHE/6

like image 37
Ran Bar-Zik Avatar answered Jan 23 '23 16:01

Ran Bar-Zik