Is there a way to set cache settings for all panes in one hit?

Editors on my site have created a lot of panel nodes without setting cache settings for any of them.

I'd like to update all the panes to use time-based simple caching - is there a better way of doing it than a direct database query?

Comments

sdboyer’s picture

nope, there's no facility for a batch action like that. sorry. a direct db query might be a little tough (given that you have to write to a serialized array), but you could probably do it pretty easily with a short script executed via drush scr.

sdboyer’s picture

Status: Active » Fixed
malcomio’s picture

Thanks - that's what I ended up doing - here's the function I called via Drush, in case it's useful to anyone else.

/**
 * Set a panel display to use simple caching.
 *
 * @param int $pid
 *   the panel ID
 * @param int $did
 *   the display ID
 * @param int $lifetime
 *   the cache lifetime in seconds
 */
function mymodule_panel_cache_change($pid, $did, $lifetime) {
  if (is_numeric($pid) && is_numeric($did)) {
    $display = panels_load_display($did);
    $cache_settings = serialize(array(
      'method' => 'simple',
      'settings' => array('lifetime' => $lifetime, 'granularity' => 'none'),
        ));
    if (db_query("UPDATE {panels_pane} SET cache = '%s' WHERE pid = %d AND did = %d", $cache_settings, $pid, $did)) {
      return TRUE;
    }
  }
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Countzero’s picture

Version: 6.x-3.9 » 7.x-3.3
Status: Closed (fixed) » Active

Does anybody know if there is a way to do this under D7 / Panels 3.3 ?

In the meantime, I'll try to adapt the snippet above to it and post it if it's relevant.

Countzero’s picture

If anyone's interested, here's what I came up with. I can't believe this functionality isn't implemented somewhere already, but well.

It's VERY rough. It will enable caching for one week for all panes of all displays on your website, so please use with caution.

function _panel_cache_change() {
      $i = 0;
      $result = db_query("SELECT * FROM {panels_pane}");
      foreach ($result as $obj) {
        $cache_settings =array(
          'method' => 'simple',
          'settings' => array('lifetime' => 604800, 'granularity' => 'none'), // You may change hese parameters to suit your needs
        );   
          $upd = db_update('panels_pane')
            ->fields(array(
                            'cache' => serialize($cache_settings)
                            )
                        )
            ->condition('pid', $obj->pid, '=')
            ->condition('did', $obj->did, '=')
            ->execute();
          if ($upd) {
            dsm('Cache activated for panel ' . $obj->pid . ' / from display ' . $obj->did );
          } else {
           dsm('Cache already activated with these parameters for panel ' . $obj->pid . ' / from display ' . $obj->did ); 
          }
        $i++;
      }      
      return "$i panes treated.";
}
Countzero’s picture

Status: Active » Needs review

If anyone has time to review the snippet, feel free. Or maintainers can close, it's up to them.

kmccarthy’s picture

Issue summary: View changes

I, like Malcomio, also have a site with a lot of panes that have never had cache set for them. As this was the only information I could find that addresses a batch modification of them, I looked into following Countzero's suggestion of setting cache via a query directly to the database. However I could only locate panel panes in the data that were not on system pages - i.e. I couldn't find any data for panes set in node_view or term_view pages. After a rather long effort to track that data down I realized I didn't know enough of how CTools stores it and that it would take a considerable amount of time to learn it, time I don't have at the moment, so I took another approach.
Since I have all of the pane configurations set in a Feature module I decided to modify the pane cache settings in that code. It seems to work fine now that I've reverted the Feature to the new code.
Hope this helps someone else looking to do the same.

kmccarthy’s picture

Update on my adventures with panel pane caching: Although it is undoubtedly a pain (pun intended?) to use the UI for setting cache for panes throughout various pages that employ panels, I would highly advise being very careful in batching configuration using any means. Setting values may have unintended consequences unless you know exactly what you want and what the results will be. Using the panels_why_so_slow module has proven to be very helpful for me in applying cache only for the panes where it really helps performance. And I've learned the hard way what selecting 'granularity' does.

japerry’s picture

Status: Needs review » Closed (outdated)

Drupal 7 is no longer supported, closing.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.