简单且实用的本地缓存(国庆day2)

1 整体流程

在这里插入图片描述

2 代码实现

2.1 查询的整体实现代码

    /**
     * 获取记录等级列表
     *
     * @param businessId 租户id
     * @return List<RecordLevelParam>
     */
    @Transactional(rollbackFor = Exception.class)
    public List<RecordLevelParam> getRecordLevelList(String businessId) {
        synchronized (lock) {
            //1 该租户首次查询,初始化本地缓存
            if (!localStorage.containsKey(businessId)) {
                initialRecordLevelCache(businessId);
            }
            try {
                boolean isUpdate = checkUpdate(businessId);
                if (isUpdate) {
                    updateRecordLevelCache(businessId);
                }

            } catch (Exception e) {

            }
            Map<Integer, RecordLevelParam> recordLevelParamMap = localStorage.get(businessId);
            List<RecordLevelParam> recordLevelParams = new ArrayList<>();
            for (Map.Entry<Integer, RecordLevelParam> entity : recordLevelParamMap.entrySet()) {
                recordLevelParams.add(entity.getValue());
            }
            return recordLevelParams;
        }
    }

2.1.1 初始化缓存

 /**
     * 初始化等级缓存
     *
     * @param businessId 租户id
     */
    private void initialRecordLevelCache(String businessId) {
        synchronized (lock) {
            //1 从数据中查询等级信息
            List<RecordLevelParam> list = prepareRecordLevelParam(businessId);
            Map<Integer, RecordLevelParam> businessMap = new HashMap<>(list.size());
            //2 将等级信息保存到本地
            if (list != null && list.size() > 0) {
                list.forEach(recordLevelParam -> businessMap.put(recordLevelParam.getLevel(), recordLevelParam));
                localStorage.put(businessId, businessMap);
            } else {
                localStorage.put(businessId, new HashMap<>());
            }
            //3 获取当前租户修改等级的时间戳
            Long timeStamp = (Long) redisManager.hget(RECORD_LEVEL_REDIS_HASH_KEY, businessId);
            Long now = System.currentTimeMillis();
            if (null == timeStamp) {
                redisManager.hset(RECORD_LEVEL_REDIS_HASH_KEY, businessId, now);
                RECORD_LEVEL_CACHE_STAMP.put(businessId, now);
            } else {
                RECORD_LEVEL_CACHE_STAMP.put(businessId, timeStamp);
            }
        }
    }

2.1.2 是否更新本地缓存

   /**
     * 是否更新本地缓存
     *
     * @param businessId 租户id
     * @throws Exception 异常
     */
    private boolean checkUpdate(String businessId) throws Exception {
        Long redisStamp = (Long) redisManager.hget(RECORD_LEVEL_REDIS_HASH_KEY, businessId);
        Long cashStamp = RECORD_LEVEL_CACHE_STAMP.get(businessId);
        if (null == cashStamp) {
            throw new Exception("unfind: " + businessId);
        }
        //redis 时间戳大,数据新,需要更新内存
        return redisStamp > cashStamp;
    }

2.1.3 从缓存中获取数据

 Map<Integer, RecordLevelParam> recordLevelParamMap = localStorage.get(businessId);
            List<RecordLevelParam> recordLevelParams = new ArrayList<>();
            for (Map.Entry<Integer, RecordLevelParam> entity : recordLevelParamMap.entrySet()) {
                recordLevelParams.add(entity.getValue());
            }

2.2 修改的整体实现代码

  @Transactional(rollbackFor = Exception.class)
    public void updateBusinessLevel(String businessId, Integer recordLevel) throws Exception {

        // 更新缓存信息
        if (localStorage.containsKey(businessId)) {
            int level = recordLevel;
            localStorage.get(businessId).put(level, getRecordLevelByBusinessId(businessId, level));
            Long now = System.currentTimeMillis();
            redisManager.hset(RECORD_LEVEL_REDIS_HASH_KEY, businessId, now);
            RECORD_LEVEL_CACHE_STAMP.put(businessId, now);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值