Problem/Motivation

RenderCache cannot be used as is atm, because its not aware of the default renderer cache contexts.

Proposed resolution

Set the default cache contexts before set() and get(). Remove from Renderer::doRender() (but leave for the is_root_call case).

  • Inject the %renderer.config% container parameter
  • Use $this->rendererConfig['required_cache_contexts'] in set() to expand the list of used cache contexts
  • Use $this->rendererConfig['required_cache_contexts'] in get() to expand the list of used cache contexts
  • Remove the required cache context for the #cache keys if-case in Renderer::doRender()

Remaining tasks

- Fix it

User interface changes

API changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Fabianx’s picture

Issue summary: View changes
Fabianx’s picture

Issue summary: View changes
dawehner’s picture

This sounds like a novice task? It reads as if all you need is to ensure those cache contexts.

Fabianx’s picture

Issue tags: +Novice

Yes, it can be done by a novice.

dawehner’s picture

Issue summary: View changes
Fabianx’s picture

Issue summary: View changes
Wim Leers’s picture

Issue tags: +php-novice
joshi.rohit100’s picture

Status: Active » Needs review
FileSize
2.75 KB
joshi.rohit100’s picture

FileSize
2.74 KB
697 bytes

Copy Paste mistake ):

The last submitted patch, 8: 2483887-RenderCache-8.patch, failed testing.

Fabianx’s picture

Issue tags: +Needs tests

Nice work, could we have a test for that, please?

dawehner’s picture

When I used the render cache directly I had to deal also on the get() level so yeah tests would be nice.

Status: Needs review » Needs work

The last submitted patch, 9: 2483887-RenderCache-9.patch, failed testing.

Fabianx’s picture

Issue summary: View changes

#12: Yes, you are right, I missed we add it to $pre_bubbling_elements, updated the IS for that.

Wim Leers’s picture

Status: Needs work » Needs review
FileSize
3.81 KB
935 bytes

Reroll + one line change to make unit tests pass.

Wim Leers’s picture

Title: RenderCache is not aware of default context properties » RenderCache is not aware of required cache contexts
Component: cache system » render system

Clarify issue.

Wim Leers’s picture

Title: RenderCache is not aware of required cache contexts » Make RenderCache a private service
Category: Bug report » Task
Issue tags: -Novice, -php-novice, -Needs tests +DX (Developer Experience)
FileSize
611 bytes
+++ b/core/lib/Drupal/Core/Render/RenderCache.php
@@ -207,9 +217,11 @@ public function set(array &$elements, array $pre_bubbling_elements) {
-      $cache_contexts = $data['#cache']['contexts'];
+      $cache_contexts = Cache::mergeContexts($data['#cache']['contexts'], $required_cache_contexts);

This is wrong; it should merge the required cache contexts with those in $data. Otherwise the render cache item, when retrieved from cache and used in something else, will not bubble up the required cache context.

Of course… one could reason that required cache contexts should not ever be exposed, and should indeed be added this way: at the very last moment, without them showing up while debugging anywhere else in the render pipeline.

But, the reasoning that we've applied in the past is: "less magic, easier debugging". That's why Renderer::doRender() does do what I described above.


Second: this will introduce some overhead/performance penalty, because it will mean required cache contexts are added both in the Render and in the RenderCache services.
That is also the reason why RenderCache does not yet deal with required cache contexts in HEAD, which is exactly what this issue is trying to change.

The only reason this issue wants to change that is for the very few, very rare occasions where code is using the RenderCache service directly. In fact, #2450897: Cache Field views row output was the only occurrence of this. (BTW while trying to find more usages, I also found #2507459: CachePluginBase and subclasses no longer need Renderer/RenderCache services injected.)

But, actually… that also doesn't use the RenderCache service directly! See #2450897-118: Cache Field views row output and related patches: […] we found another approach here where we can get back to the nicer #pre_render pattern. Which means nothing actually directly talks to the RenderCache service.

Which brings me to my proposal (which is the conclusion I've come to while writing this comment): let's make the RenderCache service private. That just enforces what we already know: just use the Renderer service. That's what turned out to make most sense. This issue is a "bug" or a "problem" only if you want to use this service directly. But that doesn't make a lot of sense. It's tightly coupled to the Renderer. It helps make Renderer more understandable. Let's keep it that way. And thus let's make this service private instead.

(Fun detail: this would be nicely symmetrical with FormBuilder and FormCache: FormCache is private to FormBuilder too!)

Wim Leers’s picture

dawehner’s picture

let's make the RenderCache service private.

Its unusable anyway, but well, there are a lot of tests in views relying on it, if I remember correctly.

Fabianx’s picture

Category: Task » Bug report

I am not yet convinced that we should not push those things to RenderCache though I agree the coupling is tight right now.

There is still the buggy behavior however that is justifying the move.

Wim Leers’s picture

There is still the buggy behavior however that is justifying the move.

There's no actual bug as far RenderCache is concerned. It just does what is told, works with what it is given.

Fact is that we will always need this in Renderer. Which means we'll be doing the same work twice. Why not KISS? We can still choose to make RenderCache a public service later.

The last submitted patch, 15: 2483887-RenderCache-15.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 17: 2483887-RenderCache-17.patch, failed testing.

Status: Needs work » Needs review
Wim Leers’s picture

Finally, green patch :)

Wim Leers’s picture

FileSize
629 bytes

Just a rebase, conflicts automatically resolved by git. Still applies

Status: Needs review » Needs work

The last submitted patch, 26: 2483887-RenderCache-26.patch, failed testing.

Fabianx’s picture

I am nowadays understanding private services much better than 3 months ago.

It makes absolute sense to make this a private service, but the IS needs some work.

Marking as potential 'rc target' as it would be problematic to not being able to make changes to that service anymore.

Wim Leers’s picture

I think it's too late to do this now?

dawehner’s picture

Well seems so. We should at least mark it as internal?

Wim Leers’s picture

Title: Make RenderCache a private service » Mark RenderCache as internal
Status: Needs work » Needs review
FileSize
1.07 KB

+1.

Status: Needs review » Needs work

The last submitted patch, 31: 2483887-31.patch, failed testing.

Wim Leers’s picture

A rare case of PIFR being green and DrupalCI being red. Which is right?

UpdatestUpdateImportSourceRemote
fail: [Other] Line 144 of core/modules/locale/src/Tests/LocaleUpdateTest.php:
Updates for Contrib module one

Seems like DrupalCI is drunk for once. Re-testing.

Fabianx’s picture

Category: Bug report » Task
Priority: Normal » Major
Status: Needs work » Reviewed & tested by the community

RTBC - we need a beta evaluation now ...

  • xjm committed 17debf4 on 8.0.x
    Issue #2483887 by Wim Leers, joshi.rohit100, Fabianx, dawehner: Mark...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

I think marking something @internal during RC is okay, but check with a committer when adding the tag (and mention who you talked to and the reasoning). Cheers!

Committed and pushed to 8.0.x.

Status: Fixed » Closed (fixed)

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