All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.base4j.cache.store.SuperCacheProvider Maven / Gradle / Ivy
package com.base4j.cache.store;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.base4j.cache.CacheException;
import com.base4j.cache.CacheExpiredListener;
import com.base4j.cache.CacheProvider;
import com.base4j.cache.store.ehcache.EhCache;
import com.base4j.cache.store.redis.RedisCache;
import com.base4j.cache.utils.PropertiesLoader;
import com.base4j.cache.utils.StrExtUtils;
import net.sf.ehcache.CacheManager;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author FY
*/
public class SuperCacheProvider implements CacheProvider {
private static final Logger logger = LoggerFactory.getLogger(SuperCacheProvider.class);
/** ehcahce的配置文件路径 */
private String CONFIG_PATH = "/cache/ehcache.xml";
private CacheManager ehCacheManager;
private ConcurrentHashMap cacheInstances = new ConcurrentHashMap();
private static JedisPool pool;
@Override
public String name() {
return "ehcache";
}
@Override
public SuperCache buildCache(String regionName, boolean autoCreate, CacheExpiredListener listener) throws CacheException {
SuperCache baseCache = cacheInstances.get(regionName);
if (baseCache == null && autoCreate) {
baseCache = new SuperCache();
try {
synchronized (this) {
EhCache ehCache = baseCache.getEhCache();
RedisCache redisCache = baseCache.getRedisCache();
if (ehCache == null) {
net.sf.ehcache.Cache cache = ehCacheManager.getCache(regionName);
if (cache == null) {
logger.warn("找不到缓存配置的 {},将使用默认配置", regionName);
ehCacheManager.addCache(regionName);
cache = ehCacheManager.getCache(regionName);
logger.debug("start EhCache region : {}", regionName);
}
ehCache = new EhCache(cache, listener);
baseCache.setEhCache(ehCache);
}
if (redisCache == null) {
redisCache = new RedisCache(regionName);
baseCache.setRedisCache(redisCache);
cacheInstances.put(regionName, baseCache);
}
// baseCache.setJeezzCacheManager(jeezzCacheManager);
// baseCache.setRegionName(regionName);
cacheInstances.put(regionName, baseCache);
}
} catch (net.sf.ehcache.CacheException e) {
throw new CacheException(e);
}
}
return baseCache;
}
@Override
public void start(Properties props) throws CacheException {
if (ehCacheManager != null) {
logger.warn("EhCache 已经启动");
return;
}
URL xml = getClass().getClassLoader().getParent().getResource(CONFIG_PATH);
if (xml == null) {
xml = getClass().getResource(CONFIG_PATH);
}
if (xml == null) {
throw new CacheException(String.format("没有找到EhCache的配置文件 : %s", CONFIG_PATH));
}
// 初始化
ehCacheManager = new CacheManager();
cacheInstances = new ConcurrentHashMap();
}
@Override
public void start(final String confFilePath) throws CacheException {
if (ehCacheManager != null) {
logger.warn("EhCache 已经启动");
return;
}
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(confFilePath);
if (resourceAsStream != null) {
ehCacheManager = new CacheManager(resourceAsStream);
cacheInstances = new ConcurrentHashMap();
logger.info("初始化ehcache的cacheInstances完成-jar");
return;
}
URL xml = getClass().getClassLoader().getParent().getResource(confFilePath);
if (xml == null) {
xml = getClass().getResource(confFilePath);
}
if (xml == null) {
throw new CacheException(String.format("没有找到EhCache的配置文件 : %s", confFilePath));
}
this.CONFIG_PATH = confFilePath;
// 初始化
ehCacheManager = new CacheManager(xml);
cacheInstances = new ConcurrentHashMap();
logger.info("初始化ehcache的cacheInstances完成");
}
@Override
public void stop() {
if (ehCacheManager != null) {
ehCacheManager.shutdown();
ehCacheManager = null;
}
}
@Override
public void start(String confFilePath, String confFileRedisPath) throws CacheException {
if (ehCacheManager != null) {
logger.warn("EhCache 已经启动");
return;
}
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(confFilePath);
if (resourceAsStream != null) {
ehCacheManager = new CacheManager(resourceAsStream);
cacheInstances = new ConcurrentHashMap();
logger.info("初始化ehcache的cacheInstances完成-jar");
}else {
URL xml = getClass().getClassLoader().getParent().getResource(confFilePath);
if (xml == null) {
xml = getClass().getResource(confFilePath);
}
if (xml == null) {
throw new CacheException(String.format("没有找到EhCache的配置文件 : %s", confFilePath));
}
// 初始化
ehCacheManager = new CacheManager(xml);
cacheInstances = new ConcurrentHashMap();
}
this.CONFIG_PATH = confFilePath;
logger.info("初始化ehcache的cacheInstances完成");
if (pool != null) {
return;
}
if (confFileRedisPath == null || "".equals(confFileRedisPath)) {
throw new CacheException(String.format("缓存的Redis配置文件路径错误 : %s", confFileRedisPath));
}
// 载入REDIS配置
Properties props = new PropertiesLoader(confFileRedisPath).getProperties();
try {
if (props.isEmpty()) {
throw new Exception(String.format("读取缓存Redis配置文件错误 : %s", confFileRedisPath));
}
// Redis链接配置
JedisPoolConfig config = new JedisPoolConfig();
String host = getProperty(props, "cache.redis.hostname", "127.0.0.1");
String password = props.getProperty("cache.redis.password", null);
if (StrExtUtils.isNullOrEmpty(password)) {
password = null;
}
int port = getProperty(props, "cache.redis.port", 6379);
int timeout = getProperty(props, "cache.redis.timeout", 2000);
int database = getProperty(props, "cache.redis.database", 0);
pool = new JedisPool(config, host, port, timeout, password, database);
logger.info("初始化redis的pool完成");
// 序列化工具类名
// serializer = props.getProperty("cache.serializer_class",
// SnappySerializer.class.getName());
} catch (Exception e) {
throw new CacheException("创建CacheProvider实例失败.", e);
}
}
private static String getProperty(Properties props, String key, String defaultValue) {
return props.getProperty(key, defaultValue).trim();
}
/**
* 取配置数据 默认为int类型的字段
*
* @param props
* 配置属性
* @param key
* 名称
* @param defaultValue
* 默认值
* @return 对应的值
*/
private static int getProperty(Properties props, String key, int defaultValue) {
try {
return Integer.parseInt(props.getProperty(key, String.valueOf(defaultValue)).trim());
} catch (Exception e) {
return defaultValue;
}
}
/**
* 从池中取出一个连接实例
* @return {Jedis}
*/
public static Jedis getResource() {
logger.info("获取pool的实例");
Jedis resource=null;
try {
resource = pool.getResource();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resource;
}
/**
* 释放资源
* @param jedis jedis instance
*/
public static void returnResource(Jedis jedis) {
if(null == jedis) {
return;
}
pool.returnResourceObject(jedis);
}
}