Official website for Linux User & Developer
FOLLOW US ON:
Nov
24

Speed up your PHP applications with memcached

by Kieron Howard

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

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

Resources

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 https://github.com/downloads/ 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:// github.com/downloads/libevent/libevent/ libevent-2.1.1-alpha.tar.gz

Step 03

Untar

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

Configure

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:

./configure

…which creates our makefile ready for the build command.

Step 05

Make

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.

memcache
Make

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 127.0.0.1 -p 11211 -v -v’.

memcached
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 http://pear.php.net/go-pear -O go-pear.php

Pages: 1 2
Tags:
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • Pingback: Links 29/11/2012: Dell Sells GNU/Linux Gear at Lower Cost Than Windows, Fedora 18 Reaches Beta | Techrights

    • http://servertechsupport.com Server Management

      If you are using wordpress then you can directly use the below plugin
      http://wordpress.org/extend/plugins/memcached/

    • http://oceanicwealth2173.wordpress.com/2013/04/22/do-not-let-hemorrhoids-slow-you-down/ treatment for piles

      I visited several sites however the audio quality for audio songs current at this web page is in fact superb.

    • http://www.ihatemystepson.com/?do=basic laminate flooring kilmarnock

      I am regular reader, how are you everybody?
      This piece of writing posted at this web site is genuinely good.

      Visit my website at laminate flooring kilmarnock

    • http://20130512111401 bike valet

      hello there and thank you for your info – I’ve certainly picked up something new from right here. I did however expertise several technical points using this site, since I experienced to reload the web site a lot of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and could damage your quality score if ads and marketing with Adwords. Anyway I’m adding this RSS
      to my email and could look out for much more of your respective intriguing content.
      Make sure you update this again soon.Visit my website at bike valet

    • http://Www.Wikiboat.de/index.php/Benutzer:TomokoLem bike expo

      I was suggested this website by my cousin.
      I am not sure whether this post is written by him
      as nobody else know such detailed about my problem.
      You’re amazing! Thanks!Visit my website at bike expo

    • http://www.wikileakdes.org/Obrusy obrusy euromat

      Hey this is somewhat of off topic but I was wanting to
      know if blogs use WYSIWYG editors or if you have
      to manually code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!Visit my website at obrusy euromat

    • http://www.webexperttips.com/ Chelsy Brown

      Superb ! Great tutorial. I have learnt this tutorial . It’s very useful selection . This tutorial will make a good helper for php programmer .Really it will very useful for all users.

      Thanks for sharing great tutorial . Keep it up .