redis公共缓存的使用

工作中的点滴记录:

1、接口服务:

public interface RedisService {

    public void  setObjToRedis(String redisKey,Object object);

    //加锁
    public boolean tryLock(String redisKey,String requestId);

    //解锁
    public boolean  releaseLock(String redisKey,String requestId);

    //是否给该话题点过赞和踩
    public  boolean hexistTopiclikeAndStep(String redisKey,String requestId);

    //是否给该评论点过赞和踩
    public  boolean hexistCommentLikeAndStep(String redisKey,String requestId);

}

2、接口的实现

package com.jd.cf.onlineJudge.rpc.impl;

import com.alibaba.fastjson.JSON;
import com.jd.cf.cache.R2MCacheService;
import com.jd.cf.onlineJudge.domain.vo.DiscussionRequestVo;
import com.jd.cf.onlineJudge.domain.vo.TopicRequestVo;
import com.jd.cf.onlineJudge.rpc.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author: tom
 * @Description:
 * @Date: Created in 17:06 2018/7/25
 * @Modified By:
 */
@Slf4j
@Service("redisService")
public class RedisServiceImpl implements RedisService {


    @Resource(name = "redisUtils")
    protected R2MCacheService r2MCacheService;
    //缓存时间
    private final int REDISTIME = 60;
    //加锁时间
    private final int lockTime= 30;
    //加锁成功标识
    private static final String LOCK_SUCCESS = "OK";
    //解锁成功标识
    private static final Long RELEASE_SUCCESS = 1L;



    @Override
    public void setObjToRedis(String redisKey,Object object) {
        log.info("保存redis入参:{}",JSON.toJSON(redisKey),JSON.toJSON(object));
        if (StringUtils.isNoneBlank(redisKey) && Objects.nonNull(object)) {
            try {
                String jsonStr = JSON.toJSONString(object);
                r2MCacheService.setex(redisKey, REDISTIME, jsonStr);
            } catch (Exception e) {
                log.error("redis存储信息异常:{}", e);
            }
        }
    }
    /**
     *
     * 
     * @Description:
     * 1、redisKey 标识rediskey
     * 2、lockTime 表示key的有效时间
     * 3、requestId 标识锁的唯一性
     * @param: [redisKey, requestId]
     * @return: boolean
     * @auther:
     * @date: 10:57 2018/7/26 
     *
     */
    @Override
    public boolean tryLock(String redisKey, String requestId) {
        log.info("加锁的入参:{}",redisKey,requestId);
        String result = r2MCacheService.setex(redisKey,lockTime,requestId);
        if (LOCK_SUCCESS.equals(result)) {
            return true;
        }
        return false;
    }
    /**
     *
     * 
     * @Description: 解锁
     * @param: [redisKey, requestId]
     * @return: boolean
     * @auther:
     * @date: 10:58 2018/7/26 
     *
     */
    @Override
    public boolean releaseLock(String redisKey, String requestId) {
        log.info("releaseLock:{}",redisKey,requestId);
        log.info("解锁的Value:{}",r2MCacheService.get(redisKey));
        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
        Object result = r2MCacheService.eval(script,redisKey,requestId);
        if (RELEASE_SUCCESS.equals(result)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean hexistTopiclikeAndStep(String redisKey,String requestId) {
        log.info("hexistTopiclikeAndStep 入参:{}",JSON.toJSONString(redisKey),JSON.toJSON(requestId));
        try {
            String topic = r2MCacheService.get(redisKey);
            if(StringUtils.isBlank(topic)||topic.equals("1")|| topic.equals("nil")){
                return true;
            }
            TopicRequestVo topicRequestVo = JSON.parseObject(topic, TopicRequestVo.class);
            if(topicRequestVo.getOperationType().equals(Integer.valueOf(requestId))){
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("hexistKey异常:{}",e);
            return false;
        }
    }


    @Override
        public boolean hexistCommentLikeAndStep(String redisKey, String requestId) {
        log.info("hexistCommentLikeAndStep 入参:{}",JSON.toJSONString(redisKey),JSON.toJSON(requestId));
        try {
            String comment = r2MCacheService.get(redisKey);
            if(StringUtils.isBlank(comment)||comment.equals("1")|| comment.equals("nil") ){
                return true;
            }
            DiscussionRequestVo discussionRequestVo = JSON.parseObject(comment, DiscussionRequestVo.class);
            if(discussionRequestVo.getOperationType().equals(Integer.valueOf(requestId))){
                return false;
            }
            return true;
        } catch (Exception e) {
            log.error("hexistKey异常:{}",e);
            return false;
        }
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值