Skip to content

Commit 855bbb5

Browse files
docs(httpcache): document PurgeTagProviderInterface for sub-resource invalidation (#2371)
* docs(httpcache): document PurgeTagProviderInterface extension point Signed-off-by: Guillaume Delré <delre.guillaume@gmail.com> * docs(httpcache): update PurgeTagProviderInterface example with per-operation methods Replace getTagsForResource() with the three dedicated methods (getTagsForInsert, getTagsForUpdate, getTagsForDelete) and show the parent-change invalidation use case that motivated the split. Drop the Laravel section (out of scope). * docs(httpcache): add Laravel registration section for PurgeTagProviderInterface * docs(httpcache): document version and trigger conditions of purge tag providers * docs(httpcache): switch purge tag provider to getTags --------- Signed-off-by: Guillaume Delré <delre.guillaume@gmail.com> Co-authored-by: soyuka <soyuka@users.noreply.github.com>
1 parent ae2c125 commit 855bbb5

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

‎core/performance.md‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,87 @@ final class UserResourcesSubscriber implements EventSubscriberInterface
293293
}
294294
```
295295

296+
### Invalidating Additional Tags on Mutation
297+
298+
Implement `ApiPlatform\HttpCache\PurgeTagProviderInterface` to add extra cache tags to invalidate
299+
when a resource is mutated. This is useful for any tags the built-in listener cannot resolve on its
300+
own: sub-resource collection IRIs (e.g. `/parents/{parentId}/children`), surrogate-key prefixes,
301+
class-based tags, or any custom invalidation strategy. This interface is available since API
302+
Platform 5.1.
303+
304+
> [!NOTE] HTTP cache invalidation must be enabled. Providers are called by the built-in persist and
305+
> remove state processors (Doctrine or Eloquent), for every write handled by API Platform. Changes
306+
> flushed outside of an API Platform operation (for instance directly with Doctrine or Eloquent in a
307+
> console command or a message handler), or by a custom state processor that does not delegate to
308+
> the built-in ones, do not trigger them.
309+
310+
The interface has a single method, `getTags()`. It receives the resource, the operation and the
311+
context:
312+
313+
- On a delete operation (`$operation instanceof DeleteOperationInterface`), providers are called
314+
before the object is removed, so its identifier is still set. Otherwise, they receive the object
315+
returned by the persistence processor.
316+
- `$context['previous_data']` holds the API resource as it was before the request. It is an object
317+
on updates and `null` on creation.
318+
- With `stateOptions` (`entityClass` or `documentClass`, for example with the ObjectMapper),
319+
`$resource` is the entity or document while `$context['previous_data']` is the API resource. Use
320+
`$operation->getClass()` to know which API resource the write is for.
321+
- Every provider is called for every write: return early, with an empty array, for the classes you
322+
do not handle.
323+
324+
The following provider invalidates the sub-resource collection of the previous and of the new parent
325+
when the parent of a child changes:
326+
327+
```php
328+
<?php
329+
// src/HttpCache/ChildPurgeTagProvider.php
330+
namespace App\HttpCache;
331+
332+
use ApiPlatform\HttpCache\PurgeTagProviderInterface;
333+
use ApiPlatform\Metadata\Operation;
334+
use App\Entity\Child;
335+
336+
final class ChildPurgeTagProvider implements PurgeTagProviderInterface
337+
{
338+
public function getTags(object $resource, Operation $operation, array $context = []): iterable
339+
{
340+
if (!$resource instanceof Child) {
341+
return;
342+
}
343+
344+
if (null !== $resource->getParent()) {
345+
yield '/parents/'.$resource->getParent()->getId().'/children';
346+
}
347+
348+
$previousResource = $context['previous_data'] ?? null;
349+
if ($previousResource instanceof Child && null !== $previousResource->getParent()) {
350+
yield '/parents/'.$previousResource->getParent()->getId().'/children';
351+
}
352+
}
353+
}
354+
```
355+
356+
#### Symfony
357+
358+
With
359+
[autowiring and autoconfiguration](https://symfony.com/doc/current/service_container/autowiring.html)
360+
enabled (the default), the service is automatically tagged
361+
`api_platform.http_cache.purge_tag_provider`. To declare the service explicitly:
362+
363+
```yaml
364+
# config/services.yaml
365+
services:
366+
App\HttpCache\ChildPurgeTagProvider:
367+
tags:
368+
- name: api_platform.http_cache.purge_tag_provider
369+
```
370+
371+
#### Laravel
372+
373+
Any class implementing `PurgeTagProviderInterface` placed inside your `app/` directory is
374+
automatically discovered and tagged with `PurgeTagProviderInterface::class` by API Platform's
375+
autoconfiguration, like other API Platform services. No further setup is required.
376+
296377
## Setting Custom HTTP Cache Headers
297378

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

0 commit comments

Comments
 (0)