すいません。インストールしてからずいぶん間が空いてしまいましたね。。。
いやいや、というのもですね。
業務上で必要になるからってmemcached勉強しようと思ったのに、結局業務で使ってるのRedisなんですもん。

はい。すいません。ただの言い訳です。

とりあえずjavaから使ってみますね。

使うライブラリはこれ。
Memcached-Java-Client

ここからダウンロードするのがお手軽です。現時点での最新版は2.6.6。
https://github.com/gwhalin/Memcached-Java-Client/downloads

適当に新規プロジェクト作成。上記のzipを展開して出てきたjarをプロジェクトに全部つっこむ。

そしておもむろにHOWTOに載ってるソースをコピー&ペースト…!

ちょちょっと修正。

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemcachedTest {
	// create a static client as most installs only need
	// a single instance
	protected static MemCachedClient mcc = new MemCachedClient();

	// set up connection pool once at class load
	static {
		// server list and weights
		String[] servers = {
			"localhost:11211"
		};

		Integer[] weights = { 3 };

		// grab an instance of our connection pool
		SockIOPool pool = SockIOPool.getInstance();

		// set the servers and the weights
		pool.setServers( servers );
		pool.setWeights( weights );

		// set some basic pool settings
		// 5 initial, 5 min, and 250 max conns
		// and set the max idle time for a conn
		// to 6 hours
		pool.setInitConn( 5 );
		pool.setMinConn( 5 );
		pool.setMaxConn( 250 );
		pool.setMaxIdle( 1000 * 60 * 60 * 6 );

		// set the sleep for the maint thread
		// it will wake up every x seconds and
		// maintain the pool size
		pool.setMaintSleep( 30 );

		// set some TCP settings
		// disable nagle
		// set the read timeout to 3 secs
		// and don't set a connect timeout
		pool.setNagle( false );
		pool.setSocketTO( 3000 );
		pool.setSocketConnectTO( 0 );

		// initialize the connection pool
		pool.initialize();

		// lets set some compression on for the client
		// compress anything larger than 64k
//		mcc.setCompressEnable( true );
//		mcc.setCompressThreshold( 64 * 1024 );
	}

	// from here on down, you can call any of the client calls
	public static void main(String[] args) {
        mcc.set( "foo", "This is a test String" );
		String bar = (String) mcc.get( "foo" );
		System.out.println(bar);
	}
}

んでデバッグ実行。

This is a test String

が表示されれば成功っす。

MemCachedClientを使う前にSockIOPoolを(一度だけ)初期化すれば良いってことっすね。

SockIOPoolってのは名前からしてMemCachedClientの内部で使用してるオブジェクトプールかと(commons-poolつかってるしね)。なので、MemCachedClientをガンガン呼んでもSockIOPoolがいい感じに処理してくれるということです。たぶん。

SockIOPoolの設定は設定ファイルに持たせたくなるけど、SockIOPoolのメソッドを見た感じでは見当たらなかったので、必要なら各自作るって感じかな?

逆に初期設定で良いなら(普通は良くないだろうけどw)、これでも動く。

public class MemcachedTest {
	public static void main(String[] args) {
		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers(new String[]{"localhost:11211"});
		pool.initialize();

		MemCachedClient mcc = new MemCachedClient();
		mcc.set( "foo", "This is a test String" );
		String bar = (String) mcc.get( "foo" );
		System.out.println(bar);
	}
}

お手軽っすね。

あとはキャッシュとして使うだけ!

イメージ的にはこんな感じ。

class XxxDao{
	List<XxxDto> getHoge(String id){
		// キャッシュから探す
		List<XxxDto> retval = memcached.get("XxxDao.getHoge:"+id);
		if(retval == null){
			// DBから取得する
			retval = DB.getHoge(id);
			// キャッシュに保存する
			if(retval != null){
				memcached.set("XxxDao.getHoge:"+id, retval);
			}
		}
		return retval;
	}
}

Enjoy! (←これで締めるのカッコイイよね!!)