Proper time-based caching support

Created on 28 November 2014, almost 10 years ago
Updated 4 November 2024, 17 days ago

I would like to add time-based caching to my Views XML Backend views. I noticed when enabling it, it always displays the same output for every use-case. It doesn't take any arguments into account. This is not the case with views using the default backend (SQL).

I dug into the code responsible for time-based views caching, and noticed the following bit of code in the get_cache_key() function (views_plugin_cache.inc):

  if (empty($key_data['build_info'])) {
    $build_info = $this->view->build_info;
    foreach (array('query','count_query') as $index) {
      // If the default query back-end is used generate SQL query strings from
      // the query objects.
      if ($build_info[$index] instanceof SelectQueryInterface) {
        $query = clone $build_info[$index];
        $query->preExecute();
        $key_data['build_info'][$index] = array(
          'sql' => (string) $query,
          'arguments' => $query->getArguments(),
        );
      }
    }
  }

  // ...

  $key = md5(serialize($key_data));

Essentially, only queries which are instances of SelectQueryInterface influence how to the key is generated. Since this is not the case, cache keys are the same for all Views XML Backend results.

Attached patch is a solution to this problem. I couldn't find another way than creating a new cache plugin, albeit a very tiny one and derived from the default views_plugin_cache_time plugin. Seems to work like a charm on my installation.

Feature request
Status

Closed: won't fix

Version

1.0

Component

Code

Created by

🇧🇪Belgium rp7

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024