缓存刷新类
Posted 你个佬六
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了缓存刷新类相关的知识,希望对你有一定的参考价值。
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;
/**
* 本地缓存通用类,支持定时刷新缓存
* <p>
* 我们采用刷新缓存时生成新的 map 对象的方式,不对 map 进行写操作,避免线程安全问题
*
* @author wanglei
* @date 2019/3/12
*/
public class BigdataBlackListCache<K, V>
private static final Logger log = LoggerFactory.getLogger(BigdataBlackListCache.class);
@Autowired
private BigdataFilterDTOMapper bigdataFilterDTOMapper;
/**
* 添加 volatile 关键字保证并发场景下的可见性
*/
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();
/**
* @param supplier 缓存提供器,通过这个接口获取缓存 map
*/
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();
/**
* 获取缓存,注意处理 null 值
*
* @param key 键
* @return 缓存的值,注意处理 null 值
*/
public V get(K key)
//比较version
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
//获取到锁后再比较一次版本,应对多个线程同时请求,其中一个线程获取信息,其他线程全部在等待,当后续线程获取锁后,
//就无须去redis中获取信息了,因为此处内存中可能已经有了
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;
以上是关于缓存刷新类的主要内容,如果未能解决你的问题,请参考以下文章