|
1 | 1 | Caching |
2 | 2 | ======= |
3 | 3 |
|
4 | | -FastForward Config supports PSR-16 caching for configuration data. This allows you to cache the merged configuration, improving performance for large or complex setups. |
| 4 | +FastForward Config supports two different caching approaches. They solve related problems, but they are not the same feature. |
5 | 5 |
|
6 | | -How it works |
7 | | ------------- |
8 | | -- Wrap your config with ``configCache($cache, ...)``. |
9 | | -- The first access will store the merged config in the cache. |
10 | | -- Subsequent accesses will read from cache, unless you clear it. |
11 | | -- Any PSR-16 compatible cache (e.g., Symfony, Doctrine, etc) is supported. |
| 6 | +.. list-table:: Cache options |
| 7 | + :header-rows: 1 |
12 | 8 |
|
13 | | -Example |
14 | | -------- |
| 9 | + * - Option |
| 10 | + - Best for |
| 11 | + - Storage |
| 12 | + * - ``configCache()`` or ``CachedConfig`` |
| 13 | + - Caching the final merged config behind any PSR-16 backend. |
| 14 | + - Your cache implementation. |
| 15 | + * - ``cachedConfigFile`` on ``configDir()`` or ``configProvider()`` |
| 16 | + - Reusing a generated PHP cache file for directory or provider aggregation. |
| 17 | + - A file on disk handled by Laminas ConfigAggregator. |
| 18 | + |
| 19 | +Caching With PSR-16 |
| 20 | +------------------- |
| 21 | + |
| 22 | +Use ``configCache()`` when your application already has a PSR-16 cache backend: |
| 23 | + |
| 24 | +.. code-block:: php |
| 25 | +
|
| 26 | + use Psr\SimpleCache\CacheInterface; |
| 27 | + use function FastForward\Config\configCache; |
| 28 | +
|
| 29 | + /** @var CacheInterface $cache */ |
| 30 | + $config = configCache($cache, __DIR__ . '/config'); |
| 31 | +
|
| 32 | +On the first resolution, the merged configuration is stored in the cache. Later resolutions read from the same cache key. |
| 33 | + |
| 34 | +Using A Custom Cache Key Or Persistent Writes |
| 35 | +--------------------------------------------- |
| 36 | + |
| 37 | +Instantiate ``CachedConfig`` directly when you need more control: |
| 38 | + |
| 39 | +.. code-block:: php |
| 40 | +
|
| 41 | + use FastForward\Config\CachedConfig; |
| 42 | + use Psr\SimpleCache\CacheInterface; |
| 43 | + use function FastForward\Config\configDir; |
| 44 | +
|
| 45 | + /** @var CacheInterface $cache */ |
| 46 | + $config = new CachedConfig( |
| 47 | + cache: $cache, |
| 48 | + defaultConfig: configDir(__DIR__ . '/config', recursive: true), |
| 49 | + persistent: true, |
| 50 | + cacheKey: 'app-config', |
| 51 | + ); |
| 52 | +
|
| 53 | +.. warning:: |
| 54 | + |
| 55 | + ``configCache()`` creates ``CachedConfig`` with the default ``persistent: false`` behavior. That means ``set()`` and ``remove()`` update the resolved in-memory config, but they do not write the modified result back to the cache backend unless you instantiate ``CachedConfig`` yourself with ``persistent: true``. |
| 56 | + |
| 57 | +Caching Provider Or Directory Aggregation To A File |
| 58 | +--------------------------------------------------- |
| 59 | + |
| 60 | +If you want Laminas ConfigAggregator to write a cache file, use ``cachedConfigFile``: |
15 | 61 |
|
16 | 62 | .. code-block:: php |
17 | 63 |
|
18 | | - use Symfony\Component\Cache\Simple\FilesystemCache; |
| 64 | + use function FastForward\Config\configDir; |
19 | 65 |
|
20 | | - $cache = new FilesystemCache(); |
21 | | - $config = configCache($cache, ['foo' => 'bar']); |
| 66 | + $config = configDir( |
| 67 | + __DIR__ . '/config', |
| 68 | + recursive: true, |
| 69 | + cachedConfigFile: __DIR__ . '/../var/cache/config.php', |
| 70 | + ); |
22 | 71 |
|
23 | | -Tips |
24 | | ----- |
25 | | -- Use caching in production for best performance. |
26 | | -- You can combine caching with any config aggregation. |
| 72 | +Choose The Right Strategy |
| 73 | +------------------------- |
27 | 74 |
|
28 | | -See also: |
29 | | -- `PSR-16 Simple Cache <https://www.php-fig.org/psr/psr-16/>`_ |
30 | | -- `Live Coverage Report <../../public/coverage/index.html>`_ |
| 75 | +- Use ``configCache()`` when you already have a PSR-16 cache service. |
| 76 | +- Use ``cachedConfigFile`` when you want an explicit generated file for directory or provider aggregation. |
| 77 | +- Clear or rebuild your chosen cache when configuration sources change between deployments. |
0 commit comments