Configuring memcached and Servlet Client

After some research I found that there is a Win32 binary version of memcached available here.

So go there and download win32 binary version to your computer. When downloaded, extract the file to folder memcahced on your C partition.

You should get only one file named memcached.exe. Now we need to install this as a service, as memCached is daemon and should be run only as service.

To install Memcached as a service, follow these steps (please change all forward slashes to backslashes):

1.If you’re running Vista, you should set memcached.exe to Run as administrator,Install the service using the command:
2.Install the service using the command:

c:/memcached/memcached.exe  -d install

3.Start the server from the Services or by running one of the following commands:

c:/memcached/memcached.exe -d  start

or

net start "memcached Server"

That is it, now your memcached server is installed and is listening to port 11211. There is one more thing left to do. By default, server is set to 64Mb memory limit, which is too small.

Once the configuration is done its time for us to create a client, that will interact with memcached server.

package com.spark.javaee6.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.spy.memcached.MemcachedClient;

/**
 * Servlet implementation class Javaee6Servlet
 */
@WebServlet("/Javaee6Servlet")
public class Javaee6Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Javaee6Servlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		MemcachedClient c=new MemcachedClient(
			    new InetSocketAddress("localhost", 11211));

			// Store a value (async) for one hour
			c.set("sample_key", 3600, "sample_value");

		PrintWriter printWriter = response.getWriter();
		printWriter.println("session key: sample_key, session value: sample_value");
	}

}

in the line 45 the cache is storing a key,value pair. Now if want to retrieve it the following code has to be integrated.

MemcachedClient c=new MemcachedClient(
			    new InetSocketAddress("localhost", 11211));

			// Store a value (async) for one hour
		String value =  (String)c.get("sample_key");

That is it.
Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s