Redis分布式锁(项目案例)

Redis分布式锁

   /**
     * 检查用户是否符合要求
     *
     * @param openId
     * @return true 符合,false 不符合
     */
    @Override
    public Boolean checkMemberVIPInfoV2(String openId) {
        YfbMembersPO yfbMembersPO = yfbMembersMapper.selectByPrimaryKey(openId);
        if (Objects.isNull(yfbMembersPO)) {
            // 查不到用户不符合要求
            return false;
        }
        if (yfbmembersService.isEnterpriseVip(openId)) {
            // 企业会员不展示
            return false;
        }
        Integer memberId = yfbMembersPO.getId();
        List<RenewalContractPO> renewalContractPOS = renewalContractMapper.listRenewalWithMemberId(memberId, 1);
        if (!CollectionUtils.isEmpty(renewalContractPOS)) {
            // 有连续包月签约信息,不符合要求
            return false;
        }
        // 拼装锁 
        String redisKey = LOCK_PRE + openId;
        Boolean lock = redisTemplate.opsForValue().setIfAbsent(redisKey, "");
        if (Objects.isNull(lock) || !lock) {
            // 未能加锁,说明30分钟内有触发,直接返回
            return false;
        }
        redisTemplate.expire(redisKey, 30, TimeUnit.MINUTES);


        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDate = now.plusDays(14);
        return vipPermissionService.hasPermission(VipTypeEnum.ZB, memberId, PfNoEnum.ALL, now.with(LocalTime.MIN), localDate);
    }

你可能感兴趣的:(redis,分布式,数据库)