acmeextension

How to install, Configure and Use Memcached in Ubuntu

By: Sunil Kumar |  In: PHP  |  Last Updated: 2018/06/13

When you are working on a high traffic web application the main component that got loaded is backend database. Especially in case, your database is not designed in such a way that it can handle high load and the database is not distributed. There are many ways to deal with this problem. One of them is using a caching system like Memcached. Memcached is a 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 is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. In this guide, we're going to discuss how to install, configure and use Memcached on an Ubuntu server.

Installing Memcached and its Components

To get started with Memcached first we need to get all the necessary component from the Ubuntu Repository which is not a tough task. To get all the updated Component first update your system using

$ sudo apt-get update

now to install Memcached

$ sudo apt-get install php-memcached memcached

Here I am supposing that you have already installed PHP and MySQL-server. If not you need to install these first. for this, you can use

$ sudo apt-get install mysql-server php5-mysql php5 php5-memcached memcached

This should install and configure everything you need.

Check Memcached Installation

After installation, our first step should be to verify whether it is installed correctly or not. There are several ways to verify that. The first way is using PHP. Create a .php(info.php) file in your project(Here in the root directory). our default document root is /var/www/html. and put following code into it

<?php
 phpinfo();
?>

now when we visit http://localhost/info.php we will find all information about PHP's configuration If you scroll down or search for the "Memcached" section header, you should find something that looks like this: This means that the Memcached extension is enabled and being found by the web server. We can also check by typing this command in terminal

$ ps aux | grep memcached
memcache 975 0.0 0.0 48740 2056 ? Ssl 22:38 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
times 6600 0.0 0.0 5120 840 pts/18 S+ 23:49 0:00 grep --color=auto memcached

Start, Stop, Restart Memcached service

To Start, Stop, Restart Memcached service we can use the following command

sudo service memcached start
sudo service memcached stop
sudo service memcached restart

Storing data inside Memcached

Now at this point, we are sure that Memcached is installed successfully and working fine, We can test it for data caching. For this, we have to store some data into Memcache. To do so we have to create a .php script where we going to write the code-

 <php
      //memcached_test.php
      /* connecting to the memcached server */
      $mem = new Memcached();         
      $mem->addServer('127.0.0.1', 11211);
      
       /*setting data into Memcached  into "first_data_key" key for 5 minutes   */
       $data = $mem->set('first_string_data','first value into memcached',300);

       /* storing an array into Memcached  */
       $value = array('key1' => 'value', 'key2'=>'value2'  );
       $array_data = $mem->set('first_array_data',$value,300);
    ?>

As Memcached store data in the key-value pair, we are storing data into "first_data_key" key.

Retrieving data from Memcache

Retrieving data from Memcached is more simple then storing data into it. Suppose we wat to retrieve the data we set above

<?php
   //memcached_test.php
   $mem = new Memcached();
   $mem->addServer('127.0.0.1', 11211);

   $string_data= $mem->get('first_string_data');
   var_dump($string_data);
   echo "<br>";
   $array_data= $mem->get('first_array_data');
   var_dump($array_data);

when we browse http://localhost/memcached_test.php output should be like this

Deleting data from Memcached

We can easily delete the data by the key which is used at the time of storing the data. If we want to delete the data of "first_array_data" key

<?php
   $mem = new Memcached();
   $mem->addServer('127.0.0.1', 11211);
   $mem->delete('first_array_data');

Working on Memcached Command line


We also can use telnet to do all the above stuff directly from the terminal or command line

Connecting to Memcached server

$ telnet <hostname> <port>

eg. telnet localhost 11211 Setting the data

$ set <key> <flags> <exptime> <bytes> [noreply]\r\n<value>\r\n

eg.

$ set first_command-line_data 0 900 4 data

Getting data from key

$ get key
eg.
$ get first_command-line_data

Flush(delete) Memcache data Flush the server key/value pair (invalidating them) after an optional [

$ flush_all [
$ flush_all 10

if we have to flush all Memcached data

flush_all

Conclusion

By now, you should have a decent understanding of how Memcached works and how you can leverage it to keep your web server from hitting the database repeatedly for the same content. Memcached is most useful when you have to query data from database most of the time or your data keep changing frequently. But if your content is static or it changes infrequently, Varnish cache is more pleasant.

You can read full varnish installation and setup guide here.

Comments


Leave a Comment

Your email address will not be published.

*


Sunil Kumar


I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Subscribe to our mailing list


%d bloggers like this: