Memcache Installation

What is Memcached?

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.

So to faster your application its needed to use the memcache at your application. Lets proceed the main goal for my post.
Lets Install PHP5 Memcache Extension Module for Ubuntu (note: you must have memcached installed prior to installing this extension module. refer to prerequisite install below):

Follow the following steps to install memcache successfully.

  • Install g++ if you haven’t already.
  • sudo apt-get install make g++
  • Prerequisite Install
  • # Download & install libevent (memcached dependency)
    # Run the command at CMD
    # Extract the archieve file
    tar xfz libevent-1.4.8-stable.tar.gz
    # Enter the directory
    cd libevent-1.4.8-stable
    # Then install by run following command
    ./configure && make && sudo make install
  • Create a symlink to libevent
  • # Run command to symlink
    sudo ln -s /usr/local/lib/ /usr/lib
  • Download & install memcached
  • # Run the following command in CMD
    # Download memcache latest version
    # Extract the archive file
    tar xfz memcached-1.2.6.tar.gz
    cd memcached-1.2.6
    ./configure && make && sudo make install
  • Run the Memcache as a daemon
  • # Run memcached as a daemon (d = daemon, m = memory, u = user, l = IP to listen to, p = port)
    memcached -d -m 1024 -u root -l -p 11211
  • PHP5-Memcache Install
  • # Download the extension module
    apt-get install php5-memcache
    #Edit /etc/php5/conf.d/memcache.ini and uncomment the following line by removing the semi-colon
    # Restart your apache server
    /etc/init.d/apache2 restart
  • Example code to Test Memcache Install
  • <?php
    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    $version = $memcache->getVersion();
    echo "Server's version: ".$version."<br/>\n";
    $tmp_object = new stdClass;
    $tmp_object->str_attr = 'test';
    $tmp_object->int_attr = 123;
    $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
    echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
    $get_result = $memcache->get('key');
    echo "Data from the cache:<br/>\n";

Enjoy the caching benefit. Enjoy!!!!!!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s