Getting the Most out of APC for Yii

You are viewing revision #3 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#4) »

Preface

The Advanced PHP Cache is a PHP extension which primarily serves as an opcode cache for PHP. The basic idea is to save PHP from re-evaluating the PHP code to intermediate bytecode on each request. Installing and enabling APC already yields a significant performance benefit. However, APC is not a black box that will magicaly change all for the better. More over it is important to understand that APC needs memory to operate. Hence, by enabling APC you are trading memory for speed.

In addition to the opcode cache, APC can also serve as a user cache for Yii via [CApcCache]. It should be noted that this will make APC's memory needs less predictable.

Installation

APC is best installed by means of your distribution's packaging system. For RedHat based distros (RHEL, CentOS, Fedora, Mandriva), you'll need to install the php-pecl-apc package: ~~~ $ sudo yum install php-pecl-apc ~~~ For Debian based distributions, this will be the php-apc package: ~~~ $ sudo apt-get install php-apc ~~~ If you are running a self-compiled version of PHP, you can install APC via the PEAR/PECL command-line tool: ~~~ $ pecl install APC ~~~ Be advised that this will compile the current version of APC on your system, so you'll need to have the GCC toolchain installed as well as the PHP headers along with the phpize helper. You will almost certainly need to add APC to your PHP configuration. Otherwise, PHP won't load it. Simply drop a new file called apc.ini into /usr/local/etc/php.d (RedHat) or /usr/local/php.d/apache2/conf.d (Debian) with the following content: ~~~ extension=apc.so ~~~

Preparing Yii

  1. Preface
  2. Installation
  3. Configuration
  4. Monitoring APC

The Yii framework comes with a yiilite.php which can be used instead of yii.php. Despite its name, it includes the most used classes. The benefit to APC is that the opcode cache can be quickly filled via one single file instead of digging through several smaller files.

If you wish to use APC as a user cache as well, you'll need to register the CApcCache as cache component in your config. Locate the components-stanza in your config and add the following lines:

'components'=>array(
  'cache'=>array(
    'class'=>'CApcCache',
  ),
  ...
),

Configuration

Server Filesystem

If you have several Yii-based applications running on one server, you might want to consider dropping one copy of the Yii framework (i.e. the content of the framework folder in the release tarball) into /usr/local/share/yii and let the include_path configuration setting point to that. APC will only have to pick up one copy of Yii then. In the process, you'll also conserve a bit of disk space.

APC Settings

APC can be fine-tuned by a number of settings. Just toss them into your apc.ini, restart your webserver (or php-fpm process) and see the magic happening. I found the following options to be important for Yii:

  • apc.enabled Self explaining. This option enables or disables APC alltogether. Be advised that this is a system-wide setting, soyou cannot selectively switch APC on or off for e.g. certain directories via a .htaccess or .user.ini file. You'll want this to be set to on.
  • apc.cache_by_default Controls the default caching behaviour of APC. This can be used together with apc.filters for complex cache setups. But in most cases, you'll want this to be 1.
  • apc.mmap_file_mask Filemask for the shared memory mechanism. The best setting I could find has been /dev/shm/apc.XXXXXX
  • apc.stat Controls if APC should check for modified files on every request. This setting will bring you the greatest speed benefit. Set it to 0 on production servers but to 1 in development environments (otherwise PHP won't pick up changes you've applied to the code). If you're rolling out a new version of your software, simply restart your webserver or FastCGI process in order to clear the opcode cache.
  • apc.lazy_classes and apc.lazy_functions These settings are marked as experimental. However, I've found no regressions after using these. If you're feeling lucky and you're using anonymous functions for evalExpression, you may want to set these to 1.
  • apc.include_once_override Setting this to on will speed up include* and require* calls. However, this setting will introduce changes in the behaviour of PHP. Yii is running fine with it but others (such as phpMyAdmin) aren't so happy. So evaluate carefully if this doesn't break other applications on your server. You've been warned!
  • apc.ttl Controls how long cached opcodes may be cached before being reloaded. Set this to 0 on your productions system so this will never happen. This is most useful together with apc.stat=0.
  • apc.num_files_hint This is a hint for APC to reserve sufficient space for the opcode cache. It helps APC during the initial cache build. It isn't terribly important as it's really just a hint. But while you're at it: Set it to roughly the number of your project's PHP files.
  • apc.user_entries_hint Same as apc.num_files_hint but for user cache entries, so it is only interesting if you're using APC as a user cache, too. A too high value might result in over-provisioning memory for the cache. This is highly individual, so you'll need to evaluate this yourself.
  • apc.shm_size The size of the cache. Take note that is the size of the opcode and the user cache. Currently, APC does not allow you to specify seperate memory segments for these. As a general rule: Do not set this lower than 16MB and monitor APC's memory usage closely. If your setting this too low, APC's memory might suffer heavy fragmentation, which will result in a high performance penalty.

Monitoring APC

Some of the aforementioned settings can be very specific to your setup and your application. So you need to take a look at what APC is doing. There are currently two ways of achieving this:

  • APC itself comes witha bundled apc.php which will tell you a bit about memory consumption, fragmentation and cache content. You'll most likely find it in /usr/local/doc/apc. Copy it to a location where your webserver can reach it and take appropriate measures to protect it from unauthorized access.
  • Available as an extension to your very own app comes the apcinfo extension which does roughly the same job as apc.php but integrates nicely into your app.
15 0
19 followers
Viewed: 51 602 times
Version: Unknown (update)
Category: How-tos
Tags: apc, caching
Written by: Da:Sourcerer
Last updated by: Da:Sourcerer
Created on: Mar 5, 2012
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles