Sunday, August 15, 2010

? Memcached and Java

1. Start the Memcache
$memcached -d -m 512 127.0.0.1 -p 1121


2. Check Memcache by Telnet  
$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get customer
END
set customer 0 3600 10  
complexity
STORED
get customer
VALUE customer 0 10
complexity
END


quit


Out of the various Java Memcached clients, I prefer to use Dustin's Spymemcached client.


Complete Spymemcached list of JAVA api is available here.




Sample code on how to create a connection:



MemcachedClient c=new MemcachedClient(
                new InetSocketAddress("hostname", portNum));

        // Store a value (async) for one hour
        c.set("someKey", 3600, someObject);
        // Retrieve a value.
        Object myObject=c.get("someKey");





                               OR





// Get a memcached client connected to several servers
      // over the binary protocol
      MemcachedClient c = new MemcachedClient(new BinaryConnectionFactory(),
              AddrUtil.getAddresses("server1:11211 server2:11211"));

      // Try to get a value, for up to 5 seconds, and cancel if it
      // doesn't return
      Object myObj = null;
      Future f = c.asyncGet("someKey");       try {           myObj = f.get(5, TimeUnit.SECONDS);       // throws expecting InterruptedException, ExecutionException       // or TimeoutException       } catch (Exception e) {  /*  /           // Since we don't need this, go ahead and cancel the operation.           // This is not strictly necessary, but it'll save some work on           // the server.  It is okay to cancel it if running.           f.cancel(true);           // Do other timeout related stuff       }








Saturday, August 14, 2010

? Memcached in Ubuntu Server 10.04

Memcache is a very simple caching system that uses memory to store objects. As you may already know, memory is faster to read than hard drive. A site will take a lot less time to read a database result cached in memory. To make the best use of memcache, you should install the memcached module. 


First, install the apache2, memcached package, php-pear , php5-dev and libmemcached-de


Install the following in the below order. 
sudo apt-get install memcached
sudo apt-get install php-pear
sudo apt-get install php5-dev
sudo apt-get install libmemcached-dev
sudo pecl install Memcache


and 
sudo echo "extension=memcache.so" >/etc/php5/apache2/conf.d/memcache.ini


Add the following line anywhere, to your php.ini file.
memcache.hash_strategy="consistent"


Start an instance of memcache daemon with the following command:
memcached -d -m 2048 -l 127.0.0.1 -p 11211


If you are only using a single server, then you only need to include this line to your site’s settings.php file.
$conf['cache_inc'] = '/sites/all/modules/memcache/memcache.inc';


The final step is to restart apache and then switch your site back online.





Following command helps to get the connection status :
 $netstat -na | grep 11211




? Install Apache2 in Ubuntu Server 10.04

Installation



The Apache2 web server is available in Ubuntu Linux. To install Apache2:
  • At a terminal prompt enter the following command:
    sudo apt-get install apache2

? Enable FTP in ubuntu


Vsftpd is an FTP daemon available in Ubuntu. It is easy to install, set up, and maintain. To install vsftpd you can run the following command:
sudo apt-get install vsftpd
To restart the daemon
sudo /etc/init.d/vsftpd restart