Skip to content

Extending Cache-Tags docs for varnish #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions core/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@ distribution of API Platform, so this feature works out of the box.
Integration with Varnish and the Doctrine ORM is shipped with the core library. You can easily implement the support for
any other proxy or persistence system.

### Extending Cache-Tags for invalidation

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:

```php
<?php

declare(strict_types=1);

namespace AppBundle\EventSubscriber;

use ApiPlatform\Core\EventListener\EventPriorities;
use AppBundle\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final class UserResourcesSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['extendResources', EventPriorities::PRE_READ],
KernelEvents::REQUEST => ['extendResources', EventPriorities::POST_READ]
];
}

public function extendResources(GetResponseEvent $event)
{
$class = $event->getRequest()->attributes->get('_api_resource_class');
$request = $event->getRequest();

if ($class === User::class) {
$resources = [
'/me'
];

$request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array)$resources);
}
}
}
```

## Enabling the Metadata Cache

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