Skip to content

Commit f3cac0b

Browse files
authored
Merge pull request #404 from iCodr8/patch-3
Extending Cache-Tags docs for varnish
2 parents 8afb4d9 + dd38679 commit f3cac0b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

core/performance.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,49 @@ 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\GetResponseEvent;
44+
use Symfony\Component\HttpKernel\KernelEvents;
45+
46+
final class UserResourcesSubscriber implements EventSubscriberInterface
47+
{
48+
public static function getSubscribedEvents()
49+
{
50+
return [
51+
KernelEvents::REQUEST => ['extendResources', EventPriorities::PRE_READ],
52+
KernelEvents::REQUEST => ['extendResources', EventPriorities::POST_READ]
53+
];
54+
}
55+
56+
public function extendResources(GetResponseEvent $event)
57+
{
58+
$class = $event->getRequest()->attributes->get('_api_resource_class');
59+
$request = $event->getRequest();
60+
61+
if ($class === User::class) {
62+
$resources = [
63+
'/me'
64+
];
65+
66+
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array)$resources);
67+
}
68+
}
69+
}
70+
```
71+
2972
## Enabling the Metadata Cache
3073

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

0 commit comments

Comments
 (0)