|
| 1 | +======= |
| 2 | +Caching |
| 3 | +======= |
| 4 | + |
| 5 | +The guides library supports optional caching to improve performance when rendering |
| 6 | +documentation repeatedly. This is particularly useful for development workflows |
| 7 | +and CI/CD pipelines where the same templates are rendered multiple times. |
| 8 | + |
| 9 | +Template Caching |
| 10 | +================ |
| 11 | + |
| 12 | +Twig templates can be compiled and cached to avoid re-parsing them on each render. |
| 13 | +This significantly improves performance when rendering large documentation sets. |
| 14 | + |
| 15 | +To enable template caching, pass a cache directory to the ``EnvironmentBuilder``: |
| 16 | + |
| 17 | +.. code-block:: php |
| 18 | +
|
| 19 | + use phpDocumentor\Guides\Twig\EnvironmentBuilder; |
| 20 | + use phpDocumentor\Guides\Twig\Theme\ThemeManager; |
| 21 | +
|
| 22 | + $cacheDir = '/path/to/cache/twig'; |
| 23 | +
|
| 24 | + $environmentBuilder = new EnvironmentBuilder( |
| 25 | + themeManager: $themeManager, |
| 26 | + extensions: $extensions, |
| 27 | + cacheDir: $cacheDir, |
| 28 | + ); |
| 29 | +
|
| 30 | +When caching is enabled: |
| 31 | + |
| 32 | +- Compiled templates are stored in the specified directory |
| 33 | +- Subsequent renders use the cached compiled templates |
| 34 | +- Templates are automatically recompiled when the source changes (``auto_reload: true``) |
| 35 | + |
| 36 | +To disable caching (default behavior), pass ``false`` or omit the parameter: |
| 37 | + |
| 38 | +.. code-block:: php |
| 39 | +
|
| 40 | + // Caching disabled (default) |
| 41 | + $environmentBuilder = new EnvironmentBuilder( |
| 42 | + themeManager: $themeManager, |
| 43 | + extensions: $extensions, |
| 44 | + ); |
| 45 | +
|
| 46 | +Cache Directory Permissions |
| 47 | +--------------------------- |
| 48 | + |
| 49 | +Ensure the cache directory: |
| 50 | + |
| 51 | +- Exists and is writable by the PHP process |
| 52 | +- Is excluded from version control (add to ``.gitignore``) |
| 53 | +- Is cleared when deploying new template versions in production |
| 54 | + |
| 55 | +Symfony Integration |
| 56 | +------------------- |
| 57 | + |
| 58 | +When using the guides library with Symfony's dependency injection, configure |
| 59 | +the cache directory in your services configuration: |
| 60 | + |
| 61 | +.. code-block:: yaml |
| 62 | +
|
| 63 | + # config/services.yaml |
| 64 | + services: |
| 65 | + phpDocumentor\Guides\Twig\EnvironmentBuilder: |
| 66 | + arguments: |
| 67 | + $cacheDir: '%kernel.cache_dir%/guides/twig' |
| 68 | +
|
| 69 | +Performance Impact |
| 70 | +------------------ |
| 71 | + |
| 72 | +Template caching provides the most benefit when: |
| 73 | + |
| 74 | +- Rendering large documentation sets (100+ files) |
| 75 | +- Running repeated builds during development |
| 76 | +- Using complex templates with many includes |
| 77 | + |
| 78 | +For small documentation sets or one-time builds, the overhead of cache management |
| 79 | +may not provide significant benefits. |
0 commit comments