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();
}
}