The post likes the solution

“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

“Like” service scenario analysis

  • Like or unlike, the user clicks the function
  • View post information: View the number of likes on a post and the status of likes by the user ID and post ID.

Technical solution of thumbs up

The key technology of “like” is to judge whether the user “likes” or not. The user who has repeatedly “liked” is not allowed to “like” again, that is, to filter repetition. Although the business is not complex, it can be directly implemented by the database, but for the high concurrency scenario of Weibo, it is impossible to check the database, which is generally cache, that is redis

  1. Like or unlike, the user clicks the function

Key =like:postid value={userid}

Run the sadd command to add a thumbs-up

127.0. 01.:6379> sadd like:1000 101
(integer) 1
127.0. 01.:6379> sadd like:1000 102
(integer) 1
127.0. 01.:6379> sadd like:1000 103
(integer) 1
127.0. 01.:6379> smembers like:1000
1) "101"
2) "102"
3) "103" 
Copy the code

Run the srem command to cancel the like

127.0. 01.:6379> srem like:1000 101
(integer) 1
127.0. 01.:6379> smembers like:1000
1) "102"
2) "103"
Copy the code
  1. View post information: You can view the number of likes on a post and the status of likes by the user ID and post ID.

Using scard command, the total number of likes

127.0. 01.:6379> smembers like:1000
1) "102"
2) "103"
127.0. 01.:6379> scard like:1000
(integer) 2
Copy the code

Run the sismember command to determine whether to press the “like” button

127.0. 01.:6379> smembers like:1000
1) "102"
2) "103"
127.0. 01.:6379> sismember like:1000 102
(integer) 1
127.0. 01.:6379> sismember like:1000 101
(integer) 0
Copy the code

Case combat: SpringBoot+Redis micro blog praise

give a like


    /** ** like */
    @GetMapping(value = "/dolike")
    public String dolike(int postid,int userid) {
        String result="";
        try {
            String key=Constants.LIKE_KEY+postid;
            long object = this.redisTemplate.opsForSet().add(key,userid);
            if (object==1){
                result="Thumbs up!";
            }else{
                result="You've liked it twice.";
            }
            log.info("Query result: {}", object);
        } catch (Exception ex) {
            log.error("exception:", ex);
        }
        return result;
    }
Copy the code

Cancel the thumb up

    /** ** unlikes */
    @GetMapping(value = "/undolike")
    public String undolike(int postid,int userid) {
        String result="";
        try {
            String key=Constants.LIKE_KEY+postid;
            long object = this.redisTemplate.opsForSet().remove(key,userid);
            if (object==1){
                result="Cancelled successfully";
            }else{
                result="You have unliked it twice.";
            }
            log.info("Query result: {}", object);
        } catch (Exception ex) {
            log.error("exception:", ex);
        }
        return result;
    }

Copy the code

The total number of likes and whether a post is liked

    /** * Select * from postid userid; /** * select * from postid userid
    @GetMapping(value = "/getpost")
    public Map getpost(int postid,int userid) {
        Map map=new HashMap();

        String result="";
        try {
            String key=Constants.LIKE_KEY+postid;
            long size = this.redisTemplate.opsForSet().size(key);
            boolean bo=this.redisTemplate.opsForSet().isMember(key,userid);
            map.put("size",size);
            map.put("isLike",bo);
            log.info("Query result: {}", map);
        } catch (Exception ex) {
            log.error("exception:", ex);
        }
        return map;
    }
Copy the code

Thumb up detail

    /** * view the likes list, which people */
    @GetMapping(value = "/likedetail")
    public Set likedetail(int postid) {
        Set set=null;
        try {
            String key=Constants.LIKE_KEY+postid;
            set = this.redisTemplate.opsForSet().members(key);
            log.info("Query result: {}", set);
        } catch (Exception ex) {
            log.error("exception:", ex);
        }
        return set;
    }
Copy the code

Redis distributed cache series

Redis distributed Cache (21) – Interested people, lists, QQ group random display solution – Nuggets (juejin. Cn)