You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/performance.md
+29-4Lines changed: 29 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,11 +20,36 @@ fresh, because the cache is purged in real time.
20
20
The support for most specific cases such as the invalidation of collections when a document is added or removed or for
21
21
relationships and inverse relations is built-in.
22
22
23
-
We also included [Varnish](https://varnish-cache.org/) in the [Docker setup](../distribution/index.md#using-the-official-distribution-recommended) provided with the
24
-
distribution of API Platform, so this feature works out of the box.
23
+
Integration with Varnish and Doctrine ORM is shipped with the core library, and [Varnish](https://varnish-cache.org/) is included in the [Docker setup](../distribution/index.md#using-the-official-distribution-recommended) provided with the
24
+
distribution of API Platform.
25
+
If you use the distribution, this feature works out of the box.
25
26
26
-
Integration with Varnish and the Doctrine ORM is shipped with the core library. You can easily implement the support for
27
-
any other proxy or persistence system.
27
+
If you don't, add the following configuration to enable the cache invalidation system:
28
+
29
+
```yaml
30
+
parameters:
31
+
# Adds a fallback VARNISH_URL if the env var is not set.
32
+
# This allows you to run cache:warmup even if your
33
+
# environment variables are not available yet.
34
+
# You should not need to change this value.
35
+
env(VARNISH_URL): ''
36
+
37
+
api_platform:
38
+
# ...
39
+
http_cache:
40
+
invalidation:
41
+
enabled: true
42
+
varnish_urls: ['%env(VARNISH_URL)%']
43
+
# Adds sensitive default cache headers
44
+
max_age: 0
45
+
shared_max_age: 3600
46
+
vary: ['Content-Type', 'Authorization']
47
+
public: true
48
+
```
49
+
50
+
Support for reverse proxies other than Varnish can easily be added by implementing the `ApiPlatform\Core\HttpCache\PurgerInterface`.
51
+
52
+
In addition to the cache invalidation mechanism, you may want to [use HTTP/2 Server Push to pre-emptively send relations to the client](push-relations.md).
API Platform leverages this capability by pushing relations of a resource to clients.
12
+
13
+
```php
14
+
<?php
15
+
16
+
namespace App\Entity;
17
+
18
+
use ApiPlatform\Core\Annotation\ApiProperty;
19
+
use ApiPlatform\Core\Annotation\ApiResource;
20
+
21
+
/**
22
+
* @ApiResource
23
+
*/
24
+
class Book
25
+
{
26
+
/**
27
+
* @var Author
28
+
* @ApiProperty(push=true)
29
+
*/
30
+
public $author;
31
+
}
32
+
```
33
+
34
+
By setting the `push` attribute to `true` on a property holding a relation, API Platform will automatically add a valid `Link` HTTP header with the `preload` relation.
35
+
According to the [Preload W3C Candidate Recommendation](https://www.w3.org/TR/preload/), web servers and proxy servers can read this header, fetch the related resource and send it to the client using Server Push.
36
+
[NGINX](https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/), [Apache](https://httpd.apache.org/docs/current/howto/http2.html#push), [CloudFlare](https://www.cloudflare.com/website-optimization/http2/serverpush/), [Fastly](https://docs.fastly.com/guides/performance-tuning/http2-server-push) and [Akamai](https://blogs.akamai.com/2017/03/http2-server-push-the-what-how-and-why.html) honor this header.
37
+
38
+
Using this feature maximises HTTP cache hits for your API resources.
39
+
For best performance, this feature should be used in conjunction with [the built-in HTTP cache invalidation system (based on Varnish)](performance.md#enabling-the-built-in-http-cache-invalidation-system).
0 commit comments