Skip to content

Commit d4935d4

Browse files
committed
reduce tag header by removing ManyToOne relations from tags list
1 parent a01be95 commit d4935d4

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

api/src/HttpCache/TagCollector.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use ApiPlatform\Metadata\Operation;
99
use ApiPlatform\Serializer\TagCollectorInterface;
1010
use App\Entity\HasId;
11+
use Doctrine\Common\Util\ClassUtils;
12+
use Doctrine\ORM\EntityManagerInterface;
13+
use Doctrine\ORM\Mapping\AssociationMapping;
1114

1215
/**
1316
* Collects cache tags during normalization.
@@ -17,7 +20,10 @@
1720
class TagCollector implements TagCollectorInterface {
1821
public const IRI_RELATION_DELIMITER = '#';
1922

20-
public function __construct(private ResponseTagger $responseTagger) {}
23+
public function __construct(
24+
private ResponseTagger $responseTagger,
25+
private EntityManagerInterface $em
26+
) {}
2127

2228
/**
2329
* Collect cache tags for cache invalidation.
@@ -37,7 +43,7 @@ public function collect(array $context = []): void {
3743
}
3844

3945
if (isset($context['property_metadata'])) {
40-
$this->addCacheTagsForRelation($context, $iri, $context['property_metadata']);
46+
$this->addCacheTagsForRelation($object, $context, $iri, $context['property_metadata']);
4147

4248
return;
4349
}
@@ -59,7 +65,7 @@ private function addCacheTagForResource(string $iri): void {
5965
$this->responseTagger->addTags([$iri]);
6066
}
6167

62-
private function addCacheTagsForRelation(array $context, string $iri, ApiProperty $propertyMetadata): void {
68+
private function addCacheTagsForRelation(mixed $object, array $context, string $iri, ApiProperty $propertyMetadata): void {
6369
if (isset($propertyMetadata->getExtraProperties()['cacheDependencies'])) {
6470
foreach ($propertyMetadata->getExtraProperties()['cacheDependencies'] as $dependency) {
6571
$cacheTag = $iri.PurgeHttpCacheListener::IRI_RELATION_DELIMITER.$dependency;
@@ -69,6 +75,13 @@ private function addCacheTagsForRelation(array $context, string $iri, ApiPropert
6975
return;
7076
}
7177

78+
$associationMappings = $this->em->getClassMetadata(ClassUtils::getClass($object))->getAssociationMappings();
79+
$associationMapping = $associationMappings[$context['api_attribute']] ?? null;
80+
// we have currently no use-case where we rely on a ManyToOne for tag invalidation
81+
if ($associationMapping instanceof AssociationMapping && $associationMapping->isManyToOne()) {
82+
return;
83+
}
84+
7285
$cacheTag = $iri.PurgeHttpCacheListener::IRI_RELATION_DELIMITER.$context['api_attribute'];
7386
$this->responseTagger->addTags([$cacheTag]);
7487
}

e2e/specs/httpCache/activities.cy.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,36 @@ const collectionXKeys =
1616
/**
1717
* activitiy "Snowboardfahren"
1818
*/
19-
'a13fadc97610 a13fadc97610#scheduleEntries a13fadc97610#camp a13fadc97610#category a13fadc97610#progressLabel a13fadc97610#activityResponsibles a13fadc97610#rootContentNode ' +
19+
'a13fadc97610 a13fadc97610#scheduleEntries a13fadc97610#activityResponsibles a13fadc97610#rootContentNode ' +
2020
/* embedded progress labels: */
21-
'af92782262d7 af92782262d7#camp a13fadc97610#embeddedProgressLabel ' +
21+
'af92782262d7 ' +
22+
'a13fadc97610#embeddedProgressLabel ' +
2223
/* embedded schedule entries: */
2324
/* (the first schedule entry also includes the period id) */
24-
'29c9e9a07d82 7fa4564a5d5d 29c9e9a07d82#period 29c9e9a07d82#activity 29c9e9a07d82#day ' +
25-
'f08d69cae18a f08d69cae18a#period f08d69cae18a#activity f08d69cae18a#day ' +
26-
'7e8086d94633 7e8086d94633#period 7e8086d94633#activity 7e8086d94633#day ' +
25+
'29c9e9a07d82 7fa4564a5d5d 29c9e9a07d82#day ' +
26+
'f08d69cae18a f08d69cae18a#day ' +
27+
'7e8086d94633 7e8086d94633#day ' +
2728
'a13fadc97610#embeddedScheduleEntries ' +
2829
/* embedded activitiy responsibles: */
29-
'06743ccfeedd 06743ccfeedd#activity 06743ccfeedd#campCollaboration ' +
30-
'21bc6661c569 21bc6661c569#activity 21bc6661c569#campCollaboration ' +
31-
'a13fadc97610#embeddedActivityResponsibles a13fadc97610#contentNodes ' +
30+
'06743ccfeedd ' +
31+
'21bc6661c569 ' +
32+
'a13fadc97610#embeddedActivityResponsibles ' +
33+
'a13fadc97610#contentNodes ' +
3234
/**
3335
* activity "Skifahren"
3436
*/
35-
'b29d387cc403 b29d387cc403#scheduleEntries b29d387cc403#camp b29d387cc403#category b29d387cc403#progressLabel b29d387cc403#activityResponsibles ' +
37+
'b29d387cc403 b29d387cc403#scheduleEntries b29d387cc403#activityResponsibles b29d387cc403#rootContentNode ' +
3638
/* embedded progress labels: */
37-
'b29d387cc403#rootContentNode b29d387cc403#embeddedProgressLabel ' +
39+
'b29d387cc403#embeddedProgressLabel ' +
3840
/* embedded schedule entries: */
39-
'e68f4e47517a e68f4e47517a#period e68f4e47517a#activity e68f4e47517a#day ' +
40-
'f0883e931649 f0883e931649#period f0883e931649#activity f0883e931649#day ' +
41-
'ee85308a97d1 ee85308a97d1#period ee85308a97d1#activity ee85308a97d1#day ' +
42-
'f89a1501dbb6 f89a1501dbb6#period f89a1501dbb6#activity f89a1501dbb6#day ' +
41+
'e68f4e47517a e68f4e47517a#day ' +
42+
'f0883e931649 f0883e931649#day ' +
43+
'ee85308a97d1 ee85308a97d1#day ' +
44+
'f89a1501dbb6 f89a1501dbb6#day ' +
4345
/* embedded activitiy responsibles: */
4446
'b29d387cc403#embeddedScheduleEntries ' +
45-
'a9a760e36fd8 a9a760e36fd8#activity a9a760e36fd8#campCollaboration b29d387cc403#embeddedActivityResponsibles ' +
47+
'a9a760e36fd8 ' +
48+
'b29d387cc403#embeddedActivityResponsibles ' +
4649
'b29d387cc403#contentNodes ' +
4750
/* collection URI (for detecting addition of new activities) */
4851
'/api/camps/70ca971c992f/activities'

