工作中的点滴记录:
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;
}
}
}