3

I assume that as soon as you add a contextual filter, that is not a fixed value, the View becomes uncachable since it becomes dynamic, right?

Is there a clear cut way to tell if the View gets cached or not?

5

I assume that as soon as you add a contextual filter, that is not a fixed value, the View becomes uncachable since it becomes dynamic, right?

No.

That just means that the view's output varies based on the URL. Specifically, a query argument.

This is why we in Drupal 8 have the concept of cache contexts: https://www.drupal.org/developing/api/8/cache/contexts.

For the Views module to efficiently determine whether a view can have a cached version or not, we need to know ahead of time which contexts a particular view display varies by. So, upon saving a modified view, the Views module iterates over all plugins that a particular view display uses and gathers its cacheability metadata. It then stores it in the view config entity.

Concrete example: the "Taxonomy Term" view that ships with Drupal 8 core. It has a path of /taxonomy/term/%, with that % being a particular taxonomy term ID. Open core/modules/taxonomy/config/optional/views.view.taxonomy_term.yml and scroll all the way to the bottom. You'll see this:

page_1:
    id: page_1
    display_title: Page
    display_plugin: page
    position: 1
    display_options:
      query:
        type: views_query
        options: {  }
      path: taxonomy/term/%
      display_extenders: {  }
    cache_metadata:
      contexts:
        - 'languages:language_interface'
        - url
        - 'user.node_grants:view'
        - user.permissions
      max-age: -1
      tags: {  }

Note the presence of the url cache context. That describes that this view varies by URL, in particular by the value for the % wildcard in the /taxonomy/term/% URL.

| improve this answer | |
3

If enabled, the view should always be cached unless you are relying on a certain field/filter/sort/... plugin that disables caching. For example a random sort.

Filters, Arguments and many other contexts are considered when defining the cache key.

You can see if a view is cacheable by looking at the calculated cacheable metadata in the exported yml file.

| improve this answer | |
3

You can read cache status inside the HTTP response header.

CASE: cache enabled

$ curl -I   d8.example.com/PathToYourView
HTTP/1.1 200 OK
...
X-Drupal-Dynamic-Cache: MISS

CASE: cache disabled

$ curl -I   d8.example.com/PathToYourView
HTTP/1.1 200 OK
...
X-Drupal-Dynamic-Cache: UNCACHEABLE
| improve this answer | |
  • I created a page view called test with a contextual filter of Node ID with a fixed value of 1. So it always shows node 1 title. According to the HTTP response header it says Uncachable (O_o) d028t.ply.st/test if this is true then most views are uncachable? – No Sssweat Mar 10 '16 at 9:15
  • Oh, turns out that view's do not get cached for Administrators (I get the Uncachable) but when being anonymous or authenticated user I get MISS or HIT. – No Sssweat Mar 10 '16 at 9:22
  • 1
    Makes sense to me. As admin, I'm not interested in cached results. – augusto Mar 10 '16 at 9:24
  • 1
    The X-Drupal-Dynamic-Cache is not at all related to a view being cached or not. It's a header indicating whether Drupal 8's dynamic_page_cache module was able to cache parts of the page or not. See drupal.org/documentation/modules/dynamic_page_cache and wimleers.com/article/drupal-8-dynamic-page-cache. – Wim Leers Mar 17 '16 at 15:30
  • 1
    This answer was not really salvageable, so I wrote a new one: drupal.stackexchange.com/a/195078/7632. – Wim Leers Mar 17 '16 at 15:38

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.