Commit aa73e1d
committed
feature #22 [Cache] Cache the parsed entrypoints.json (Kocal)
This PR was squashed before being merged into the main branch.
Discussion
----------
[Cache] Cache the parsed entrypoints.json
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | -
| License | MIT
Adds an opt-in `cache` config key to `RepriseBundle` (default `false`), mirroring the option WebpackEncoreBundle already has.
Right now, under PHP-FPM, `EntrypointsLookup` re-reads and `json_decode`s `entrypoints.json` on every single request before it can resolve one URL. With `cache: true`, the file is parsed once during `cache:warmup` and the resulting `Entrypoints` object is compiled into a `PhpArrayAdapter` file (`%kernel.build_dir%/reprise.cache.php`). At runtime the bundle just reads that object back instead of decoding JSON on each request.
A few implementation details worth flagging for review:
- `EntrypointsLookup` now goes through an optional PSR-6 cache: hit returns the cached object, miss reads the file and saves it. With `cache: false` (the default), nothing changes.
- `EntrypointsCacheWarmer` writes the compiled file and never throws: a missing or malformed `entrypoints.json` at deploy time shouldn't be able to break `cache:warmup`.
- What gets cached is the built `Entrypoints` object, not the raw array. `PhpArrayAdapter` exports it via `symfony/var-exporter`, which handles the bundle's `final`/`readonly` classes without needing `__set_state`. Nice side effect: validation runs once at warmup instead of on every request.
- The cache pool and warmer are only wired when `cache: true`, behind a `class_exists()` guard that gives a clear error if the Cache component isn't there. `symfony/cache` and `symfony/var-exporter` stay `require-dev`, so projects that don't enable caching don't pull them in.
- The cache is keyed once (Reprise is mono-build), and it's refreshed by your deploy's `cache:clear` + `cache:warmup`. So after rebuilding assets, run `cache:clear`. This is a production optimization, not something you'd want in dev.
## Usage
```yaml
# config/packages/reprise.yaml
reprise:
cache: true
```
Enabling it requires the Cache component:
```
$ composer require symfony/cache
```
## Testing
Unit tests cover the cache read path (`ArrayAdapter`) and the warmer (round-tripping a real compiled file), plus a functional test booting a `cache: true` kernel. Added a `cache` entry to the Configuration section of `doc/index.rst`.
Commits
-------
f34968e [Cache] Cache the parsed entrypoints.json23 files changed
Lines changed: 1110 additions & 18 deletions
File tree
- config
- docs/superpowers
- plans
- specs
- doc
- playground
- config
- packages
- routes
- src
- Asset
- CacheWarmer
- tests
- Asset
- CacheWarmer
- Functional
- Kernel
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
157 | 160 | | |
158 | 161 | | |
159 | 162 | | |
| |||
171 | 174 | | |
172 | 175 | | |
173 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
174 | 180 | | |
175 | 181 | | |
176 | 182 | | |
| |||
0 commit comments