Caching methods

Caching plays an important role in improving your website performance. Below are some commonly used caching mecahnism with PHP


Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached tool is a cache for storing frequently used information to save you from loading (and processing) information from slower sources, such as disks or a database. It can be deployed in a dedicated situation or as a method of using up spare memory in an existing environment.Memcached is currently used by a large number of websites including LiveJournal, Wikipedia, Flickr, Bebo, Twitter, Typepad, Yellowbot, Youtube, Digg, WordPress, Craigslist, and Mixi.The latest stable memcached release is 1.4.22

When to use memcached

Typical sequence for loading information for display

1.Execute one or more queries to load the information from the database
2.Format the information suitable for display (or further processing)
3.Use or display the formatted data

When using memcached, you change the application logic slightly to accommodate the cache: Try loading the information from the cache
If it exists, use the cached version of the information. If it does not exist: Proceed through above steps and store the information into the cache

The key components are:

  • Client software, which is given a list of available Memcached servers.
  • A client-based hashing algorithm, which chooses a server based on the “key” input.
  • Server software, which stores the values with their keys into an internal hash table.
  • Server algorithms, which determine when to throw out old data (if out of memory), or reuse memory.

By default, memcached uses the following settings:

Memory allocation of 64MB
Listens for connections on all network interfaces, using port 11211
Supports a maximum of 1024 simultaneous connections


You’ll likely need to install the development package for libevent
Ubuntu: apt-get install libevent-dev
Redhat/Fedora: yum install libevent-devel

Benefits of using memcached

1. Faster than loading the information each time from disk or re-running query.
2. Scales to multiple servers.
3. Works with many different languages, your objects are not locked into PHP
4. The failure of any memcached server is not critical. For persistent data, you can fall back to an alternative lookup method using database queries, and reload the data into RAM on a different server.


The Alternative PHP Cache (APC) is a free and open source opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code. Most PHP accelerators work by caching the compiled opcode/bytecode of PHP representation of php files to avoid the overhead of parsing and compiling source code on each request. To further improve performance, the cached code is stored in shared memory and directly executed from there, minimizing the amount of slow disk reads and memory copying at runtime.

PHP version: works with all PHP versions up to PHP 5.4
Latest stable version: 3.1.13


eAccelerator is a free open-source PHP accelerator & optimizer. It increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. It also optimizes scripts to speed up their execution. eAccelerator typically reduces server load and increases the speed of your PHP code by 1-10 times. eAccelerator stores compiled PHP scripts in shared memory and executes code directly from it. It creates locks only for a short time, while searching for a compiled PHP script in the cache, so one script can be executed simultaneously by several engines. Files that can’t fit in shared memory are cached on disk only.

The latest release,, features support for PHP 5.3 and also works with PHP 5.1 and 5.2. In older releases, the encoder will only work with PHP versions from the 4.x.x branch. eAccelerator only works with mod_php or php in fastcgi mode. It can’t be used in cgi or cli because eAccelerator needs to set up shared memory, and this can only be done when all php instances that need to access it are forks of the first process.


XCache is a open-source opcode cacher, which means that it accelerates the performance of PHP on servers. It optimizes performance by removing the compilation time of PHP scripts by caching the compiled state of PHP scripts into the shm (RAM). This will increase the rate of page generation time by up to 5 times as it also optimizes many other aspects of php scripts and reduces serverload. XCache is a fast, stable PHP opcode cacher that has been tested and is now running on production servers under high load. It is tested on Linux and FreeBSD and supported under Windows, for thread-safe and non-thread-safe versions of PHP.

The XCache project is lead by mOo who is also a developer of Lighttpd
PHP version: full support for up to PHP 5.6
Latest stable version: 3.2.0


Opcode cache: apc/xcache/eaccelerator are opcode cachers and cache php bytecodes. So it can lead to faster php execution. Memcache/MemcacheD is not an Opcode Cacher. It is a distributed memory caching system. It does not improve the speed/performance of your PHP code. It can be used to store data only.

Object cache: Both apc/xcache/eaccelerator and memcache can be used for object cache. Apc being in process cache can be little faster. But Memcache is a very good variant when you have a cluster of webservers and you need to have one cache for all of them. It can be installed on a remote server and do caching only. Also, some webhosting companies offer Memcached servers with their webhosting plans.

Apache restart: Apache restart resets apc/xcache/eaccelerator cache but it does not reset memcache. This is a good thing as cache warming won’t be needed again. So memcache is better option from this perspective.

Administration: Since memcache is managed by external process, it can be accessed by non php processes also


One of the tools that can be used to enhance caching of the server in an easiest method is installing “Apachebooster”. This will cache both dynamic as well as static pages on cpanel servers. You may download the same from here


There are no revisions for this post.

Tags: , , , , ,

No comments yet.

Leave a Reply