1

I have a Site where Views are used to display flagged Nodes. When i turn on Cache Pages for anonymous Users, the anonymous Users only see the change in the Views until the cache is cleared. (If logged in no problem). What is the best Way to deal with this caching issue. I would prefer that all pages with views where item flags are updated would be recached. Is this possible ?

2 Answers 2

1

Similar to the first answer but not assuming your cache is in the database. (memcache, redis, apc, etc)

function hook_flag_flag($flag, $entity_id, $account, $flagging) {
  $view_name = '<view_name>';
  $display_id = '<display_id>';
  $cid = $view_name . ':' . $display_id . ':*';
  cache_clear_all($cid, $bin = 'cache_views_data', $wildcard = TRUE);
}
0

You can achieve this functionality using the Flag module hook_flag_flag, This hook will act on an object being flagged.

In this hook you need to clear the cache data for that particular view from cache_views_data table.

Below is the sample code,

function hook_flag_flag($flag, $entity_id, $account, $flagging) {
  $view_name = '<view_name>';
  $display_id = '<display_id>';
  $cid = $view_name . ':' . $display_id;
  if (db_table_exists('cache_views_data')) {
    db_delete('cache_views_data')
      ->condition('cid', db_like($cid) . '%', 'LIKE')
      ->execute();
  }
}

Usually views will set the cache ID based on the view name, display id and other data being passed.

Below is code snippet which prepares the cache ID for views result

$this->_results_key = $this->view->name . ':' . $this->display->id . ':results:' . md5(serialize($key_data));

So specify the cache ID accordingly in the delete query. However make sure to not to delete other entries in the cache_views_data table.

Hope this helps.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.