首页 > 代码库 > Memcachedclientutils类

Memcachedclientutils类

0.个人标签

我的CSDN博客地址: http://blog.csdn.net/caicongyang 

1.依赖的jar包

commons-pool-1.5.6.jar

slf4j-api-1.6.1.jar

java_memcached-release_2.6.6.jar

slf4j-simple-1.6.1.jar

2.DangaMemCacheClient.java

package com.ccy.comm.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

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

/**
 * <p> 
 * Title: DangaMemCacheClient.java 
 * Package  
 * </p>
 * <p>
 * Description: MemCacheClient Utils类 
 * <p>
 * @author Tom.Cai
 * @created 2015-7-16 下午3:49:24 
 * @version V1.0 
 *
 */
public class DangaMemCacheClient {
	private static DangaMemCacheClient instance = null;
	private static ReentrantLock lock = new ReentrantLock();
	private MemCachedClient mc;
	private static int expTime = 3600 * 24 * 30;
	private static String address="192.168.253.132";
	private static String port="12000";

	public static DangaMemCacheClient getInstance() {
		if (instance == null) {
			lock.lock();
			try {
				if (instance == null) {
					instance = new DangaMemCacheClient();
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}
		return instance;
	}

	private DangaMemCacheClient() {
		try {
			mc = new MemCachedClient();
			String[] servers = { address+":"+port };
			Integer[] weights = { 10 };

			SockIOPool pool = SockIOPool.getInstance();

			pool.setServers(servers);
			pool.setWeights(weights);

			pool.setInitConn(5);
			pool.setMinConn(5);
			pool.setMaxConn(250);
			pool.setMaxIdle(expTime);

			pool.setMaintSleep(30);

			pool.setNagle(false);
			pool.setSocketTO(30);
			pool.setSocketConnectTO(0);

			pool.initialize();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object getObject(String key) {
		return mc.get(key);
	}

	public boolean setObject(String key, Object value) {
		return mc.set(key, value);
	}

	public boolean setObject(String key, Object value, long time) {
		return mc.set(key, value, new Date(time));
	}

	public boolean removeObject(String key) {
		return mc.delete(key);
	}
	
	/**
	 * 循环获取全部Memcache中的全部key
	 * @return
	 */
	public List<String> getKeys() {
		List<String> keyList = new ArrayList<String>();
		Map slabs = mc.statsItems();
		Iterator itemsItr = slabs.keySet().iterator();
		while (itemsItr.hasNext()) {
			String serverInfo1 = itemsItr.next().toString();
			// 取得這個server的各種 status [itemname:number:field=value]
			Map itemNames = (Map) slabs.get(serverInfo1);
			Iterator itemNameItr = itemNames.keySet().iterator();
			// 以status key值去迴圈
			while (itemNameItr.hasNext()) {
				String itemName = itemNameItr.next().toString();
				// 拆解status 欄位
				// itemAtt[0] = itemname
				// itemAtt[1] = CacheDump的参數
				// itemAtt[2] = field:number or age
				String[] itemAtt = itemName.split(":");
				if (itemAtt[2].startsWith("number")) {
					Map chcheDump = mc.statsCacheDump(Integer.parseInt(itemAtt[1]), 0);
					Iterator itr = chcheDump.keySet().iterator();
					int i = 0;
					while (itr.hasNext()) {
						String serverInfo2 = itr.next().toString();
						Map items = (Map) chcheDump.get(serverInfo2);
						Iterator keyItr = items.keySet().iterator();
						while (keyItr.hasNext()) {
							String key = keyItr.next().toString();
							String memKey = key;
							i++;
							try {
								key = URLDecoder.decode(key, "UTF-8");
								String value = http://www.mamicode.com/((String) items.get(memKey));>

3.说明

本人将其写成一个工具来适用于仅仅缓存不存数据库的情况,当然能够写一个service在供spring来调用!



我的CSDN博客地址: http://blog.csdn.net/caicongyang 



Memcachedclientutils类