缓存刷新类

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;

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你个佬六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值