トップ «前の日記(2009-02-07) 最新 次の日記(2009-02-12)» 編集

Ussy Diary


2009-02-10

[Java]memcached によるプロセス間通信

ベンチの取り方に誤りがあったので修正。

前回は RMI でアクセスし思ったようなパフォーマンスを得られなかったので、どうしようかなあと考え memcached があるじゃないかということで計測してみた。

spymemcached ライブラリを使いコードは以下の簡単なもので試した。

public class MemcachedApplication {

    public static void main(String[] args) throws IOException {
       MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));

       long start = System.currentTimeMillis();
       c.set("someKey", 3600, (Object) new Hoge());
       System.out.println(System.currentTimeMillis() - start);

       start = System.currentTimeMillis();
       Hoge myObject = (Hoge) c.get("someKey");
       System.out.println(System.currentTimeMillis() - start);

       System.out.println("age:" + myObject.age);

       start = System.currentTimeMillis();
       c.set("someKey", 3600, (Object) new Hoge());
       System.out.println(System.currentTimeMillis() - start);

       c.shutdown();
    }

    public static class Hoge implements Serializable {
        public int age = 100;
    }
}

実行結果

199
15
age:100
0

初回接続に時間がかかるだけで、 get/set とも 15 ms ぐらいですむので実用できそう。シンプルなプロトコルでやりとりする memcached が使われる理由が分かった。どうでもいいけどブロック (delegate) 使って時間計測したい。

参考

Examples - spymemcached - Example usage - Google Code