com.base4j.cache.Base4jCacheFactory Maven / Gradle / Ivy
package com.base4j.cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import com.base4j.cache.broadcast.BroadcastType;
import com.base4j.cache.store.SuperCacheProvider;
import com.base4j.cache.utils.CacheCustoms;
/**
* @author hj
*/
public class Base4jCacheFactory implements FactoryBean, InitializingBean, DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(Base4jCacheFactory.class);
/** 配置文件:集群环境的组播同步网络配置文件 XML */
private String cacheJgroupConfFileJar = "cache/jgourps.xml";
/** 配置文件:ehcache XML */
private String cacheEhcacheConfFileJar = "cache/ehcache.xml";
/** 配置文件:redis */
private String cacheRedisConfFileJar = "cache/redis.properties";
/** 是否开启了集群配置,默认是开启的 */
private boolean useCluster = true;
// 是否开启二级缓存:redis,默认关闭,仅使用ehcache实现
private boolean openSecondCache = false;
// 缓存广播类型,在集群模式先必须开启;若开启了二级缓存,请确定广播模式
private BroadcastType cacheBroadcast;
private CacheProvider provider_lv1;
private CacheProvider provider_lv2;
private CacheProvider provider;
private String region;
/**
* 模板类调用,根据Cache LV key获取缓存的provider
* @param lvKey 缓存等级,1-一级缓存,2-二级缓存
* @return {@link CacheProvider}
*/
public CacheProvider getProvider(byte lvKey) {
switch (lvKey) {
case CacheCustoms.CACHE_LV_1:
return provider_lv1;
case CacheCustoms.CACHE_LV_2:
return provider_lv2;
default:
return provider_lv1;
}
}
public CacheProvider getProvider() {
return provider;
}
/**
* 获得缓存的操作接口实例
* @param lvKey 缓存等级,1-一级缓存,2-二级缓存
* @param regionName 缓存的region name
* @param autoCreate 是否为自动构建
* @param listener 缓存事件监听器
* @return {@link Cache}
*/
public Cache getCache(byte lvKey, String regionName, boolean autoCreate, CacheExpiredListener listener) {
return getProvider(lvKey).buildCache(regionName, autoCreate, listener);
}
public Cache getCache(String regionName, boolean autoCreate, CacheExpiredListener listener) {
return provider.buildCache(regionName, autoCreate, listener);
}
@Override
public void afterPropertiesSet() throws Exception {
if (openSecondCache) {
Assert.notNull(cacheRedisConfFileJar, "二级缓存已开启,其配置文件路径不能为 null.");
}
if (useCluster) {
if (cacheBroadcast == null) {
throw new IllegalArgumentException("集群模式下,必须设置缓存的 broadcast 类型");
}
switch (cacheBroadcast) {
case JGROUPS_MULTICAST: {
Assert.notNull(cacheJgroupConfFileJar, "组播的网络配置文件路径不能为 null.");
break;
}
case REDIS_PUBSUB: {
logger.info("使用redis进行缓存广播");
if (!openSecondCache) {
throw new IllegalArgumentException("未开启二级缓存实现,不支持该 broadcast 类型");
}
break;
}
default:
throw new IllegalArgumentException("不支持的广播类型 : " + cacheBroadcast);
}
}
Assert.notNull(cacheEhcacheConfFileJar, "一级缓存ehcache的配置文件路径不能为 null.");
// 做初始化
// 1.初始化一级 cache 的 provider
// this.provider_lv1 = new EhCacheProvider();
// this.provider_lv1.start(cacheEhcacheConfFileJar);
// 2.初始化二级cache 的 provider
// if (openSecondCache) {
// this.provider_lv2 = new RedisCacheProvider();
// this.provider_lv2.start(cacheRedisConfFileJar);
// }
this.provider = new SuperCacheProvider();
this.provider.start(cacheEhcacheConfFileJar,cacheRedisConfFileJar);
// 3.初始化工作完成
logger.info("缓存初始化完成.");
}
// ---------------- getter/setter ---------------------
public String getcacheJgroupConfFile() {
return cacheJgroupConfFileJar;
}
public void setcacheJgroupConfFile(String cacheJgroupConfFile) {
this.cacheJgroupConfFileJar = cacheJgroupConfFile;
}
public String getcacheEhcacheConfFile() {
return cacheEhcacheConfFileJar;
}
public void setcacheEhcacheConfFile(String cacheEhcacheConfFile) {
this.cacheEhcacheConfFileJar = cacheEhcacheConfFile;
}
public String getcacheRedisConfFile() {
return cacheRedisConfFileJar;
}
public void setcacheRedisConfFile(String cacheRedisConfFile) {
this.cacheRedisConfFileJar = cacheRedisConfFile;
}
public boolean isOpenSecondCache() {
return openSecondCache;
}
public void setOpenSecondCache(boolean openSecondCache) {
this.openSecondCache = openSecondCache;
}
public BroadcastType getCacheBroadcast() {
return cacheBroadcast;
}
public void setCacheBroadcast(BroadcastType cacheBroadcast) {
this.cacheBroadcast = cacheBroadcast;
}
public boolean isUseCluster() {
return useCluster;
}
public void setUseCluster(boolean useCluster) {
this.useCluster = useCluster;
}
@Override
public void destroy() throws Exception {
// logger.info("哈哈:destroy");
}
@Override
public Base4jCacheFactory getObject() throws Exception {
// TODO Auto-generated method stub
// logger.info("哈哈:getObject");
return this;
}
@Override
public Class extends Base4jCacheFactory> getObjectType() {
// TODO Auto-generated method stub
return (this != null ? this.getClass() : Base4jCacheFactory.class);
}
@Override
public boolean isSingleton() {
// TODO Auto-generated method stub
// logger.info("哈哈:isSingleton");
return true;
}
}