Tutorial

How To Install and Use Memcache on Ubuntu 12.04

How To Install and Use Memcache on Ubuntu 12.04
Not using Ubuntu 12.04?Choose a different version or distribution.
Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Memcache

Memcache is a system that works to speed up virtual private servers by caching server information. The program allows you to allocate a specific amount of the server ram toward caching recently queried data for a certain amount of time. Once the data is requested again, memcache speeds up the process of retrieving it by displaying the cached information instead of generating the result from the database.

Setup

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Basic Users Tutorial. Before starting off, it’s a good idea to update apt-get to make sure that all of the packages we download to the VPS are up to date.

sudo apt-get update

Additionally, you should have MySQL and PHP installed on the virtual server.

sudo apt-get install mysql-server php5-mysql php5 php5-memcache

Install Memcache

Installing memcache takes several steps.

To start, install memcached via apt-get.

sudo apt-get install memcached

The next step is to install php-pear, the repository that stores memcache.

sudo apt-get install php-pear

If you do not have a compiler on your server, you can download build-essential in order to install memcache:

sudo apt-get install build-essential

Finally use PECL (PHP Extension Community Library) to install memcache:

sudo pecl install memcache

Say yes by pressing enter during the installation when you are asked if you would like to “Enable memcache session handler support? [yes] :”

Once you have completed the installation of memcache with PECL on the VPS, add memcached to memcache.ini:

echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini

Now you are ready to start using Memcache.

Confirm Memcache and See Stats

After Memcache is downloaded, you can check that it has been installed by searching for it:

ps aux | grep memcache

Additionally, you can see the memcache stats by typing:

echo "stats settings" | nc localhost 11211

Step Three—How Memcache Works

Memcache works by redirecting code to first attempt to retrieve data from the cache before querying the server’s database. The cache populates by saving recently retrieved server data for a certain amount of time. By caching recently requested information, future queries do not have to go through the longer process of retrieving the information from a database and can, instead, access it through the cache.

The memcache page shows this abbreviated code on its homepage to summarize the memcache process:

function get_foo(foo_id)
    foo = memcached_get("foo:" . foo_id)
    return foo if defined foo

    foo = fetch_foo_from_database(foo_id)
    memcached_set("foo:" . foo_id, foo)
    return foo
end

A Simple Memcache Example

This section will set up a simple php script to use memcache for retrieving a single value originally found in a mysql table.

The following steps set up a mysql user who can access the appropriate database, create a table to query, and insert the one value that we will test in the new mysql table.

Log into mysql: mysql -u root -p and execute the following commands:

use test;

grant all on test.* to test@localhost identified by 'testing123';

create table example (id int, name varchar(30));

insert into example values (1, "new_data");

exit;

Once you have exited MySQL, create the memcache script file:

nano memtest.php

We are now going to build up the php script step by step (the entire script will be at the end of the section):

  • Start off by creating a new persistent connection with memcache, which runs on memcache’s default port, 11211.
    <?php
    $meminstance = new Memcache();
    $meminstance->pconnect('localhost', 11211);
  • The next step is to connect to the new mysql database with the user that we created earlier:
    mysql_connect("localhost", "test", "testing123") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
  • After that, go ahead and create the query that we will pose to the server, as well as provide a key to identify that specific action:
    $query = "select id from example where name = 'new_data'";
    $querykey = "KEY" . md5($query);
  • The script first searches the cache for the answer to the query. If the result does not exist, the script reroutes the question to the original database. Once the query has been answered by the original database, the script stores the result in memcache, using the “set” command-- which both saves it and allows the user to designate the number of seconds that it should remain in the cache (600 would save it in the cache for 10 minutes).

    When we run the script for the first time, it will inform us that the data was collected from the mysql database. However, as it does so, it stores the information in the cache, so that a second run of the script retrieves it from the cache and lets the user know.

    In 10 minutes the cache is emptied once more and running the script will make it access the database once again.

    $result = $meminstance->get($querykey);
    
    if (!$result) {
           $result = mysql_fetch_array(mysql_query("select id from example where name = 'new_data'")) or die('mysql error');
           $meminstance->set($querykey, $result, 0, 600);
    print "got result from mysql\n";
    return 0;
    }
    
    print "got result from memcached\n";
    return 0;
    
    ?>

Altogether the script looks like this:

<?php
$meminstance = new Memcache();
$meminstance->pconnect('localhost', 11211);

mysql_connect("localhost", "test", "testing123") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$query = "select id from example where name = 'new_data'";
$querykey = "KEY" . md5($query);

$result = $meminstance->get($querykey);

if (!$result) {
       $result = mysql_fetch_array(mysql_query("select id from example where name = 'new_data'")) or die('mysql error');
       $meminstance->set($querykey, $result, 0, 600);
print "got result from mysql\n";
return 0;
}

print "got result from memcached\n";
return 0;

?>

Running the script on the command line produces the following results:

# php memtest.php 
got result from mysql

# php memtest.php 
got result from memcached

# php memtest.php 
got result from memcached

Conclusion

This tutorial covers speeding up the retrieval of data from a database by connecting it to memcache. However, do keep in mind that memcache’s strengh originates from the fact that is a cache—it is not a datastore. When using memcache, do not expect it to replace a database. Because memcache only holds values for a set length of time for a given key, you may not always find the information you need cached, and in cases like these, having the original server database is imperative.

Nevertheless, memcache is a very useful program and can do a lot to increase the server efficiency.

If you have any other questions about Memcache, feel free to ask about specifics on our forum.

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

i got this error message: “PHP Fatal error: Class ‘Memcache’ not found in /home/tommy/www/memtest.php on line 2”

i don’t think i missed anything…

sorry i missed the following step: “sudo apt-get install mysql-server php5-mysql php5 php5-memcache”

problem solved… :) thanks for this awesome tutorial…

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
November 1, 2012

Thanks! Glad it worked out =]

Thank You!

hi.how about php connection.is it cashed too.or just the query cahsed?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
December 2, 2012

If you want to speed up PHP connections to MySQL you would need to turn on persistent connections. If persistent connections are enabled when a script finishes executing it doesn’t close the MySQL connection, this allows the next time that the script is called with the same credentials to use an already open connection. This improves script response times as the script no longer has the overhead of opening and then closing the MySQL connection.

Memcache will cache queries so that if a script calls the same query repeatedly after the first time it is called and goes through MySQL to pull the data it will be cached in memcache. Then the next hit to that script will get that information from memcache without needing to run the query, again significantly improving performance.

Does it automatically cache the queries?

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
January 30, 2013

This article lays out how to install and use memcache so if you follow the simple guide on how to run your queries using memcache then you will be caching them with memcache before the query hits the DB.

Hi Guys, This method worked for me - this was a really helpful tutorial. Thanks a lot =) Well, i had to Restart the PHP-FPM after the set-up; until then the page was showing “Page temporarily not available.”

sudo /etc/init.d/php5-fpm restart

Just thought of adding this info. Thanks again.

Sorry, but

sudo echo “extension=memcache.so” > sudo /etc/php5/conf.d/memcache.ini

just creates a file sudo in the current directory. The command should read

sudo echo “extension=memcache.so” > /etc/php5/conf.d/memcache.ini

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.