Trying to understand and implement Symfony 3 Caching for Framework and Doctrine

We have a working application based on Symfony 3.2 (started with Symfony 2.3) and Doctrine ORM 2.5, and it’s amazing how it all worked out.

I read a lot about the new Symfony Cache component, up and down APC and APCU, opcache, pull requests for chaching in Symfony, etc .... but, to be honest, this time you lost me a little.

Therefore, I am kindly asking if I can support me in understanding and 2) the implementation of caching for the "standard" Symfony / Doctrine application in production.

Background / Assumptions

1) opcache must be enabled and active, and the cache is associated with any byte code.

2) Currently, I have no requirements to cache my own applications. All this relates to caching of the structure, for example annotations, class maps, validations, ORM metadata, etc.

2) Most developers do not want to deal with more than one caching provider, so be it APCu, xcache, redis, memcache or anything else. There can be very good reasons for having different things for different tasks, but let it stick to one so that it is simple.

Caching options in a standard Symfony / Doctrine application in prod mode

1) Class loading

We still have ApcClassLoader in place of app.php :

 $loader = require __DIR__ . '/../app/autoload.php'; include_once __DIR__ . '/../var/bootstrap.php.cache'; $apcLoader = new ApcClassLoader('arcsf2', $loader); $apcLoader->register(true); $loader->unregister(); require_once __DIR__ . '/../app/AppCache.php'; $kernel = new AppKernel('prod', false); $kernel->loadClassCache(); $kernel = new AppCache($kernel); 

In my understanding, there are only two options: the built-in Symfony, ApcClassLoader and XcacheClassLoader. Thus, this may contradict Assumption 2 above.

Question:

Required / required / required / significantly improve the performance of ClassLoaders caching?

Or is it enough to use standard app.php for now?

 $loader = require __DIR__.'/../app/autoload.php'; include_once __DIR__.'/../var/bootstrap.php.cache'; $kernel = new AppKernel('prod', false); $kernel->loadClassCache(); 

2) Validation Caching

We still have this in our config_prod.yml :

 framework: validation: cache: validator.mapping.cache.doctrine.apc 

Question:

Honestly, I have no idea if this really works with Symfony 3.2 and the new Cache component. And how to change it to another cache provider, if necessary. How can I change it to be the "last" using Symfony 3.2 Cache?

3) Doctrine caching:

More or less the same question relates to the section of the orm doctrine in config_prod.yml :

 doctrine: orm: metadata_cache_driver: apc result_cache_driver: apc query_cache_driver: apc 

Question:

Is this still the way to go? How to change this, use the new Symfony Cache component - can this be done anyway?

4) New options?

How about a new one? options? settings in config_prod.yml :

 framework: cache: app: cache.adapter.someProviderOrPool system: cache.adapter.someProviderOrPool 

Question:

What information is cached here, by whom and how does it somehow replace / expand some of the above topics?

Summarizing:

I want to basically change all my prod configs that will comply with Symfony 3.2, and I want to use redis for caching (replacing apc) whenever possible, but I absolutely don't know how and where to start.

**** **** EDIT

As in this context, how do the Symfony Cache Component and DoctrineCacheBundle work together? Replacement? Addition? Building? Are we working together? Conflicting? Not comparable?

+8
symfony doctrine2
source share
1 answer

Symfony performance documentation is out of date. We updated it with another committer, but the Pull request is still pending approval. Now I just copy / paste a new document. Here you can find the GitHub Pull request.

Use OPcache Byte Cache

OPcache stores compiled PHP files so as not to recompile them for each request. Some byte cache codes are available, but with PHP 5.5, PHP comes with built-in OPcache. For older versions, the most widely used cache is APC.

Tuning OPcache for Maximum Performance

The default OPcache configuration is not suitable for a Symfony application, so we recommend that you change these settings as follows:

 ; php.ini ; maximum memory that OPcache can use to store compiled PHP files opcache.memory_consumption=256M ; maximum number of files that can be stored in the cache opcache.max_accelerated_files=20000 

Do not check PHP timestamps

On production servers, PHP files should never be changed unless a new version of the application is deployed. However, by default, OPcache checks to see if the cached files have changed their contents since they were cached. This check introduces some overhead that can be avoided as follows:

 ; php.ini ; after each deploy, call `opcache_reset()` or restart the web server ; to empty the cache and regenerate the cached files. Otherwise you won't ; see the updates made in the application opcache.validate_timestamps=0 

Note

OPcache is different for web server and command console. You cannot clear the OPCache web server by running some command in your terminal. You either need to restart the web server, or call the opcache_reset () function via the web server (i.e., have this in a script that you execute over the Internet).

Configure PHP Real Path Cache

When a relative path is converted to its real and absolute path, PHP caches the result to improve performance. The default configuration of this cache is not suitable for applications that open many PHP files, such as Symfony. It is recommended that you change these settings as follows:

 ; php.ini ; maximum memory allocated to store the results realpath_cache_size=4096K ; save the results for 10 minutes (600 seconds) realpath_cache_ttl=600 

Configure PHP Real Path Cache

PHP uses an internal cache to save the result of mapping file paths to their real and absolute file system paths. This improves the performance of applications such as Symfony, which open many PHP files, especially on Windows systems.

By default, PHP sets the realpath_cache_size value of 16K, which is too small for Symfony. Consider updating this value to at least 4096K. In addition, cached paths are only saved for 120 seconds. Also consider updating this value with the realpath_cache_ttl parameter:

 ; php.ini realpath_cache_size=4096K realpath_cache_ttl=600 

Optimize composer autoloader

The loader class used in developing the application is optimized for finding new and changed classes. On production servers, PHP files should never be changed unless a new version of the application is deployed. Therefore, you can use Composer autoloader optimization to scan the entire application once and create a β€œclass map”, which is a large array of locations of all classes and is stored in the vendor / composer / autoload_classmap.php file.

Run this command to generate the class map during installation (and thus make it part of the deployment process too):

 $ composer install --no-dev --optimize-autoloader --classmap-authoritative --apcu-autoloader 

--no-dev Excludes classes that are needed only in the development environment (for example, tests).

--optimize-autoloader each PSR-0 and PSR-4 compatible class used in your application.

--classmap-authoritative Prevents the collector from scanning the file system for classes that are not found in the class map.

--apcu-autoloader To use this option, you need to install the APCu PHP extension. It will cache classmap in APCu. It will not generate classmap, though, so you should always use it with --optimize-autoloader

Tip

If your production server still uses the legacy APC PHP extension instead of OPcache, install the APCu Polyfill component in your application to enable compatibility with APCu PHP features and unlock support for advanced Symfony features such as the APCu Cache adapter.

Note

When using the APCu autoloader, if you add new classes, they will be found automatically, and everything will work as before (that is, there is no reason to "clear" the cache). However, if you change the location of a specific namespace or prefix, you need to reset the APCu cache. Otherwise, the autoloader will still look at the old location for all classes within this namespace.

0
source share

All Articles