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.Base4jCacheManager Maven / Gradle / Ivy
package com.base4j.cache;
import java.util.Collection;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import com.base4j.cache.utils.CacheCustoms;
import com.base4j.cache.utils.StrExtUtils;
/**
* 缓存对外开发接口
*
* @author FY
*/
public class Base4jCacheManager extends AbstractCacheAbstractTemplate {
private static final Logger logger = LoggerFactory.getLogger(Base4jCacheManager.class);
@Override
public CacheObject get(String region, String key) {
logger.info("获取缓存:region:" + region + ",key:" + key);
CacheObject co = new CacheObject();
co.setRegion(region);
co.setKey(key);
if (!StrExtUtils.isNullOrEmpty(region) && !StrExtUtils.isNullOrEmpty(key)) {
Object object = factory.getCache(CacheCustoms.CACHE_LV_1, region, true, (CacheExpiredListener) getBroadcastChannel()).gets(key);
// 一级缓存中没有找到,到二级中继续查询
if (object == null && factory.isOpenSecondCache()) {
object = factory.getCache(CacheCustoms.CACHE_LV_2, region, true, null).gets(key);
co.setLv(CacheCustoms.CACHE_LV_2);
} else {
co.setLv(CacheCustoms.CACHE_LV_1);
}
// 缓存数据
if (object != null) {
co.setUp((CacheBox) object);
}
}
return co;
}
@Override
public void set(String region, String key, Object value) {
set(region, key, value, 0);
}
@Override
public void set(String region, String key, Object value, Integer expired) {
logger.info("设置缓存:region:" + region + ",key:" + key + ",value:" + value);
if (!StrExtUtils.isNullOrEmpty(region) && !StrExtUtils.isNullOrEmpty(key)) {
if (value == null) {
// 删除该key下的所有数据
evict(region, key);
} else {
// 1.清除原有的一级缓存数据
if (factory.isUseCluster()) {
sendCmdBroadcast(CacheCustoms.OPT_DELTED_KEY, region, key);
}
// 2.添加缓存数据
CacheBox cb = new CacheBox(value, expired);
factory.getCache(CacheCustoms.CACHE_LV_1, region, true, (CacheExpiredListener) getBroadcastChannel()).put(key, cb);
if (factory.isOpenSecondCache()) {
factory.getCache(CacheCustoms.CACHE_LV_2, region, true, null).put(key, cb);
}
}
}
}
@Override
public void evict(String region, String key) {
logger.info("删除缓存:region:" + region + ",key:" + key);
// 1.删除一级缓存
factory.getCache(CacheCustoms.CACHE_LV_1, region, true, (CacheExpiredListener) getBroadcastChannel()).evict(key);
// 2.删除二级缓存
if (factory.isOpenSecondCache()) {
factory.getCache(CacheCustoms.CACHE_LV_2, region, true, null).evict(key);
}
// 3.广播删除消息
if (factory.isUseCluster()) {
sendCmdBroadcast(CacheCustoms.OPT_DELTED_KEY, region, key);
}
}
@Override
public void batchEvict(String region, List keys) {
logger.info("批量删除缓存:region:" + region + ",key:" + keys.toString());
// 1.删除一级缓存
factory.getCache(CacheCustoms.CACHE_LV_1, region, true, (CacheExpiredListener) getBroadcastChannel()).evict(keys);
// 2.删除二级缓存
if (factory.isOpenSecondCache()) {
factory.getCache(CacheCustoms.CACHE_LV_2, region, true, null).evict(keys);
}
// 3.广播删除消息
if (factory.isUseCluster()) {
sendCmdBroadcast(CacheCustoms.OPT_DELTED_KEY, region, keys);
}
}
@Override
public void clear(String region) {
logger.info("清空缓存:region:" + region);
// 1.清除一级缓存
factory.getCache(CacheCustoms.CACHE_LV_1, region, true, (CacheExpiredListener) getBroadcastChannel()).clear();
// 2.清除二级缓存
if (factory.isOpenSecondCache()) {
factory.getCache(CacheCustoms.CACHE_LV_2, region, true, null).clear();
}
}
@Override
public List keys(String region) {
logger.info("获取缓存key:region:" + region);
return factory.getCache(CacheCustoms.CACHE_LV_1, region, true, (CacheExpiredListener) getBroadcastChannel()).keys();
}
@Override
protected Collection extends Cache> loadCaches() {
// TODO Auto-generated method stub
// EhCacheProvider EhCacheProvider =
// factory.getProvider(CacheCustoms.CACHE_LV_1);
logger.info("jbaseCache======================================");
return null;
}
@Override
public Cache getCache(String region) {
logger.info("获取缓存:region:" + region);
com.base4j.cache.Cache cache = factory.getCache(region, true, (CacheExpiredListener) getBroadcastChannel());
return cache;
}
@Override
public Collection getCacheNames() {
logger.info("获取缓存名称");
return null;
}
}