Skip to content

Commit 2a1e76e

Browse files
authored
added extending Cache-Tags
1 parent 5b1a390 commit 2a1e76e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

core/performance.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,48 @@ distribution of API Platform, so this feature works out of the box.
2626
Integration with Varnish and the Doctrine ORM is shipped with the core library. You can easily implement the support for
2727
any other proxy or persistence system.
2828

29+
### Extending Cache-Tags for invalidation
30+
31+
Sometimes you need individual resources like `/me`. To work properly with Varnish, the cache tags need to be augmented with these resources. Here is an example how this can be done:
32+
33+
```php
34+
<?php
35+
36+
declare(strict_types=1);
37+
38+
namespace AppBundle\EventSubscriber;
39+
40+
use ApiPlatform\Core\EventListener\EventPriorities;
41+
use AppBundle\Entity\User;
42+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
43+
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
44+
use Symfony\Component\HttpKernel\KernelEvents;
45+
46+
final class UserResourcesSubscriber implements EventSubscriberInterface
47+
{
48+
public static function getSubscribedEvents()
49+
{
50+
return [
51+
KernelEvents::VIEW => ['extendResources', EventPriorities::POST_WRITE],
52+
];
53+
}
54+
55+
public function extendResources(GetResponseForControllerResultEvent $event)
56+
{
57+
$user = $event->getControllerResult();
58+
$request = $event->getRequest();
59+
60+
if ($user instanceof User) {
61+
$resources = [
62+
'/me'
63+
];
64+
65+
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array)$resources);
66+
}
67+
}
68+
}
69+
```
70+
2971
## Enabling the Metadata Cache
3072

3173
Computing metadata used by the bundle is a costly operation. Fortunately, metadata can be computed once and then cached.

0 commit comments

Comments
 (0)