2

I have a page generated by a custom module that contains two sections:

  • Product info
  • Recently viewed products

The product info only changes when the product is updated.

Recently viewed products are unique to every visitor (data is stored in $_SESSION).

Each section has its own render array.

'productinfo' => [
    // ...
    '#cache' => [
        'tags' => ['product:1234']
    ]
],
'recentlyviewed' => [
    // ...
    '#lazy_builder' => array(
      // ....
    ),
    '#create_placeholder' => TRUE,
    '#cache' => [
        'max-age' => 0
    ]
]

The behaviour I expect is that the dynamic page cache can cache the productinfo section, but the recentlyviewed would never be cached. However, if a session has not yet been started (i.e. first page view), the entire html page is cached to the cache_render DB table, and cache_dynamic_page_cache is always empty.

The X-Drupal-Dynamic-Cache header is always set to UNCACHABLE.

How can I get the dynamic page cache to cache the product info section?

2

Your question has two different topics:

  1. How can I get the dynamic page cache to cache the product info section?

Set cache keys:

'productinfo' => [
    // ...
    '#cache' => [
        'keys' => ['mymodule', 'product', '1234'],
        'tags' => ['product:1234'],
    ],
],

Sections of a render array are only cached, if they have cache keys. The keys are used to build the unique cid for the cache render table.

  1. However, if a session has not yet been started (i.e. first page view), the entire html page is cached

Anonymous traffic is cached by the Internal Page Cache. If you start a session, the next request will be able to pass the Internal Page Cache and is handled by the Dynamic Page Cache.

  • OK, thanks! I saw cache keys were mentioned, but could not find any further documentation. Is there any specific naming convention, or do they just have to be unique? – Tim Jan 3 '17 at 13:53
  • There are no naming convention. I would use three keys, one for the project, so that there is no conflict with other projects, one for the type and one for the id. – 4k4 Jan 3 '17 at 14:02
  • The product info now gets its own render cache entry, using the keys for the cid. The dynamic page cache isn't being used, but I guess it's not needed. – Tim Jan 3 '17 at 14:51

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.