Problem/Motivation

We had a need for views caches of several hours but needed these caches to be cleared immediately after content was changed or published without clearing all site / views caches. We also had a need to alter caching behavior with further cache granularity in custom modules.

Proposed resolution

The patch accomplishes the following:

1. Separate cache key for results from users, role, language, custom key_data, etc.

This allows more targeted clearing of views caches. For example, we've added custom rules to clear views caches when specific node types are displayed in order to avoid clearing the entire site cache. For an example of how this is done see: https://gist.github.com/malcolmp/6f9139e07f2f67e4fc16008c9687aa24

2. Allow modules to alter views cache $key_data to vary cache by custom dimensions.

By adding a hook views_plugin_cache_key_extra we've allowed custom modules to extend views caching. We've used this to achieve different output in cached views output using triggers other than roles allowing some degree of user personalization.

I wouldn't say these changes are currently ready for Core, but I'm interested in community feedback on this change as we've utilized it to improve views cache performance.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

malcolm_p created an issue. See original summary.

Status: Needs review » Needs work

The last submitted patch, views--views-plugin-cache-key.patch, failed testing.

Marko B’s picture

Yes, this would be nice alter to have. Wondering why didn't you just put alter to change "$key_data" in get_cache_key but add new function where you add the same thing and then append it to key?

malcolm_p’s picture

That would probably make sense and clean up the code. I was previously also separately getting the "extra" portion of the key but don't really need to be doing that anymore.

malcolm_p’s picture

Patch updated for 7.x-3.20.

malcolm_p’s picture

Fixed patch for 7.x-3.20 attached.

DamienMcKenna’s picture

Assigned: malcolm_p » Unassigned

FYI the "assigned" field is for indicating that you're actively working on an issue, if you're not actively working on an issue please leave it set to "unassigned". Thank you.

malcolm_p’s picture

I've basically just been re-rolling as needed and probably won't take this much further as I'm focused on D8 work.

Here is the patch for this issue re-rolled against 7.x-3.23.

DamienMcKenna’s picture

Status: Needs work » Needs review

Thank you. Let's see what testbot thinks.

malcolm_p’s picture

Here is an updated patch, unfortunately it doesn't work fully as the cache key doesn't separate certain query arguments such as page, offset, etc. The best solution to achieve this functionality on a site is quite simply to use Drupal 8 for the cache tag system.