Controller
import cn.gzccc.wx.utils.RedisKeyUtils;
import cn.gzccc.wx.utils.Result;
import cn.hutool.core.convert.Convert;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/wx/api/news_like")
public class RedisLikeController {
@Autowired
private RedisTemplate redisTemplate;
/**
* redis 点赞和取消点赞
*
* @param newsId 文章id
* @param userId 用户id
* @return
*/
@PostMapping("/spot")
public Result saveLiked2Redis(Integer newsId, Integer userId){
if (ObjectUtils.isEmpty(newsId) && ObjectUtils.isEmpty(userId)){
return Result.fail("参数传入失败");
}
String key = RedisKeyUtils.getLikedKey(newsId, userId);
//hasKey()非空返回true进入方法 空返回false走else
if (redisTemplate.opsForHash().hasKey(RedisKeyUtils.MAP_KEY_USER_LIKED,key)){
if (Integer.parseInt(Convert.toStr(redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED,key)))==1){
// 取消点赞
redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, 0);
// 点赞数-1
redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, newsId.toString(), -1L);
}else if (Integer.parseInt(Convert.toStr(redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED,key)))==0) {
// 点赞
redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, 1);
// 点赞数+1
redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, newsId.toString(), 1L);
}
}else {
// 点赞
redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, 1);
// 点赞数+1
redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, newsId.toString(), 1L);
}
int status = Integer.parseInt(Convert.toStr(redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_USER_LIKED, key)));
return Result.success(status);
}
/**
* redis 文章阅读量+1
*
* @param newsId 文章id
* @return
*/
@PostMapping("/readAdd")
public Result incrementLikedCount(Integer newsId){
// 文章阅读量+1
redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_USER_NEWS_READING_QUANTITY, newsId.toString(), 1);
return Result.success("操作成功");
}
}
RedisKeyUtils
/**
* @Description: (用于根据一定规则生成 key)
*/
public class RedisKeyUtils {
/**
* 保存用户点赞数据的key
*/
public static final String MAP_KEY_USER_LIKED = "MAP_USER_LIKED";
/**
* 保存文章被点赞数量的key
*/
public static final String MAP_KEY_USER_LIKED_COUNT = "MAP_USER_LIKED_COUNT";
/**
* 文章阅读量key
*/
public static final String MAP_USER_NEWS_READING_QUANTITY = "MAP_USER_NEWS_READING_QUANTITY";
/**
* 拼接被点赞的文章id和点赞的人的id作为key。格式 222222::333333
* @param userlike 被点赞的文章id
* @param uid 点赞的人的id
* @return
*/
public static String getLikedKey(Integer userlike, Integer uid){
StringBuilder builder = new StringBuilder();
builder.append(userlike);
builder.append("::");
builder.append(uid);
return builder.toString();
}
}
Result
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
@Data
public class Result<T> extends HashMap<String, Object> implements Serializable {
//200是正常,非200表示异常
private int code;
private String msg;
private T data;
public Result() {
}
public void setCode(int code) {
this.code = code;
this.put("code", this.code);
}
public void setMsg(String msg) {
this.msg = msg;
this.put("msg", this.msg);
}
public Result(int code, String msg, T data) {
this.setCode(code);
this.setMsg(msg);
this.setData(data);
}
public void setData(T data) {
this.data = data;
this.put("data", this.data);
}
@Override
public Result<T> put(String key, Object value) {
super.put(key,value);
return this;
}
public static Result<Object> success(int code, String msg, Object data) {
return new Result<Object>(code, msg, data);
}
public static Result<Object> success(Object data) {
return success(200, "请求成功", data);
}
public static Result<Object> success(String data) {
return success(200, "ok", data);
}
public static Result<Object> fail(int code, String msg, Object data) {
return new Result<Object>(code, msg, data);
}
public static Result<Object> fail(String msg, Object data) {
return fail(400, msg, data);
}
public static Result<Object> fail(int code, String msg) {
return fail(code, msg, null);
}
public static Result<Object> fail(String data) {
return fail(400, "error", data);
}
}
入库数据
微信小程序