e2e/specs/httpCache/categories.cy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const collectionXKeys =
1313
/* campCollaboration for bipiUser */
1414
'b0bdb7202a9d ' +
1515
/* Category ES */
16-
'ebfd46a1c181 ebfd46a1c181#camp ebfd46a1c181#preferredContentTypes ebfd46a1c181#rootContentNode ebfd46a1c181#contentNodes ' +
16+
'ebfd46a1c181 ebfd46a1c181#preferredContentTypes ebfd46a1c181#rootContentNode ebfd46a1c181#contentNodes ' +
1717
/* Category LA */
18-
'1a869b162875 1a869b162875#camp 1a869b162875#preferredContentTypes 1a869b162875#rootContentNode 1a869b162875#contentNodes ' +
18+
'1a869b162875 1a869b162875#preferredContentTypes 1a869b162875#rootContentNode 1a869b162875#contentNodes ' +
1919
/* Category LP */
20-
'dfa531302823 dfa531302823#camp dfa531302823#preferredContentTypes dfa531302823#rootContentNode dfa531302823#contentNodes ' +
20+
'dfa531302823 dfa531302823#preferredContentTypes dfa531302823#rootContentNode dfa531302823#contentNodes ' +
2121
/* Category LS */
22-
'a023e85227ac a023e85227ac#camp a023e85227ac#preferredContentTypes a023e85227ac#rootContentNode a023e85227ac#contentNodes ' +
22+
'a023e85227ac a023e85227ac#preferredContentTypes a023e85227ac#rootContentNode a023e85227ac#contentNodes ' +
2323
/* collection URI (for detecting addition of new categories) */
2424
'/api/camps/3c79b99ab424/categories'
2525

e2e/specs/httpCache/scheduleEntries.cy.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const collectionXKeys =
1515
'10d8f02ce5b4 ' +
1616
/* scheduleEntries + links */
1717
/* the first scheduleEntry also includes the period id 7fa4564a5d5d */
18-
'e68f4e47517a 7fa4564a5d5d e68f4e47517a#period e68f4e47517a#activity e68f4e47517a#day ' +
19-
'f0883e931649 f0883e931649#period f0883e931649#activity f0883e931649#day ' +
20-
'29c9e9a07d82 29c9e9a07d82#period 29c9e9a07d82#activity 29c9e9a07d82#day ' +
21-
'ee85308a97d1 ee85308a97d1#period ee85308a97d1#activity ee85308a97d1#day ' +
22-
'f08d69cae18a f08d69cae18a#period f08d69cae18a#activity f08d69cae18a#day ' +
23-
'7e8086d94633 7e8086d94633#period 7e8086d94633#activity 7e8086d94633#day ' +
24-
'f89a1501dbb6 f89a1501dbb6#period f89a1501dbb6#activity f89a1501dbb6#day ' +
18+
'e68f4e47517a 7fa4564a5d5d e68f4e47517a#day ' +
19+
'f0883e931649 f0883e931649#day ' +
20+
'29c9e9a07d82 29c9e9a07d82#day ' +
21+
'ee85308a97d1 ee85308a97d1#day ' +
22+
'f08d69cae18a f08d69cae18a#day ' +
23+
'7e8086d94633 7e8086d94633#day ' +
24+
'f89a1501dbb6 f89a1501dbb6#day ' +
2525
/* collection URI (for detecting addition of new schedule entries) */
2626
'/api/periods/7fa4564a5d5d/schedule_entries'
2727

0 commit comments

Comments
 (0)