日期:2014-05-17  浏览次数:20874 次

windows 上memcached初步使用-java版

参考:http://q.sohu.com/forum/5/topic/640252

?????????? http://danga.com/memcached/

?????????? http://code.jellycan.com/memcached/

1.下载: memcached的Windows版本和java客户端 (从http://code.jellycan.com/memcached/下载Older Version)

????????? memcached-1.2.1-win32.zip和java_memcached-release_1.6.zip

?2.?解压:memcached-1.2.1-win32.zip 后 进入memcached.exe目录 ,运行下面命令安装启动memcached服务

?? eg:

?????? c:>memcached.exe -d install
???????c:>memcached.exe -l 127.0.0.1 -m 32 -d start

?? 执行完后,任务管理器会出现memcached进程

??? 解压java_memcached-release_1.6.zip 将java_memcached-release_1.6.jar加入java项目的库中。

?

3. 一个简单的应用例子:

package com.cn.xiao.learn.memcached;

import java.util.Date;

import com.danga.MemCached.MemCachedClient;

import com.danga.MemCached.SockIOPool;

public class MemCachedTest {

	// 创建全局的唯一实例
	protected static MemCachedClient mcc = new MemCachedClient();
	protected static MemCachedTest MemCachedTest = new MemCachedTest();
	// 设置与缓存服务器的连接池

	static {

		// 服务器列表和其权重
		String[] servers = {"127.0.0.1:11211" };
		Integer[] weights = { 3 };
		// 获取socke连接池的实例对象
		SockIOPool pool = SockIOPool.getInstance();
		// 设置服务器信息
		pool.setServers(servers);
		pool.setWeights(weights);
		// 设置初始连接数、最小和最大连接数以及最大处理时间
		pool.setInitConn(5);
		pool.setMinConn(5);
		pool.setMaxConn(250);
		pool.setMaxIdle(1000 * 60 * 60 * 6);
		// 设置主线程的睡眠时间
		pool.setMaintSleep(30);

		// 设置TCP的参数,连接超时等
		pool.setNagle(false);
		pool.setSocketTO(3000);
		pool.setSocketConnectTO(0);

		// 初始化连接池
		pool.initialize();
		// 压缩设置,超过指定大小(单位为K)的数据都会被压缩

		mcc.setCompressEnable(true);
		mcc.setCompressThreshold(64 * 1024);

	}

	/**
	 * 
	 * 保护型构造方法,不允许实例化!
	 * 
	 * 
	 */
	protected MemCachedTest() {
	}

	/**
	 * 
	 * 获取唯一实例.
	 * 
	 * @return
	 */

	public static MemCachedTest getInstance() {
		return MemCachedTest;
	}

	/**
	 * 
	 * 添加一个指定的值到缓存中.
	 * 
	 * @param key
	 * 
	 * @param value
	 * 
	 * @return
	 */
	public boolean add(String key, Object value)
	{
		return mcc.add(key, value);
	}

	public boolean add(String key, Object value, Date expiry) {
		return mcc.add(key, value, expiry);
	}

	public boolean replace(String key, Object value) {
		return mcc.replace(key, value);
	}

	public boolean replace(String key, Object value, Date expiry) {
		return mcc.replace(key, value, expiry);
	}

	/**
	 * 
	 * 根据指定的关键字获取对象.
	 * 
	 * @param key
	 * 
	 * @return
	 */
	public Object get(String key) {
		return mcc.get(key);
	}

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		MemCachedTest cache = MemCachedTest.getInstance();
		cache.add("hello1", 234);
		System.out.print("hello : " + cache.get("hello1"));
		TBean tb = new TBean();
		tb.setName("name");
		cache.add("bean", tb);
		TBean tb1 = (TBean) cache.get("bean");
		System.out.print("name=" + tb1.getName());
		tb1.setName("name2");
		tb.setName("name3");
		// cache.replace("bean", tb);
		System.out.println("tb1:name=" + tb1.getName());
		System.out.println("tb1:name=" + tb.getName());
		System.out.println("tb1:name=" + ((TBean) cache.get("bean")).getName());
	}
}

class TBean implements java.io.Serializable {

	private static final long serialVersionUID = 1945562032261336919L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

?

转自:http://blog.csdn.net/taxuexunmei414923794/article/details/6590435