Drupal Nodequeues and Caching

I've mostly been a fan of Draggable Views in the past. More recently, I came across Nodequeue, which is older than Draggable Views but seems easier to tweak. Nodequeues come handy if your publication needs to order a few select nodes for display in a view or a block rather than sorting by date. A few clicks allow you to order nodes, add them to "smartqueues" filtered by taxonomy term, and then use Views to sort them according to their nodequeue position.

However, re-ordering, adding, or removing nodes in the nodequeues won't clear the Drupal cache, and while an authenticated user will see the change in a page or a view instantly, an anonymous user will not until the cache is cleared again. The caching system, and how its clearing process is triggered, can be quite obscure for most non-admin users of a drupal site. They are also used to seeing the pages updated automatically any time they make an update to a node, and seeing the pages that have a nodequeue remain unchanged can be puzzling.

Fortunately, nodequeue offers a couple of hooks that can be used in your custom module to correct this problem. Clearing the cache is an expensive operation, but if your nodequeues only get changed every now and then, it seems justified to clear it in the same time. In a custom module, adding the following should do the trick:

/**
* Implementation of hook_nodequeue_add()
*/
function your_module_nodequeue_add($sqid, $nid) {
    your_module_full_cache_clear();
}

/**
* Implementation of hook_nodequeue_remove()
*/
function your_module_nodequeue_remove($sqid, $nid) {
    your_module_full_cache_clear();
}

/**
* Implementation of hook_nodequeue_swap()
*/
function your_module_nodequeue_swap($sqid, $nid) {
    your_module_full_cache_clear();
}

function your_module_full_cache_clear() {
    // Some logic here if needed.
    cache_clear_all();
    drupal_set_message('nodequeue: all cache cleared');
}

After adding these lines to your module, you'll see that whenever you add or remove a node from a nodequeue, the cache gets cleared and you get the message in your_module_full_cache_clear on screen. Yay! But whenever you reorder the queues by dragging and dropping nodes and save this new order, the cache remains untouched. This unexpected behavior is the most frustrating part. The hook_nodequeue_swap is supposed to take care of clearing the cache when the order changes. As it turns out --and this is at least true for nodequeue version 6.x-2.9-- the function responsible for saving a new subqueue order, named nodequeue_save_subqueue_order, doesn't call for the hook_nodequeue_swap. To remedy to this, you can simply call for the hook at the end of this function (located line 1226):

module_invoke_all('nodequeue_swap', $sqid, $nid);

After looking into it for a while, there's a bug request published in the module's issue queue already and even a patch: http://drupal.org/node/992326 So the next release won't have this problem anymore.