Problem/Motivation

In #2939505: Forum index does not update if new sub-forums are added we're merging tags from 3 sources which results in really ugly code...

Cache::mergeTags(Cache::mergeTags($this->nodeEntityTypeDefinition->getListCacheTags(), $this->commentEntityTypeDefinition->getListCacheTags()), $this->taxonomyTermEntityTypeDefinition->getListCacheTags()),

Proposed resolution

Given the importance of merge cache metadata easily let's take advantage of variadics to make this easier...

Cache::mergeTags($this->nodeEntityTypeDefinition->getListCacheTags(), $this->commentEntityTypeDefinition->getListCacheTags(), $this->taxonomyTermEntityTypeDefinition->getListCacheTags()),

Remaining tasks

User interface changes

API changes

API widened to accept more arguments (of the same type) for:
\Drupal\Core\Cache\Cache::mergeTags()
\Drupal\Core\Cache\Cache::mergeMaxAges()
\Drupal\Core\Cache\Cache::mergeContexts()

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
FileSize
5.24 KB
alexpott’s picture

A little bit more elegant.

alexpott’s picture

And a little bit more.

joachim’s picture

Status: Needs review » Needs work

Looks good, just a docs clarity problem:

+++ b/core/lib/Drupal/Core/Cache/Cache.php
@@ -20,16 +20,14 @@ class Cache {
    *   Cache contexts array to merge.

@@ -46,18 +44,15 @@ public static function mergeContexts(array $a = [], array $b = []) {
    *   Cache tags array to merge.

Should these say 'Cache contexts arrayS to merge.'? The other param that has become variadic is being changed from saying 'value' to 'values'.

Neslee Canil Pinto’s picture

Status: Needs work » Needs review
FileSize
5.35 KB
658 bytes
alexpott’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Cache/Cache.php
@@ -67,25 +62,21 @@ public static function mergeTags(array $a = [], array $b = []) {
+   * @param int[] ...

This is wrong it should be int ... - each argument is not an array - it's an int.

Neslee Canil Pinto’s picture

Status: Needs work » Needs review
FileSize
5.35 KB
376 bytes
longwave’s picture

+++ b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
@@ -73,18 +80,26 @@ public function testMergeTags(array $a, array $b, array $expected) {
+      [Cache::PERMANENT, Cache::PERMANENT, FALSE, Cache::PERMANENT],

@@ -93,8 +108,13 @@ public function mergeMaxAgesProvider() {
+    if ($c === FALSE) {

Maybe nitpicking but NULL feels like a better thing to use to represent a missing value, I had to read this twice before I figured out what was going on here but I think NULL would make it clearer from the data provider alone.

edit: or even, just pass $a, $b and optionally $c as a single array argument, and use the splat operator in testMergeMaxAges() too!

jungle’s picture

FileSize
4.83 KB
1.91 KB

edit: or even, just pass $a, $b and optionally $c as a single array argument, and use the splat operator in testMergeMaxAges() too!

Adopted this approach.

longwave’s picture

We could also change testMergeTags() the same way rather than adding a separate test method?

Also slightly surprised that CacheTest covers tags and max ages but not contexts - I can't find a unit test for mergeContexts, is adding basic coverage for that out of scope?

jungle’s picture

  • Did the same for testMergeTags()
  • covered mergeContexts()
jungle’s picture

  1. And BTW, \Drupal\Tests\Core\Cache\CacheTest::validateTagsProvider() seems not being used.
  2. +++ b/core/lib/Drupal/Core/Cache/Cache.php
    @@ -20,16 +20,14 @@ class Cache {
    +   * @param string[] ...
    +   *   Cache contexts arrays to merge.
    
    @@ -46,18 +44,15 @@ public static function mergeContexts(array $a = [], array $b = []) {
    +   * @param string[] ...
    

    Should the variable be added right after ...?

longwave’s picture

+++ b/core/tests/Drupal/Tests/Core/Cache/CacheTest.php
@@ -62,8 +65,15 @@ public function mergeTagsProvider() {
+    $this->assertEquals(['bar', 'foo', 'llama'], Cache::mergeTags(['bar', 'foo'], ['foo', 'bar'], ['llama', 'foo']));

Now we can just roll this into mergeTagsProvider()

jungle’s picture

FileSize
11.25 KB
1.01 KB

Addressed #14, file a new issue for #13.1 if necessary.

jungle’s picture

FileSize
7.19 KB
1.01 KB

A wrong patch uploaded in #15

longwave’s picture

+++ b/core/lib/Drupal/Core/Cache/Cache.php
@@ -20,16 +20,14 @@ class Cache {
+   * @param string[] ...$cache_contexts

@@ -46,18 +44,15 @@ public static function mergeContexts(array $a = [], array $b = []) {
+   * @param string[] ...

Do we use the variable name or not in variadic @params? The coding standards docs do not say.

alexpott’s picture

Re #17 we can't use the variable name...our PHPCS implementation will moan lots and lots...

alexpott’s picture

#16 fails PHPCS with

Checking changed files...

FILE: ...ex/dev/sites/drupal8alt.dev/core/lib/Drupal/Core/Cache/Cache.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
 23 | ERROR | [ ] Parameter type "string[] ..." must not contain
    |       |     spaces
 23 | ERROR | [x] Expected 1 spaces after parameter type; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 134ms; Memory: 8MB

PHPCS: core/tests/Drupal/Tests/Core/Cache/CacheTest.php passed
longwave’s picture

Status: Needs review » Reviewed & tested by the community

All looks good - new functionality plus improved test coverage.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: 3125032-2-19.patch, failed testing. View results

longwave’s picture

Status: Needs work » Reviewed & tested by the community

Random fail in FieldLayoutTest

andypost’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

For API changes

jungle’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record

Change Record added, setting back to RTBC

  • catch committed 4884992 on 9.1.x
    Issue #3125032 by jungle, alexpott, Neslee Canil Pinto, longwave,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.1.x, thanks!

I'm a bit borderline on backporting this to 8.9.x, but leaving in 9.1.x for now since it's an obvious API addition even if there are no bc issues as such.

jungle’s picture

@catch, thank you for committing!

Should the CR associated be published? And as a reminder, introduced in branch and Introduced in version should be adjusted before publishing.

Wim Leers’s picture

OMG so nice! 😍

Status: Fixed » Closed (fixed)

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