2 Minutes To Code (β Version)

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

How to put objects in memcache

This example shows how to put objects in the memcached server.

Use the set(...) method of net.spy.memcached.MemcachedClient object, passing it the unique name by which to identify the object, expiry time period of the object in seconds and the object itself. In the example below, the expiry time used is 3600 seconds.

package com.techfundaes.memcacheBag;

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

import net.spy.memcached.MemcachedClient;

public class StoreObjectsInCache
	public static void main(String[] args) throws Exception
		MemcachedClient memcacheClient = new MemcachedClient(new InetSocketAddress("localhost", 11211));
		Object objectToCache = new Object();
		memcacheClient.set("keyObject", 3600, objectToCache);
		Date startDate = new Date();
		memcacheClient.set("keyDate", 3600, startDate);