For PHP application speed, speed up, and then speed! Part 3rd: Using the Memcache daemon to cache data in memory
The first two articles in this series provide the technology to speed up the PHP application. The 1th section describes the XCache, which is a php extension that caches PHP opcode into memory. XCache will avoid costly and (strictly speaking) unnecessary costs to recompile the same PHP code to deliver a page. XCache is a free open source software that can be very expensive to install and provide a huge return. The 2nd part introduces Xdebug, which is the PHP extension that configures PHP code. XDebug is something similar to a software X-ray Photo: It will dive into the application, expose the internal workings, and reveal how the code spends its cycles. Once you have the Xdebug metric--not before--you can optimize your code to adjust the algorithm, reduce bottlenecks, and reduce excessive memory usage.
Note: XCache is suitable for production use. It is best to use XDebug during development because its computational load is wasteful in the active computer.
This time, let's explore the third, particularly effective, performance enhancement factor. The Memcache daemon named memcached is a High-performance distributed object cache. The memcached installation location is between the application and the data store, and it saves the object in RAM.
The Memcache PHP extension provides a simple application interface (API) for accessing the cache. To use caching, you need to invoke the API to determine whether the object was previously cached. If it is already cached, simply retrieve the object and continue processing. Otherwise, go to the database, get the necessary data, map it to the object, and add it to the cache. There, memcached will minimize or eliminate database queries for information you have previously processed.
If XCache and XDebug are turbochargers, then memcached is a jet engine. Please be ready to ignite the afterburner.
Higher speed requirements
Typically, the most time-consuming task in a PHP application is data retrieval. In fact, the time it takes to get information from a repository (file or database server) is much higher than compiling and even executing a PHP program. The time required to connect to the database server is a delay, waiting for the query to complete adding additional pauses, and the transfer of the result may even result in more delays. In addition, if the code uses objects, there is an overloaded mapping plane line pointing to the object.
MySQL can use its query cache to speed up the query phase. You can also copy a database (a primary database, multiple replicas) to share the burden of query processing across multiple CPUs. However, once the underlying table changes, the contents of the MySQL query cache will become obsolete. Also, the query cache is hit only if one query is the same as the previous query. There are also restrictions on replicas. For example, you cannot distribute a database write operation.
Fundamentally, although query caching and database replicas are purposeful and have a place in the overall workload management strategy (the query cache consumes some memory, but otherwise it saves; the replica minimizes the risk of disaster downtime), but the connection and transfer times are unchanged.
Memcache PHP Extensions will cache objects into RAM. Each cache hit replaces a round trip to the database server, making the application run faster. You may well find that memcached also (indirectly) improves the performance of the database server, and because memcached is used as an alternative to persistent storage, fewer requests arrive at the database server, allowing the database server to respond more efficiently to queries that have been received.
You can run memcached on one or more servers, and the cached content will be replicated across all nodes. If the server fails, the client API software will reroute the cached read-write operation to a normally running standby server.
Unlike XCache, you must modify your code to integrate memcached. However, if you have carefully isolated the database access code within some object methods, the modifications are likely to be very minor and centralized.
The Memcache daemon written by Danga Interactive is free Open-source software licensed by the free terms of Berkeley Software distribution (BSD) License. Daemons should be easily built on UNIX® and linux® systems, or on Mac OS X and microsoft®windows®. Many Linux distributions offer memcached packages; Check the package repository. If you use Mac OS X or Windows and prefer easy pre-built binaries, you can find such software on the Web with a simple Google search.
(RE) building PHP
Let's build, install, and deploy memcached on Debian Linux. To speed up the execution process and allow you to test memcached independently of the existing WEB server infrastructure, use the XAMPP Apache release as the basis for your build. XAMPP is easy to install and includes Apache V2, MySQL, PHP V4 and V5, Perl, many libraries, and many WEB applications (such as phpMyAdmin). Starting with XAMPP is ideal if you never build Linux, Apache, MySQL, and PHP (LAMP) stacks from scratch, or if you need to avoid the arguments associated with such efforts.
Note: If you have previously built PHP with source code and retained the file, simply add the--enables-memcache option to the list of configuration switches and skip the steps shown earlier in building the memcached and PHP memcache extensions.
To build and deploy memcached, you need a XAMPP release, including XAMPP development files, XAMPP source code for the PHP version that comes with memcached, and a php extension to the Memcache. You can download XAMPP binaries and XAMPP development files from XAMPP (build the prerequisite files for additional components). You can also use wget to quickly get software:
$ wget 'http://www.apachefriends.org/download.php?xampp-linux-1.6.tar.gz'
$ wget 'http://www.apachefriends.org/download.php?xampp-linux-devel-1.6.tar.gz'
The previous tarball (Tarball is a compressed. tar file and usually suffix. tar.gz to the end) contains binary; the latter tarball contains the header files needed to build code for the XAMPP system.
Although you can anchor XAMPP to any location in the file system, install the compression pack into the/opt. At the same time, the development files are installed into the/opt. Using/OPT will make the rest of the build process easier. Use the-C option on tar to extract the files directly into the/OPT, as follows:
Listing 1. Extract the file directly into the/OPT
$ sudo mkdir /opt
$ tar xzf xampp-linux-1.6.tar.gz -C /opt
$ tar xzf xampp-linux-devel-1.6.tar.gz -C /opt
$ ls -CF /opt/lampp
RELEASENOTES error/ info/ logs/ phpsqliteadmin/
backup/ etc/ lampp* man/ sbin/
bin/ htdocs/ lib/ manual/ share/
build/ icons/ libexec/ modules/ tmp/
cgi-bin/ include/ licenses/ phpmyadmin/ var/
Next, download and extract the PHP version of the source code (XAMPP V1.6 the PHP V4.4.6) that shipped with XAMPP, and download the PHP php.net from V4.4.6. Wget will once again make it easy for you to accomplish the task:
$ wget http://us2.php.net/get/php-4.4.6.tar.bz2/from/www.php.net/mirror
$ tar xjf php-4.4.6.tar.bz2
$ cd php-4.4.6
Next, modify the XAMPP PHP build script to rebuild PHP to enable Memcache. You can find the original build script (and other build scripts) in/opt/lampp/share/lampp/configures.tar.gz. Use the following code to extract the PHP V4 build script:
$ tar xzfv /opt/lampp/share/lampp/configures.tar.gz \
php/configure-php4-oswald