使用byte code cache
多数byte code cache
For this reason, some byte code caches offer an option to disable these checks.
For example, to disable these checks in APC, simply add
apc.stat=0 to your
For the same reasons, the byte code cache must also be cleared when deploying
the application (for example by calling
apc_clear_cache() PHP function when
using APC and
opcache_reset() when using OPcache).
In PHP, the CLI and the web processes don't share the same OPcache. This
means that you cannot clear the web server OPcache by executing some command
in your terminal. You either need to restart the web server or call the
opcache_reset() functions via the web server
(i.e. by having these in a script that you execute over the web).
By default, PHP's OPcache saves up to 2,000 files in the byte code cache. This number is too low for the typical Symfony application, so you should set a higher limit with the opcache.max_accelerated_files configuration option:
; php.ini opcache.max_accelerated_files = 20000
默认时PHP sets a
16K which is too low for
Symfony. Consider updating this value at least to
4096K. In addition, cached
paths are only stored for
120 seconds by default. Consider updating this
value too using the
1 2 3
; php.ini realpath_cache_size=4096K realpath_cache_ttl=600
By default, the Symfony Standard Edition uses Composer's autoloader in the autoload.php file. This autoloader is easy to use, as it will automatically find any new classes that you've placed in the registered directories.
Unfortunately, this comes at a cost, as the loader iterates over all configured
namespaces to find a particular file, making
file_exists() calls until it
finally finds the file it's looking for.
The simplest solution is to tell Composer to build an optimized "class map",
which is a big array of the locations of all the classes and it's stored
$ composer dump-autoload --optimize --no-dev --classmap-authoritative
Another solution is to cache the location of each class after it's located
the first time. Symfony comes with a class -
that does exactly this. To use it, just adapt your front controller file.
If you're using the Standard Distribution, make the following changes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// app.php // ... use Symfony\Component\ClassLoader\ApcClassLoader; $loader = require __DIR__.'/../app/autoload.php'; include_once __DIR__.'/../app/bootstrap.php.cache'; // Use APC for autoloading to improve performance // Change 'sf2' by the prefix you want in order // to prevent key conflict with another application $loader = new ApcClassLoader('sf2', $loader); $loader->register(true); // ...
For more details, see Cache a Class Loader.
To ensure optimal flexibility and code reuse, Symfony applications leverage a variety of classes and 3rd party components. But loading all of these classes from separate files on each request can result in some overhead. To reduce this overhead, the Symfony Standard Edition provides a script to generate a so-called bootstrap file, consisting of multiple classes definitions in a single file. By including this file (which contains a copy of many of the core classes), Symfony no longer needs to include any of the source files containing those classes. This will reduce disc IO quite a bit.
If you're using the Symfony Standard Edition, the bootstrap file is automatically
rebuilt after updating the vendor libraries via the
composer install command.
Even when using a byte code cache, performance will improve when using a bootstrap
file since there will be fewer files to monitor for changes. Of course if this
feature is disabled in the byte code cache (e.g.
apc.stat=0 in APC), there
is no longer a reason to use a bootstrap file.
本文，包括例程代码在内，采用的是 Creative Commons BY-SA 3.0 创作共用授权。