Installing Memcached

| | Comments (0) | TrackBacks (0)

Memcached is a great caching application which is widely used in many web 2.0 site, such as LiveJournal, Facebook, Sourceforge etc. I'm also very interest in this software, so first step, let's try to install and use it. For more information about Memcached, please refer to http://danga.com/memcached/

Here, I'd like show you how to install Memcached on RedHat Advanced Server Update 6.

Dependencies


Installation:

  • libevent

./configure --prefix=/usr
make && make install

  • memcached

There have many configuration options to use when you configure memcached, all the support options are available from:
./configure --help

./configure --prefix=/home/memcached --with-libevent=/usr
make && make install

Starting memcached
/home/memcached/bin/memcached -d -m 100 -l 192.168.1.136 -p 11211 -u nobody

-d: run as daemon
-m: how many memory in size of MB to allocate to memcached
-l: the listen on address which running the memcached
-p: the port allocate for memcached
-u: the user which running memcached daemon

After starting memcached daemon, we can use 'netstat' to verify the port 11211 is opened.
#netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 192.168.1.136:11211         0.0.0.0:*                   LISTEN

Some simple Perl scripts to test memcached

We have installed and run the memcached on 192.168.1.136, next step, I'll do some tests on another box 192.168.1.137 to push/pull the data from 192.168.1.136.

Before run the Perl testing scripts, we need install 'Cache::Memcached' module, find it from cpan. Then let's run the following two scripts on the client 192.168.1.137.

1. Pushing the data to memcached
Name: push_mem.pl

#!/usr/bin/perl
use Cache::Memcached;
my $memd = new Cache::Memcached {
      'servers' => [ "192.168.1.136:11211"],
    };

# Set a value
$memd->set("my_key", "123");

$memd->disconnect_all();
exit;

2. Pulling the data from memcached
Name: print_mem.pl

#!/usr/bin/perl
use Cache::Memcached;
my $memd = new Cache::Memcached {
      'servers' => [ "192.168.1.136:11211"],
    };
my $val = $memd->get( "my_key" );
if ( $val )
{
     print "Value is '$val'\n";
}

$memd->disconnect_all();
exit;

Result: Value is '123'

Ok, we can see that the value '123' was stored in memcached when I run the 'push_mem.pl' for the first time, and then we get the same value from memcached when I run 'print_mem.pl'.

Here, I only show you the the simplest examples on how to use memcached, but in a production environment, that will be more complex.

Tips:
We can telnet memcached with port 11211 to check the memcached status, it will be helpful for troubleshooting.

[root@web1 scripts]# telnet 192.168.1.136 11211
Trying 192.168.1.136...
Connected to web1.isoracle.com (192.168.1.136).
Escape character is '^]'.
input command: stats
STAT pid 23810
STAT uptime 622
STAT time 1209240944
STAT version 1.2.5
STAT pointer_size 32
STAT rusage_user 0.000999
STAT rusage_system 0.061990
STAT curr_items 1
STAT total_items 1
STAT bytes 58
STAT curr_connections 2
STAT total_connections 5
STAT connection_structures 3
STAT cmd_get 1
STAT cmd_set 1
STAT get_hits 1
STAT get_misses 0
STAT evictions 0
STAT bytes_read 43
STAT bytes_written 36
STAT limit_maxbytes 104857600
STAT threads 1
END

And we can calculate the hint rate, the hint rate= get_hits/ cmd_get

0 TrackBacks

Listed below are links to blogs that reference this entry: Installing Memcached.

TrackBack URL for this entry: http://www.isoracle.com/mt/mt-tb.cgi/21

Leave a comment