关于我的开源项目RedisFile讲解 二

redis连接池配置池相关包com.ylzinfo.redis.manager

package com.ylzinfo.redis.manager;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;



import com.ylzinfo.redis.storage.CachePool;

import redis.clients.jedis.Jedis;


public class CacheManager
{
	private ConcurrentHashMap<String, CachePool> cachePool = new ConcurrentHashMap<String, CachePool>();
	
	private static CacheManager cacheManagerInstance = new CacheManager();
	
	private Properties cacheProperties;
	
	private boolean isLanuch = false;
	/*
	 * 创建一个单例
	 */
	public static CacheManager getInstance()
	{
		if(cacheManagerInstance == null)
		{
			cacheManagerInstance = new CacheManager();
		}
		return cacheManagerInstance;
	}
	/*
	 * 根据配置创建实例
	 */
	private CacheManager()
	{
		this.cacheProperties = ConfigureManager.getInstance().getCacheConfig();
		launch();
	}
	/*
	 * 加载数据池
	 */

	private void launch()
	{
		if( ! isLanuch)
		{
			
			String needLaunchPoolInstance = cacheProperties.getProperty("instances");
			String[] needLaunchPools = needLaunchPoolInstance.split(",");
			for(String instance : needLaunchPools)
			{
			
				String redisHost = cacheProperties.getProperty(instance + "_host");
				Integer redisPort = Integer.valueOf(cacheProperties.getProperty(instance + "_port"));
				/*
				 * 获取数据库,一个redis默认可以配置16个数据库
				 */
				Integer redisDB = Integer.valueOf(cacheProperties.getProperty(instance + "_db"));
				/*
				 * 加入池
				 */
				CachePool cachePoolInstance = new CachePool(redisHost, redisPort, redisDB);
				cachePoolInstance.launch();
				cachePool.put(instance, cachePoolInstance);
			}
			isLanuch = true;
		}
	}
	
	
	public void shutDown()
	{
		if(this.cachePool.size() > 0)
		{
			Iterator<Entry<String, CachePool>> iterator = this.cachePool.entrySet().iterator();
			while(iterator.hasNext())
			{
				Map.Entry<String, CachePool> hashEntry = iterator.next();
				CachePool poolInstance = hashEntry.getValue();
				poolInstance.destory();
				
				cachePool.remove(hashEntry.getKey());
			}
			
			cachePool.clear();
		}
	}
	
	
	public Jedis getResource(CachePoolName poolName) {
		String strName = poolName.toString();
		
		return getJedisResource(strName);
	}
	

	public void returnResource(CachePoolName poolName,  Jedis jedis) {
		if(jedis == null) {
			return ;
		}
		
		String strName = poolName.toString();
		returnJedisResource(strName, jedis);
	}
	

	private void returnJedisResource(String poolName, Jedis jedisInstance) {
		CachePool pool = cachePool.get(poolName);
		if(pool != null) {
			pool.returnResource(jedisInstance);
		}
	}
	

	private Jedis getJedisResource(String instanceName) {
		Jedis jedisResource = null;
		
		CachePool pool = cachePool.get(instanceName);
		if(pool != null) {
	
			jedisResource = pool.getResource();
			
		}
		return jedisResource;
	}
	

	public enum CachePoolName {
		upload, 
		unread, 
		group_counter
	}
}
package com.ylzinfo.redis.manager;


import java.util.Properties;

import com.ylzinfo.redis.config.Configure;


public class ConfigureManager
{


	private static ConfigureManager _configureManagerInstance = null;
	
	public static ConfigureManager getInstance()
	{
		if(_configureManagerInstance == null)
		{
			_configureManagerInstance = new ConfigureManager();
		}
		return _configureManagerInstance;
	}
	
	private Configure configure;
	
	private ConfigureManager()
	{
		configure = new Configure();
	}
	

	public void initial() throws Exception
	{
		reloadAllConfigs();
	}
	


	public Properties getCacheConfig()
	{
		return configure.getCacheConfig();
	}
	

	public Properties getSystemConfig()
	{
		return configure.getSystemConfig();
	}
	

	

	
	public void loadAllConfigs() throws Exception
	{
		configure.loadConfigs();
	}
	
	
	public void reloadAllConfigs() throws Exception {
		loadAllConfigs();
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值