com.base4j.cache.AbstractCacheAbstractTemplate Maven / Gradle / Ivy
package com.base4j.cache;
import com.base4j.cache.broadcast.JGroupBroadcastChannel;
import com.base4j.cache.broadcast.RedisBroadcastChannel;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.support.AbstractCacheManager;
import java.util.List;
/**
* 缓存方法调用入口
* 注:需设置JVM禁用IPv6服务,编译时添加参数:-Djava.net.preferIPv4Stack=true
*
* @author FY
*/
public abstract class AbstractCacheAbstractTemplate extends AbstractCacheManager implements InitializingBean {
/** 组播通道命名 */
private static String clusterName = "C_DEF";
protected Base4jCacheFactory factory;
private static CacheBroadcastChannel broadcastChannel;
private CacheExpiredListener listener;
private Base4jCacheManager jeezzCacheManager;
// ----------------------- Cache method ----------------------------
public Base4jCacheFactory getFactory() {
return factory;
}
/**
* 缓存中取数据
* @param region 缓存region name
* @param key 缓存业务key
* @return {@link CacheObject}
*/
public abstract CacheObject get(String region, String key);
/**
* 写入缓存 - 不带过期时间
* @param region 缓存region name
* @param key 缓存key
* @param value 缓存数据
*/
public abstract void set(String region, String key, Object value);
/**
* 写入缓存 - 带过期时间;
* 不建议使用该方法,当前缓存的实现架构下,有效期不能同时设置,ehcache在配置文件中,而redis可以在代码上设置,所以不是很合理
* @param region 缓存region name
* @param key 缓存key
* @param value 缓存数据
* @param expired 过期时间
*/
public abstract void set(String region, String key, Object value, Integer expired);
/**
* 删除缓存
* @param region 缓存region name
* @param key 缓存key
*/
public abstract void evict(String region, String key);
/**
* 删除缓存 - 批量
* @param region 缓存region name
* @param keys 缓存key集合
*/
public abstract void batchEvict(String region, List keys);
/**
* 清除缓存
* @param region 缓存region name
*/
public abstract void clear(String region);
/**
* 获得一个region下面所有的cache key
* @param region 缓存region name
* @return {@link List} key list
*/
public abstract List keys(String region);
// ----------------------- Cache Template init method ------------------------------
@Override
public void afterPropertiesSet() {
if (factory == null) {
throw new CacheException("缓存的工厂类实例不能为null.");
}
if (clusterName == null || "".equals(clusterName)) {
AbstractCacheAbstractTemplate.clusterName = "C_DEF";
}
// 初始化组播网络配置,是否开启了集群配置
if (factory.isUseCluster()) {
if (factory.getCacheBroadcast().equals(com.base4j.cache.broadcast.BroadcastType.JGROUPS_MULTICAST)) {
try {
broadcastChannel = new JGroupBroadcastChannel(factory.getcacheJgroupConfFile(), clusterName, factory);
} catch (Exception e) {
e.printStackTrace();
}
} else if (factory.getCacheBroadcast().equals(com.base4j.cache.broadcast.BroadcastType.REDIS_PUBSUB)) {
broadcastChannel = new RedisBroadcastChannel(clusterName, factory);
}
}
}
public void sendCmdBroadcast(byte optKey, String region, Object key) throws CacheException {
if (broadcastChannel != null) {
broadcastChannel.sendCmdBroadcast(optKey, region, key);
}
}
// ------------------------ getter/setter ---------------------------
public void setFactory(Base4jCacheFactory factory) {
this.factory = factory;
}
public CacheBroadcastChannel getBroadcastChannel() {
return broadcastChannel;
}
}