Skip to content

Jesse's Software Engineering Blog

Sep 12

Jesse

PHP 5.5 with Opcache and APCu

As of PHP 5.5 Zend Opcache is a part of the core PHP distribution. PHP code is an interpreted language and is therefore parsed and compiled into opcode on every request. An Opcache allows the opcode to be stored in shared memory in which subsequent requests will be able to pull from, opposed to having to do the entire compilation process. APC, which was an opcache used with PHP prior to 5.5, has been updated to be solely a user cache. This allows a PHP application to store data in shared memory.

Opcache Installation

As stated earlier, PHP 5.5 comes with Opcache, and as of this article, using Ubuntu 14.04, is enabled by default. To install on Ubuntu:

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install apache2
sudo apt-get install php5

To see the status of the Opcache, check the output of a phpinfo() call:

Opcode Caching                 Up and Running
Optimization                   Enabled

The settings for the Opcache can be found in:

/etc/php5/apache2/php.ini

And the extension is included here:

/etc/php5/mods-available

Checking the enabled modules directory shows a symlink to the opcache.ini file:

/etc/php5/apache2/conf.d/

Opcache Configurations

There are only a handful of PHP functions for interacting with the cache, and a variety of run-time configuration options. Some of the key configurations to consider:

opcache.enable – Whether the Opcache is enabled. There is a separate directive for command line PHP: enable_cli

opcache.memory_consumption – The size of the Opcache memory space in MB

opcache.validate_timestamps – How often the cache should validate files. Setting this to 0 will provide optimal performance; however, every time code is introduced to the server the cache will need to be manually cleared

opcache.enable_file_override – If PHP will use cached files in loading via the file() functions. Turning this on may improve performance.

opcache.consistency_checks – How often the cache needs to validate it’s check sums. This should always be off unless debugging.

opcache.fast_shutdown – How the Zend engine handles deallocating resources at the end of a request. Turning this on can improve performance.

opcache.use_cwd – Whether to include the file path in cached file name. Important on projects with shared file names.

opcache.blacklist_filename – List of files to exclude from cache. For example when using WordPress you may not want the admin files to populate cache as they do not affect the user’s experience.

opcache.save_comments – Whether the cached opcode should keep the code’s comments. Be careful as this can break some functionality in certain tools such as frameworks or ORMs.

opcache.load_comments – Specifies whether cached comments should be loaded or not during the request.

When trying to decide the size of the cache I usually start low, browse through all my sites, and see what percentage of the cache has been filled and then adjust accordingly. Setting the validate_timestamps value to 0 is one of the best performance updates that can be done, just remember to reset the cache when new code is introduced. There are numerous GUIs that can be used to monitor the cache. A simple one I’ve used before: Opcache GUI. Simply place the index.php file in a secure location on your server and use the UI to monitor the cache, as well as re-validate files and see stats like hits per file, etc.

APCu Installation

APCu can be installed via the Ubuntu package manager:

sudo apt-get install php5-apcu

The .ini file is located here:

/etc/php5/mods-available

PHP has a list of run-time configurations for APC, and the directives applicable to the user cache still work. I have also written a post on APC configurations. APCu comes with a UI for monitoring the APCu cache:

/usr/share/doc/php5-apcu/apc.php

Again simply move the file to a secure location on your server and use the UI to monitor your cache.

Using APCu

APCu uses the same PHP functions that APC used so you don’t have to worry about breaking your application when switching over. Also, most modern frameworks provide abstractions for interacting with APC. Going over all the functionality of APCu is outside the scope of this post, but should be researched further.

Conclusion

Configuring the Opcache is simple to do and in my opinion worth upgrading to PHP 5.5 to take advantage of. APCu can be a great alternative to Memcache or Redis for small scale applications that don’t need distributed caching systems.

Blog Powered By Wordpress