1

Case: I have over 50 views in my site and for performance improvement all were enabled to cache data during their implementation.

Now I want to disable cache for all views at once for some reason (make it more realtime).

Please help.

1

You could switch them all off with features and a bit of hacking. Also, as this involves changing lots of things in mass, make sure you back up your work before doing it and make sure you test it out on a dev server first.

With features, you can create a feature containing just your views. You can then download this feature, which will give you something that looks a lot like a module file.

One of the files in this module will be a file called your_feature.views_default.inc, which will contain a bunch of code that will look ALOT like a view export script (because that's exactly what it is.)

If you look at the lines of code, they each represent a configuration in your views, you should be have a bunch of lines that looks like:

  $handler->display->display_options['cache']['type'] = 'time';
  $handler->display->display_options['cache']['results_lifespan'] = '3600';
  $handler->display->display_options['cache']['results_lifespan_custom'] = '0';
  $handler->display->display_options['cache']['output_lifespan'] = '3600';
  $handler->display->display_options['cache']['output_lifespan_custom'] = '0';

depending how you set your caching. Regardless, if you want to shut off caching on all your views, all of those lines need to become:

$handler->display->display_options['cache']['type'] = 'none';

One your edits are done, save the file and upload the feature module back up to your site in your sites/all/modules folder.

Once it's uploaded, head back to your site, and head to the features admin at admin/structure/features, you should see your feature hanging out there, and it's state should be "Overridden." Click on "Overridden," and you see the overview of your feature, along with a handy-dandy button on the lower right hand of the screen that says "revert components." Click the checkbox next to the views list (to tell features what you want reverted), and then click on the revert components to revert the views to what is stored in code.

Now, there is a down side to using this method. The way features works is it actually takes your view and stores it in code, not the DB. So removing your feature would actually remove you views as well. This will be true until you edit your view, in which case it would go back in the database (just that one view though, not all of them).

1

If you are looking for a permanent solution. I would suggest doing it in an update hook. Create an array of the views or views/displays you want to affect, and then do something like the following

<?php
$views = array(
  'view1' => array(),
  'view1' => array('master', 'display_1'),
);

foreach ($views as $view_name => $displays) {
  $view = views_get_view($view_name);
  foreach($view->display as &$display) {
    if (empty($displays) || isset($view->display[$display]) {
      $display->display_options['cache']['type'] = 'none';
    }
  }
  $view->save();
}

If you are looking for a temporary solution (as in for development/testing purposes), consider setting $conf['views_skip_cache'] = TRUE; in your settings.php or local.settings.php file. This will turn off caching completely for Views. See views_cache_get().

0

I don't think it's possible as Views are saved in the database as a PHP array.

However, there are 2 cache tables cache_views and cache_views_data which contain the various cached items, if you emptied the contents of these tables it would have the same effect as turning off caching apart from you'd need to empty these tables on every request.

0

Think you're looking for this module https://www.drupal.org/project/views_cache_bully

It's listed in the speaker notes of slide 26 from the DIY Drupal 7 performance presentation

Looking at how the module works you'd want to implement hook_views_pre_view(). Here's where the magic happens in the module

/**
 * Implements hook_views_pre_view().
 */
function views_cache_bully_views_pre_build(&$view) {
  // Run if this view does not have a cache.
  if ($view->display_handler->get_plugin('cache')->plugin_name == 'none') {
    // Make views_cache_bully_view_is_exempt() available.
    module_load_include('inc', 'views_cache_bully');
    if (!views_cache_bully_view_is_exempt($view)) {
      // Enable the view cache.
      $view->display_handler->override_option('cache', array(
        'type' => 'time',
        'results_lifespan' => variable_get('views_cache_bully_results_lifespan', 3600),
        'output_lifespan' => variable_get('views_cache_bully_output_lifespan', 3600),
      ));
    }
  }
}

So in your case I would change it to this

/**
 * Implements hook_views_pre_view().
 */
function mymodule_views_pre_build(&$view) {
  // Run if this variable is enabled.
  if (variable_get('mymodule_no_views_cache', TRUE)) {
    // Disable the view cache.
    $view->display_handler->override_option('cache', array(
      'type' => 'none',
    ));
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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