Skip to content

Commit 68f08ab

Browse files
docs(httpcache): document PurgeTagProviderInterface extension point
Signed-off-by: Guillaume Delré <delre.guillaume@gmail.com>
1 parent 6f7c564 commit 68f08ab

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

‎core/performance.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,59 @@ final class UserResourcesSubscriber implements EventSubscriberInterface
282282
}
283283
```
284284

285+
### Invalidating Additional Tags on Mutation
286+
287+
Implement `ApiPlatform\HttpCache\PurgeTagProviderInterface` to add extra cache tags to invalidate
288+
when an entity is mutated. This is useful for any tags the built-in listener cannot resolve on its
289+
own: sub-resource collection IRIs (e.g. `/parents/{parentId}/children`), surrogate-key prefixes,
290+
class-based tags, or any custom invalidation strategy.
291+
292+
```php
293+
<?php
294+
// src/HttpCache/ChildPurgeTagProvider.php
295+
namespace App\HttpCache;
296+
297+
use ApiPlatform\HttpCache\PurgeTagProviderInterface;
298+
use App\Entity\Child;
299+
300+
final class ChildPurgeTagProvider implements PurgeTagProviderInterface
301+
{
302+
public function getTagsForResource(object $entity): iterable
303+
{
304+
if (!$entity instanceof Child || null === $entity->getParent()) {
305+
return [];
306+
}
307+
308+
yield '/parents/'.$entity->getParent()->getId().'/children';
309+
}
310+
}
311+
```
312+
313+
#### Symfony
314+
315+
With [autowiring and autoconfiguration](https://symfony.com/doc/current/service_container/autowiring.html)
316+
enabled (the default), the service is automatically registered. To declare the service explicitly:
317+
318+
```yaml
319+
# config/services.yaml
320+
services:
321+
App\HttpCache\ChildPurgeTagProvider:
322+
tags:
323+
- name: api_platform.http_cache.purge_tag_provider
324+
```
325+
326+
#### Laravel
327+
328+
Register and tag the implementation in a service provider:
329+
330+
```php
331+
$this->app->bind(\App\HttpCache\ChildPurgeTagProvider::class);
332+
$this->app->tag(
333+
[\App\HttpCache\ChildPurgeTagProvider::class],
334+
\ApiPlatform\HttpCache\PurgeTagProviderInterface::class
335+
);
336+
```
337+
285338
## Setting Custom HTTP Cache Headers
286339

287340
The `cacheHeaders` attribute can be used to set custom HTTP cache headers:

0 commit comments

Comments
 (0)