redis实现文章阅读量...等功能

 

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

 

 

入库数据

 

 

微信小程序

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yixian123.com

谢谢打赏,祝老板心想事成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值