Tuesday, 29 June 2010

How to Install MEMCACHED on ubuntu?

first of all write the following command on command line:

sudo apt-get install memcached
now go to php.ini file and enable memcache.so


OR

following command automatically append php.ini file and write memcache.so

sudo apt-get install php5-memcache memcached


now check memcached is running or not?

type

/etc/init.d/memcached status

/etc/init.d/memcached start

To check wether 11211 port is enable or not with 127.0.0.1
telnet 127.0.01 11211

Now run the memcached script on server:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<?php

        // put your code here
        $cache = new Memcache();

        $cache->connect("127.0.0.1");
        //addServer("127.0.0.1",11211);
        $user_key = 12345;

    /** Look in cache, if not found, set object (misses null) **/
    if (!$user = $cache->get($user_key)) {
      /**
       * Set object with expire of 1 hour and no compression
       */
      $value = array(34,56,34,23,23,124,65,767,34,76576,99);
      $cache->set($user_key, $value, NULL, 6000);
      $user = $value;
      echo "In";
    }
    else
    {
            $tVar = $cache->get($user_key);
        echo '<PRE>';
        print_r($tVar);
        echo "Out";
    }
        ?>
    </body>
</html>


This script will first time enter in if condition and second time in else condition.... if it does then your memcached is working fine else there is some error.

Cheers!

1 comment:

  1. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    Hire PHP Developers in India

    ReplyDelete