⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memcachepool.java

📁 羽量级数据持久层开发框架
💻 JAVA
字号:
package org.speedframework.cache;

/*
 * ******************************************************************
 * Project Name: aspfirst_java
 * MemCache.java 2007-3-12
 * Copyright by Beijing cy2.cn
 * All right reserved.
 * Authou: wangchuyun
 * use server memcached-1.2.1-win3.zip clent java_memcached-release_1.3.2.jar  
 * http://www.whalin.com/memcached/
 * ******************************************************************/


import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

import org.speedframework.exception.SpeedFrameworkException;

// import cn.cy2.resource.UlandGlobals;

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

/**
 * MemCached读取类
 * 
 * @author wangchuyun
 */
public class MemCachePool extends MemCachedClient implements CachePool {

	private SockIOPool pool = null;

	private TreeSet memhost = new TreeSet();

	// 是否将对象直接存储为字符串
	private boolean isPrimitiveAsString = false;

	// 数据是否压缩,默认true
	private boolean isCompressEnable = true;

	// 默认压缩数据的界限,大于此值的数据才压缩
	private int compressThreshold = 15360;
	/**
	 * 构造MemCache类
	 * 
	 * @param host
	 *            memcache主机ip
	 * @param port
	 *            memcache主机端口,默认为11211
	 */
	public MemCachePool(String host, String port) {
		memhost.add(host + ":" + port);
		init();
	}

	/**
	 * 默认构造MemCache类,host默认为本机,port默认为11211
	 */
	public MemCachePool() {
		memhost.add("127.0.0.1:11211");
		init();
	}

	public void addMemcachedHost(String host, String port) {
		memhost.add(host + ":" + port);
	}

	public String[] getMemcachedHost() {
		if (memhost.isEmpty()) {
			return null;
		}
		String[] cacheServers = new String[memhost.size()];
		int i = 0;	
		for (Iterator it = memhost.iterator(); it.hasNext();) {
			cacheServers[i++] = (String) it.next();
		}
		return cacheServers;
	}

	

	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	private synchronized void init() throws SpeedFrameworkException {
		if (memhost.isEmpty()) {
			throw new SpeedFrameworkException("host or port is empty");
		}

		if (pool == null) {

			String[] cacheServers = new String[memhost.size()];
			int i = 0;
		
			for (Iterator it = memhost.iterator(); it.hasNext();) {
				cacheServers[i++] = (String) it.next();
			}
			pool = SockIOPool.getInstance();
			pool.setServers(cacheServers);

			pool.initialize();

			this.setCompressEnable(isCompressEnable);
			if (isCompressEnable) {
				this.setCompressThreshold(compressThreshold);
			}
			this.setPrimitiveAsString(isPrimitiveAsString);// 是否存储简单的字符串,一般与php,asp等进行交换数据时用true

		}
	}	

	public String getStatus() {
		String info = "";
		Map stats = this.stats();
		for (Iterator iter = stats.keySet().iterator(); iter.hasNext();) {
			String key = (String) iter.next();
			info += (key + "=" + stats.get(key)) + ";";
		}
		stats = null;
		return info;

	}

	
	

	public synchronized Object get(Object cacheName) throws SpeedFrameworkException {
		// TODO 自动生成方法存根
		
		return this.get(cacheName);
	}

	public synchronized void put(Object cacheName, Object value) throws SpeedFrameworkException {
		// TODO 自动生成方法存根
		this.put(cacheName,value);
		
	}

	public synchronized void remove(Object string) throws SpeedFrameworkException {
		// TODO 自动生成方法存根
		this.remove(string);
		
	}

	public String getStats() throws SpeedFrameworkException {
		// TODO 自动生成方法存根
		String info = "";
		Map stats = this.stats();
		for (Iterator iter = stats.keySet().iterator(); iter.hasNext();) {
			String key = (String) iter.next();
			info += (key + "=" + stats.get(key)) + ";";
		}
		stats = null;
		return info;
	}

	public void dispose() {
		// TODO 自动生成方法存根
		pool.shutDown();
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -