2 Minutes To Code (β Version)

Spy-memcached, java client for memcached - 2 minute tutorial

How to fetch objects from memcache

This example shows how to retrieve an object stored in memcache.

To retrieve the stored object, use the get(...) method of class net.spy.memcached.MemcachedClient. The retrieving application should know before-hand the name under which the required object is stored.

Also notice that the get(...) method returns an object of class java.lang.Object. So the application needs to cast the object to the proper class before using it. Notice, how a cast to java.util.date is performed in the example below.
package com.techfundaes.memcacheBag;

import java.net.InetSocketAddress;
import java.util.Date;

import net.spy.memcached.MemcachedClient;

public class GetObjectsFromMemcache
{
	public static void main(String[] args) throws Exception
	{
		MemcachedClient memcacheClient = new MemcachedClient(new InetSocketAddress("localhost", 11211));
		
		//Object objectToCache = new Object();
		//memcacheClient.set("keyName", 3600, objectToCache);
		//Date startDate = new Date();
		//memcacheClient.set("keyDate", 3600, startDate);
		
		Object fetchedObject = memcacheClient.get("keyName");
		Date fetchedDate = (Date)memcacheClient.get("keyDate"); 
	}
}