Skip to content

Commit ec11e56

Browse files
docs(perfs): compatibility with laravel and reorganize (#2073)
1 parent 41d48dd commit ec11e56

File tree

1 file changed

+87
-9
lines changed

1 file changed

+87
-9
lines changed

core/performance.md

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ Update your Caddyfile with the following configuration:
7171

7272
This will tell to caddy to use the HTTP cache and activate the tag-based invalidation API. You can refer to the [cache-handler documentation](https://github.com/caddyserver/cache-handler) or the [souin website documentation](https://docs.souin.io) to learn how to configure the HTTP cache server.
7373

74-
Setup the HTTP cache invalidation in your API Platform project
74+
Set up HTTP cache invalidation in your API Platform project using the Symfony or Laravel configuration below:
75+
76+
##### Cache Invalidation Configuration using Symfony
7577

7678
```yaml
7779
api_platform:
@@ -83,6 +85,24 @@ api_platform:
8385
purger: api_platform.http_cache.purger.souin
8486
```
8587
88+
##### Cache Invalidation Configuration using Laravel
89+
90+
```php
91+
<?php
92+
// config/api-platform.php
93+
return [
94+
// ....
95+
'http_cache' => [
96+
'invalidation' => [
97+
// We assume that your API can reach your caddy instance by the hostname http://caddy.
98+
// The endpoint /souin-api/souin is the default path to the invalidation API.
99+
'urls' => ['http://caddy/souin-api/souin'],
100+
'purger' => 'api_platform.http_cache.purger.souin',
101+
]
102+
],
103+
];
104+
```
105+
86106
Don't forget to set your `Cache-Control` directive to enable caching on your API resource class.
87107
This can be achieved using the `cacheHeaders` property:
88108

@@ -107,6 +127,8 @@ And voilà, you have a fully working HTTP cache with an invalidation API.
107127

108128
Integration with Varnish and Doctrine ORM is shipped with the core library.
109129

130+
##### Varnish cache invalidation system using Symfony
131+
110132
Add the following configuration to enable the cache invalidation system:
111133

112134
```yaml
@@ -122,12 +144,39 @@ api_platform:
122144
vary: ['Content-Type', 'Authorization', 'Origin']
123145
```
124146
147+
##### Varnish cache invalidation system using Laravel
148+
149+
Add the following configuration to enable the cache invalidation system:
150+
151+
```php
152+
<?php
153+
// config/api-platform.php
154+
return [
155+
// ....
156+
'http_cache' => [
157+
'invalidation' => [
158+
'enabled' => true,
159+
'varnish_urls' => ['%env(VARNISH_URL)%'],
160+
]
161+
],
162+
'defaults' => [
163+
'cache_headers' => [
164+
'max_age' => 0,
165+
'shared_max_age' => 3600,
166+
'vary' => ['Content-Type', 'Authorization', 'Origin'],
167+
]
168+
],
169+
];
170+
```
171+
125172
## Configuration
126173

127174
Support for reverse proxies other than Varnish or Caddy with the HTTP cache module can be added by implementing the `ApiPlatform\HttpCache\PurgerInterface`.
128175
Three purgers are available, the built-in caddy HTTP cache purger (`api_platform.http_cache.purger.souin`), the HTTP tags (`api_platform.http_cache.purger.varnish.ban`), the surrogate key implementation
129176
(`api_platform.http_cache.purger.varnish.xkey`). You can specify the implementation using the `purger` configuration node,
130-
for example, to use the `xkey` implementation:
177+
for example, to use the `Xkey` implementation see the Symfony or Laravel configuration below:
178+
179+
### Exemple of Varnish Xkey implementation using Symfony
131180

132181
```yaml
133182
api_platform:
@@ -147,6 +196,37 @@ api_platform:
147196
glue: ', '
148197
```
149198
199+
### Exemple of Varnish Xkey implementation using Laravel
200+
201+
```php
202+
<?php
203+
// config/api-platform.php
204+
return [
205+
// ....
206+
'http_cache' => [
207+
'invalidation' => [
208+
'enabled' => true,
209+
'varnish_urls' => ['%env(VARNISH_URL)%'],
210+
'purger' => 'api_platform.http_cache.purger.varnish.xkey',
211+
],
212+
'public' => true,
213+
],
214+
'defaults' => [
215+
'cache_headers' => [
216+
'max_age' => 0,
217+
'shared_max_age' => 3600,
218+
'vary' => ['Content-Type', 'Authorization', 'Origin'],
219+
'invalidation' => [
220+
'xkey' => [
221+
'glue' => ', ',
222+
]
223+
],
224+
]
225+
],
226+
];
227+
```
228+
229+
150230
In addition to the cache invalidation mechanism, you may want to [use HTTP/2 Server Push to pre-emptively send relations
151231
to the client](push-relations.md).
152232

@@ -257,20 +337,18 @@ This feature is enabled by default in the production environment of the API Plat
257337
### Search Filter
258338

259339
When using the `SearchFilter` and case insensitivity, Doctrine will use the `LOWER` SQL function. Depending on your
260-
driver, you may want to carefully index it by using a [function-based
261-
index](https://use-the-index-luke.com/sql/where-clause/functions/case-insensitive-search) or it will impact performance
262-
with a huge collection. [Here are some examples to index LIKE
263-
filters](https://use-the-index-luke.com/sql/where-clause/searching-for-ranges/like-performance-tuning) depending on your
264-
database driver.
340+
driver, you may want to carefully index it by using a [function-based index,](https://use-the-index-luke.com/sql/where-clause/functions/case-insensitive-search) or it will impact performance
341+
with a huge collection. [Here are some examples to index LIKE filters](https://use-the-index-luke.com/sql/where-clause/searching-for-ranges/like-performance-tuning) depending on your database driver.
265342

266343
### Eager Loading
267344

268-
By default, Doctrine comes with [lazy loading](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/working-with-objects.html#by-lazy-loading) - usually a killer time-saving feature but also a performance killer with large applications.
345+
By default, Doctrine comes with [lazy loading](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/working-with-objects.html#by-lazy-loading)
346+
- usually a killer time-saving feature but also a performance killer with large applications.
269347

270348
Fortunately, Doctrine offers another approach to solve this problem: [eager loading](https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/working-with-objects.html#by-eager-loading).
271349
This can easily be enabled for a relation: `#[ORM\ManyToOne(fetch: "EAGER")]`.
272350

273-
By default in API Platform, we chose to force eager loading for all relations, with or without the Doctrine
351+
By default, in API Platform, we chose to force eager loading for all relations, with or without the Doctrine
274352
`fetch` attribute. Thanks to the eager loading [extension](extensions.md). The `EagerLoadingExtension` will join every
275353
readable association according to the serialization context. If you want to fetch an association that is not serializable,
276354
you have to bypass `readable` and `readableLink` by using the `fetchEager` attribute on the property declaration, for example:

0 commit comments

Comments
 (0)