I have a dashboard with about seven views that are each very expensive. Most of the views can be computed hourly, but I would prefer cron to run them rather than the unfortunate user. How can I have cron refresh the views cache and never a user?

2 Answers 2


Something like this should work:

  1. Set the Views to cache data.
  2. Implement hook_cron(), and delete the cached data from the cache_views and cache_views_data tables.
  3. Programmatically request the URL which contains the expensive Views to rebuild them (this could be done via CURL, file_get_contents(), etc.)
  • Recommend using httprl's non blocking mode when doing the HTTP requests.
    – mikeytown2
    Sep 18, 2013 at 22:55
  • @mikeytown2 - that's an interesting library, thanks for posting. Personally, I'd go with something lighter (in this case), such as this example, but I can definitely see the appeal of proper parallel requests. Sep 18, 2013 at 23:01

I implement things like that as a drush script, which I think invoke from a crontab. Something like would be a start:

$cids = array(...); // the $cid list that you want to nuke

foreach ($cids as $cid) {
  cache_clear_all($cid, 'cache_views'); // may want to also do {cache_views_data}

$urls = array(...); // the URLs w/ the views that you just cleared

foreach ($urls as $url) {
  $url = url($url, array('absolute' => TRUE)); // if needed
  cache_clear_all($url, 'cache_page');
  $request = drupal_http_request($url);

I then call it as

0 * * * * /path/to/drush -u 1 scr /path/to/script.php

from my crontab.

Doing it from crontab instead of Drupal cron can help eliminate PHP timeouts.

  • Both answers seem good but this might be a little more flexible. The views I want to blast are private to their users. How will the required authentication be handled? Sep 19, 2013 at 13:15
  • 1
    @JoeBeuckman Not sure about that one. Instead of fetching the URL, you may need to use the Views API to instantiate the view, pass in the $account, and then generate the rendered HTML. You may also have to loop through your users.
    – mpdonadio
    Sep 19, 2013 at 15:24

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.