layout | current_menu |
---|---|
documentation |
performances |
PHP-DI uses the definitions you configured to instantiate classes.
Reading those definitions (and, if enabled, reading autowiring or annotations) on each request can be avoided by using a cache.
PHP-DI is compatible with all PSR-16 caches (PSR-16 is a standard for PHP cache systems). To choose which library you want to use, you can have a look at Packagist. You can for example use Symfony's cache component or the PHP Cache library.
There is no cache system installed by default with PHP-DI, you need to install it with Composer. The examples below use the Symfony cache component.
composer require symfony/cache
You can then pass a cache instance to the container builder:
$cache = new Symfony\Component\Cache\Simple\ApcuCache();
$containerBuilder->setDefinitionCache($cache);
Heads up: do not use a cache in a development environment, else all the changes you make to the definitions (annotations, configuration files, etc.) may not be taken into account. The only cache you may use in development is DI\Cache\ArrayCache
(which is the only cache implementation provided by PHP-DI) because it doesn't persist data between requests.
Depending on the cache library you will choose, it will provide adapters to different kind of backends, for example: APCu, Memcache, Redis, Filesystem, etc.
Here is the list of caches Symfony Cache supports: supported adapters.
In production environments, caches based on APCu are recommended. Given PHP-DI's architecture, there will be a cache request for each container entry you get. Remote caches (like Redis or Memcache) will most certainly have a latency too high for such low level calls and will not be efficient.
If you run the same application twice on the same machine, both installs will use the same cache which can cause conflicts.
Conflicts can also happen if an application runs on different "environments" (e.g. production, development…) on the same machine (see the environments documentation).
To avoid this situation, you should use a cache "prefix": each installation of your app has a unique ID, and this ID is used to prefix cache keys to avoid collisions.
You can also add your application's version to the cache prefix so that on each new deployment the cache for the old version is discarded.
For example with Symfony Cache:
$environment = 'prod'; // or 'dev'
$appVersion = '...';
$namespace = 'MySuperApplication-' . $environment . '-' . $appVersion;
$cache = new Symfony\Component\Cache\Simple\ApcuCache($namespace);
$containerBuilder->setDefinitionCache($cache);