1. 程式人生 > >redis公共快取的使用

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;
        }
    }

}