ITeye博客

java-memcached使用实例

ldz0414 · 2014-05-30 11:06 · java · 缓存相关 · 开源软件 · Markdown


package com.memcache;

import java.util.List;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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

/**
 * 使用memcached的缓存测试类.
 * 
 * @author 阿蜜果
 */
public class MemcachedTest {
	// 创建全局的唯一实例
	protected static MemCachedClient mcc = new MemCachedClient();

	protected static MemcachedTest memCached = new MemcachedTest();

	// 设置与缓存服务器的连接池
	static {
		// 服务器列表和其权重
		String[] servers = { "127.0.0.1:11211" };
		Integer[] weights = { 3 };

		// 获取socket连接池的实例对象
		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 memCached;
	}

	/**
	 * 添加一个指定的值到缓存中.
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return 在缓存中若该key不存在,并成功添加返回true,否则将返回false
	 */
	public boolean add(String key, Object value) {
		return mcc.add(key, value);
	}

	/**
	 * 添加一个键值对到缓存中.
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param expires
	 *            超时时间
	 * @return 在缓存中若该key不存在,并成功添加返回true,否则将返回false
	 */
	public boolean add(String key, Object value, Date expires) {
		return mcc.add(key, value, expires);
	}

	/**
	 * 将某个键的值改变成新值,首先需要保证该键存在.
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @return 成功返回true,失败返回false
	 */
	public boolean replace(String key, Object value) {
		return mcc.replace(key, value);
	}

	/**
	 * 将某个键的值改变成新值,首先需要保证该键存在.
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param expires
	 *            超时时间
	 * @return 成功返回true,失败返回false
	 */
	public boolean replace(String key, Object value, Date expires) {
		return mcc.replace(key, value, expires);
	}

	/**
	 * 添加一个指定的值到缓存中.
	 * 
	 * @param key
	 * @param value
	 * @return 成功返回true,否则返回false
	 */
	public boolean set(String key, Object value) {
		return mcc.set(key, value);
	}

	/**
	 * 添加一个指定的值到缓存中,并设置其超时时间.
	 * 
	 * @param key
	 *            键
	 * @param value
	 *            值
	 * @param expires
	 *            超时时间
	 * @return 成功返回true,否则返回false
	 */
	public boolean set(String key, Object value, int expires) {
		return mcc.set(key, value, expires);
	}

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

	/**
	 * 将指定key的value值+1,将返回最后的value值
	 * 
	 * @param key
	 * @return 返回最后的value值
	 */
	public long incr(String key) {
		return mcc.incr(key);
	}

	/**
	 * 将指定key的value值-1,将返回最后的value值
	 * 
	 * @param key
	 * @return 返回最后的value值
	 */
	public long decr(String key) {
		return mcc.decr(key);
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		MemcachedTest cache = MemcachedTest.getInstance();
		cache.set("count", 1234);
		System.out.println("count=" + cache.get("count"));
		boolean flag = cache.add("schedule_5", "55");
		System.out.println("flag=" + flag);
		System.out.println("schedule_2=" + cache.get("schedule_2"));
		Bean bean = new Bean(1, "bean1", new Date());
		cache.set("bean", bean);
		Bean cachedBean = (Bean) cache.get("bean");
		System.out.println(cachedBean);
		Map map = new HashMap();
		map.put("key1", "value1");
		map.put("key2", "value2");
		cache.set("map", map);
		Map cachedMap = (Map) cache.get("map");
		cachedMap.put("key3", "value3");
		System.out.println(cachedMap);
		List list = new ArrayList();
		list.add(1);
		list.add(2);
		list.add(bean);
		cache.set("list", list);
		List cachedList = (List) cache.get("list");
		System.out.println(cachedList);
	}
}

class Bean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2947079639200259035L;
	int id;
	String name;
	Date date;

	public Bean(int id, String name, Date date) {
		this.id = id;
		this.name = name;
		this.date = date;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String toString() {
		return id + "|" + name + "|" + date.toString();
	}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。