使用 memcache java 客户端示例

使用 memcache java 客户端示例
原文出处:http://blog.chenlb.com/2008/12/use-memcache-java-client-demo.html
具说大数据量,用这个不错。
memcache java 客户端 api。地址:http://www.danga.com/memcached/apis.bml ,有种客户端,我这使用 Dustin Sallings 版的(已经在 google code 了 http://code.google.com/p/spymemcached/)。

spymemcached 的下载地址:http://spymemcached.googlecode.com/files/memcached-2.2.jar 它依赖 spy.jar,spy.jar的下载地址:http://bleu.west.spy.net/~dustin/repo/spy/jars/spy-2.4.jar

把两个jar放到classpath下,然后写代码试用下:写入

package com.chenlb.use;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.Date;

import net.spy.memcached.MemcachedClient;

public class MemcacheUse {

	private static class MyData implements Serializable {
		private static final long serialVersionUID = 1L;
		private long d = new Date().getTime();
		public String toString() {
			return "my data ["+d+"]";
		}
	}

	public static void main(String[] args) throws IOException {
		MyData myData = new MyData();

		MemcachedClient c=new MemcachedClient(new InetSocketAddress("localhost", 11211));
		// Store a value (async) for one hour

		c.set("someKey", 3600, myData);

		// Retrieve a value (synchronously).
		Object myObject=c.get("someKey");

		c.shutdown();

		System.out.println(myObject);

	}

}

读:主要是验证下,一小时后读是否有效。

package com.chenlb.use;   
  
import java.io.IOException;   
import java.net.InetSocketAddress;   
  
import net.spy.memcached.MemcachedClient;   
  
public class MemcacheGetUse {   
  
    public static void main(String[] args) throws IOException {   
        MemcachedClient c=new MemcachedClient(new InetSocketAddress("172.16.249.220", 11211));   
  
        // Retrieve a value (synchronously).   
        Object myObject=c.get("someKey");   
  
        System.out.println(myObject);   
  
        c.shutdown();   
    }   
  
}  

猜你喜欢

转载自love398146779.iteye.com/blog/1633999
今日推荐