package com.soybean.message.util;
import com.ctrip.framework.apollo.ConfigService;
import com.soybean.common.utils.basic.CollectionUtil;
import com.soybean.common.utils.basic.JsonUtil;
import com.soybean.common.utils.basic.StringUtil;
import com.soybean.message.constants.MsgCommonConstant;
import com.soybean.message.constants.ServerConstant;
import com.soybean.message.dto.BigdataFilterDTO;
import com.soybean.message.mapper.BigdataFilterDTOMapper;
import com.soybean.message.syntax.EventVersionSyntax;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
public class BigdataBlackListCache<K, V> {
private static final Logger log = LoggerFactory.getLogger(BigdataBlackListCache.class);
@Autowired
private BigdataFilterDTOMapper bigdataFilterDTOMapper;
private volatile Map<K, V> cache;
private final Supplier<Map<K, V>> supplier;
private volatile String version;
private static final ReentrantLock GLOBAL_LOCK = new ReentrantLock();
public BigdataBlackListCache(Supplier<Map<K, V>> supplier) {
if (supplier == null) {
throw new IllegalArgumentException("缓存提供器不能为空");
}
this.supplier = supplier;
this.cache = this.supplier.get();
this.version = ConfigService.getConfig(MsgCommonConstant.TECH_COLLECTRREFRESH).getProperty(MsgCommonConstant.COLLECT_VERSION, "").toString();
}
public V get(K key) {
String aplloVersion = ConfigService.getConfig(MsgCommonConstant.TECH_COLLECTRREFRESH).getProperty(MsgCommonConstant.COLLECT_VERSION, "").toString();
if (aplloVersion.equals(this.version)) {
return cache.get(key);
} else {
GLOBAL_LOCK.lock();
try {
aplloVersion = ConfigService.getConfig(MsgCommonConstant.TECH_COLLECTRREFRESH).getProperty(MsgCommonConstant.COLLECT_VERSION, "").toString();
if (aplloVersion.equals(this.version)) {
return cache.get(key);
}
this.cache = (Map<K, V>) bigdataBlackListRefresh();
this.version = ConfigService.getConfig(MsgCommonConstant.TECH_COLLECTRREFRESH).getProperty(MsgCommonConstant.COLLECT_VERSION, "").toString();
log.info("bigdataBlackList version:{}", this.version);
String toJSON = JsonUtil.toJSON(this.cache);
log.info("bigdataBlackList cache:{}", toJSON);
return cache.get(key);
} catch (Exception e) {
log.error("get function error", e);
return null;
} finally {
GLOBAL_LOCK.unlock();
}
}
}
public Map<String, String> bigdataBlackListRefresh() {
Map<String, String> map = new HashMap<>(1024);
List<BigdataFilterDTO> bigdataFilterDTOS = bigdataFilterDTOMapper.selectAll();
EventVersionSyntax instance = EventVersionSyntax.getInstance();
if (!CollectionUtil.isNullOrEmpty(bigdataFilterDTOS)) {
for (BigdataFilterDTO dto : bigdataFilterDTOS) {
instance.mix(dto.getEventName(), dto.getExpression(), dto.getVersion());
}
}
String mixSyntaxString = instance.mixSyntaxString();
if (!StringUtil.isNullOrEmpty(mixSyntaxString)) {
map.put(ServerConstant.BIGDATA_BLACK_LIST, mixSyntaxString);
}
return map;
}
}