Notice: Undefined index: order_next_posts in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 194

Notice: Undefined index: post_link_target in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 195

Notice: Undefined index: posts_featured_size in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 196

Speed up your PHP applications with memcached

How to reduce load on your database by minimising the number of database requests you need to make

memcache web development

The ever increasing complexity of web applications has meant the need for larger and more complicated database schemas. While there are efforts to improve high input/output apps by using non-relational databases such as MongoDB and then pairing them with a non- blocking language such as Node.js, these are still in their infancy, and in many cases tried and tested languages, frameworks and databases are best for the particular job. The number of database calls you make largely depends on your app, and it’s obviously possible to reduce these as much as possible within your code and by increasing server RAM and CPU power.

There are other more efficient (free!) ways of improving app performance, and one of these is by using memcached. Memcached works by storing objects or strings within memory, ready to be used again quickly, without the need to hit the database again. Uses include storing user session data. Memcache is available to be used in many languages, but here we are looking at deploying it using the most common dynamic web scripting language: PHP.

memcache web development
Memcache has a nice PHP extension that allows you to create easy object-orientated calls to the server


LAMP stack running PHP 5.2.0+

Step by Step

Step 01

Check dependencies

The first step is to see whether or not we have the ‘libevent’ library installed on your development machine. To do this, open up a terminal window and type in:

whereis libevent

If the file is found, then you are good to go.

Step 02

Install libevent

To download libevent, in the terminal enter ‘wget libevent/libevent/libevent-2.0.20-stable.tar.gz’ and then wait for it to be grabbed from GitHub. There is also an alpha version from here:

https:// libevent-2.1.1-alpha.tar.gz

Step 03


Once that’s finished downloading, we can unwrap the tarball.

In the terminal, enter ‘tar xfz [your downloaded file name]’ (for example, ‘tar xfz libevent-2.0.20-stable.tar.gz’). This will leave you with a folder.

Step 04


Change into the directory using ‘cd libevent-2.0.20-stable.tar.gz’ and then we can start the build process with a configure command. Enter:


…which creates our makefile ready for the build command.

Step 05


Now we can run our ‘make’ command.

This might take a little while depending on the speed of your machine. Finally, you can enter:

sudo make install

…to finish off the install process. Assuming there were no errors, libevent is now installed.


Step 06

Symlink libevent

Then we can create a symlink to the library, so that is can be accessed from the usr/lib directory. Back in the terminal, enter:

sudo ln -s /usr/local/lib/libevent- 2.0.20-stable /usr/lib

Next we can download memcache.

Step 07

Download memcached

To download memcached, enter:

wget http://memcached.googlecode. com/files/memcached-1.4.15.tar.gz

…which is the latest version. It might be possible to grab it using your favourite package manager, but this way we will make sure we are using the latest version.

Step 08

Untar memcached

As before, we need to untar it using ‘tar xfz memcached-1.4.15.tar.gz.’

Follow that up with ‘cd memcached-1.4.15’, where we are then ready to run the ‘./configure’ command again for memcached.

Step 09

Make memcached

Then enter ‘make’ and once that’s finished, we can run our ‘sudo make install’ to install memcached. After this has finished and you have not received any errors, we are ready to start the memcached daemon.

Step 10

Start memcached

To start memcached we need to give it a few parameters. The -m switch determines how much RAM you want to be available; the minimum is 48MB. The -d switch sets whether it will run daeomonized, and the -v sets the verbosity of output; more -v switches means increased output.

Step 11

Other options

The -p switch sets the port, allowing multiple instances to run on one machine. -l sets the IP; -u the user. Here we’re running as root. An example start-up would be ‘memcached -d -m 1024 -u root -l -p 11211 -v -v’.

Install PEAR

Step 12

Install PEAR

To make our life easy we will install the extension using PEAR. If you hae got it installed already you can skip next few steps. Enter:

wget -O go-pear.php