Seems with the latest dev-version (2011-Jul-22), the purge command is not working anymore.

I edit several nodes but the (old) nodes are still being served from cache.
When the cache expiration time is reached (10min in my setup), the edited nodes are finally visible.

CommentFileSizeAuthor
#6 varnish-1232448-6.patch1.44 KBDane Powell
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

flatcircle’s picture

Status: Active » Closed (works as designed)

And suddenly (after a reboot of the Varnish server) the purging is working again.

flatcircle’s picture

Status: Closed (works as designed) » Active

Still experiencing problems.
After a while the purge command stops working.

I've setup a staging server a while ago and with an older version of the Varnish module, I have no problems.

7.x-1.x-dev - datestamp = "1307839269" => edit node => purge command to Varnish => working

7.x-1.x-dev - datestamp = "1311295316" => edit node => purge command to Varnish => broken

alexis’s picture

I can confirm the problem exists in "7.x-1.x-dev" datestamp = "1311295316".

I compared with varnish.module for Drupal 6.x and there we had hook_comment and hook_nodeapi like:

function varnish_nodeapi(&$node, $op) {
    // We've probably just run through node_save, and normally this is where
    // Drupal calls a cache_clear_all().
  if (in_array($op, array('insert', 'update', 'delete', 'delete revision'))
    && variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) == VARNISH_DEFAULT_CLEAR) {
        varnish_purge_all_pages();
  }
}

/**
 * Implementation of hook_comment()
 *
 * Used to pick up cache_clearing events
 */
function varnish_comment($comment, $op) {
  if (in_array($op, array('insert', 'update', 'publish', 'unpublish', 'delete'))
    && variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) == VARNISH_DEFAULT_CLEAR) {
        varnish_purge_all_pages();
  }
}

Shouldn't the corresponding Drupal 7 hooks be added to fix this?

alexis’s picture

I've fixed part of the problem. Sorry if I don't submit a patch in the proper way, I haven't had the time to read the docs for git yet.

You just need to add these functions to varnish.module:

/**
 * Implements hook_node_insert().
 */
function varnish_node_insert($node) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_node_update().
 */
function varnish_node_update($node) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_node_delete().
 */
function varnish_node_delete($node) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_node_revision_delete().
 */
function varnish_node_revision_delete($node) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_comment_insert().
 */
function varnish_comment_insert($comment) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_comment_update().
 */
function varnish_comment_update($comment) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_comment_delete().
 */
function varnish_comment_delete($comment) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_comment_publish().
 */
function varnish_comment_publish($comment) {
  varnish_purge_all_pages();
}

/**
 * Implements hook_comment_unpublish().
 */
function varnish_comment_unpublish($comment) {
  varnish_purge_all_pages();
}

This is the same approach used by the Varnish module for Drupal 6 and considers cases when a node or its comments change. I haven't considered the case for purging user profiles as I don't need that part for now but should be easy to add if you take a look at the 6.x module.

I'd prefer calling a function like this instead of purging all pages just because one node has changed:

function varnish_purge_node($nid) {
  if (variable_get('varnish_cache_clear', VARNISH_DEFAULT_CLEAR) == VARNISH_DEFAULT_CLEAR) {
    $paths = array(drupal_get_path_alias('node/' . $nid));
    varnish_expire_cache($paths);
  }
}

but that adds some complexity to the logic as this code only purges the url where the full node is displayed but parts of a node, probably the teaser or title, can appear on other pages and those url's should be purged from Varnish as well.

I'm wondering if the module developers are thinking of a different approach and if that was the reason to remove this logic from the latest development code.

flatcircle’s picture

Absolutely right!

Indeed, the best way to send a purge command to Varnish is:

1) Only purge the node which has been modified
2) Purge additional nodes where content is also shown (teasers on frontpage, etc...)

Sending a purge command for all nodes when only 1 node has been modified is NOT recommended.

For example: you have a busy website with thousands of nodes and the website is being edited during the day by some editors/publishers.

This means the Varnish cache will be wiped constantly (because of the edits on nodes), resulting that almost no nodes are being served from Varnish because the cache has to be rebuild each time!

Dane Powell’s picture

FileSize
1.44 KB

Here's a patch based on #4- it's not an ideal solution, but it's better than nothing.

flatcircle’s picture

Will this patch be included in future builds of the Varnish-module?

micheas’s picture

Should the status be changed to needs review?

Or is the patch in post #6 ugly enough that a rewrite is needed?

fabsor’s picture

Status: Active » Postponed (maintainer needs more info)

This should not be needed in the Drupal 7 version. The new version works as a Drupal Cache backend, which means it will fetch all calls to cache_clear_all() automaticly. This means that the cache will be cleared automaticly by Drupal core.

If you need more granular cache invalidation, you can turn this feature off and build it yourself, or try the Expire module, which is supported by Varnish.

I ran the tests, and they indicate that everything should be working as they should. I also tried the module out by testing it manually and when it's properly configured, it works as expected. For information on how to set this up, see the project page.

I'm setting this to postponed, in case there was some use case I missed.

acouch’s picture

I had this issue when varnish cron was enabled. This #1461792: Cache doesn't clear if "varnish_flush_cron" set to 1 fixed it.

Dane Powell’s picture

It seems that in my case, this is my fault for not RTFM... I just needed to add Varnish as a caching backend as described in the README file:

<?php
// Add Varnish as the page cache handler.
$conf['cache_backends'] = array('sites/all/modules/varnish/varnish.cache.inc');
$conf['cache_class_cache_page'] = 'VarnishCache';
?>
MiSc’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Closed (outdated)

Closing issues that had no activity the latest year, please re-open if the issues is still relevant.