In actual project development, Redis is used to implement articles or comments that cannot be repeated

In actual project development, Redis is used to implement articles or comments that cannot be repeated

Business scene

Likes are ubiquitous in our lives. Like in the circle of friends, like in the QQ space, if you see a good-looking article, you agree to give him a like. If you accidentally click it, you can cancel the like.

Insert picture description here

Insert picture description here

Implementation code

Implementation logic

The article is stored in the data table. The article must have a corresponding id, and the user also has a corresponding id. The article id and the user id form a key, store a value in the change key, and then store it in redis. When the user clicks on the front end When the like button is clicked, we judge whether the user has liked the user by querying whether there is a key-value pair in redis. If the information is queried in redis, it means that the user has already liked the button before. Click it again to cancel the like. That is to delete the corresponding key-value pair in redis, and reduce the number of likes of the changed article by one

Code

Controller layer code

 /*防止重复点赞*/
    /*PUT /comment/thumb/{id}*/
    @RequestMapping(value = "/thumbup/{commentid}",method = RequestMethod.PUT)
    public Result thumbup2(@PathVariable String commentid){
        //把用户点赞信息保存到Redis中
        //每次点赞之前,先查询用户点赞信息
        //如果没有点赞信息,用户可以点赞
        //如果有点赞信息,用户不能重复点赞
        /*模拟获取到了的用户id*/
        String userid="123";
       /* 通过redis查询数据如果redis中查询的到数据则说明已经点赞过了*/
        Object result= redisTemplate.opsForValue().get("thumbup_" + userid + "_" + commentid);
        /*如果result为null则调用commentservice方法来进行点赞*/
        if (result==null){
            commentService.thumbup(commentid);
           /* 保存对应的点赞信息*/
            redisTemplate.opsForValue().set("thumbup_" + userid + "_" + commentid,1);
            return new Result(true, StatusCode.OK,"点赞成功");
        } else {
            /*设置点赞数为0*/
            commentService.canclethumbup(commentid);
          /*如果不为空则则设置为取消点赞,从redis中删除该条记录即可*/
            redisTemplate.delete("thumbup_" + userid + "_" + commentid);
            return new Result(true,StatusCode.OK,"已经取消点赞");
        }
    }

Like and unlike codes in the Service layer

Likes also involve thread safety issues. Here we use mongodb's inc method to solve thread safety issues, because mongodb is thread safe

 public void canclethumbup(String id){
       /*修改条件*/
        Query query = new Query();
        /*即修改_id值为id的文档*/
        query.addCriteria(Criteria.where("_id").is(id));
        /*修改数据*/
        Update update = new Update();
        /*key为对应的文档名称,后者为对应的加减值*/
        update.inc("thumbup",-1);
        /*执行操作*/
        mongoTemplate.updateFirst(query,update,"comment");
    }

    public void thumbup(String id) {
      /*  *//*方法1:缺点不可以解决并发的问题*//*
        Comment comment = commentRepository.findById(id).get();
        *//*修改点赞数*//*
        comment.setThumbup(comment.getThumbup()+1);
        *//*执行操作*//*
        commentRepository.save(comment)*/

        /*方法二执行并发操作*/
        /*修改条件*/
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        /*修改的数据*/
        Update update = new Update();
        /*mongodb支持线程安全所以调用其原装的方法,就不会导致线程不安全的问题*/
        /*使用列值增长*/
        update.inc("thumbup",1);

        //直接修改数据
        //第一个参数是修改的条件
        //第二个参数是修改的数值
        //第三个参数是MongoDB的集合名称
        mongoTemplate.updateFirst(query,update,"comment");

    }

Guess you like

Origin blog.csdn.net/pjh88/article/details/115270990