Use redis database to record the read status of articles

design

There are several options for implementing a read feature record

1. Client-side record, use page cookie or mobile client-side cache to record.

2. Background relational database records, use the mysql database to create a read record table for records.

3. Background non-relational database records use redis database records.

Each choice has its own advantages and disadvantages in different situations. The advantage of redis implementation is that it can record across platforms and has high execution efficiency.

 

The read function mainly involves the following data, user id, article id, and read flag. With these data, we can determine which articles the user has read.

First of all, we need to determine which data to use as the key value. In fact, both the user id and the article id can be used as the key value. Now we have to look at the relatively large amount of data. Our project currently has about 100,000 users and about 100 articles. Obviously, it is reasonable to use the article id as the key, otherwise 100,000 pieces of data will be created.

Then let's determine how the value is stored. The article id is used as the key, then we can store the user id in the Set as the value, and the user's id will be added when the user reads the article. Doing so also implicitly stores the read flag.

Also explain why the value is stored in the Set type. The list is repeatable. We must first determine whether it exists before storing it in the database. Such an operation obviously consumes performance, so it is sufficient to use the set as long as it is stored every time.

 

code

The complete test code has been uploaded to the code cloud: https://gitee.com/imlichao/Article-reading-record

This part posts the main implementation code for reference

package pub.lichao.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 使用redis进行文章已读控制
 */
@Controller
public class ArticleController {

    @Autowired JedisUtil jedisUtil;

    public List<Article> articleList = new ArrayList<Article>();

    /**
     * 构造方法初始化一组文章
     * 真实情况一般是从数据库中查询
     */
    public ArticleController(){
        for( int i = 1;i <= 20 ;i++){
            Article article = new Article();
            article.setId(Long.valueOf(i));
            article.setTitle("测试文章" + i );
            articleList.add(article);
        }
    }

    /**
     * 阅读文章
     * @param articleId 文章id
     * @param personId 读者id
     * @param model
     * @return
     */
    @RequestMapping(value = "/readArticle", method = RequestMethod.GET)
    public String readArticle(Long articleId,Long personId,ModelMap model){
        //在redis中增加阅读用户id
        jedisUtil.addSet(0,articleId.toString(),personId.toString());
        return "redirect:articleList?personId="+personId;
    }

    /**
     * 文章列表查询
     * @param personId 读者id
     * @param model
     * @return
     */
    @RequestMapping(value = "/articleList", method = RequestMethod.GET)
    public String articleList(Long personId,ModelMap model){
        if(personId == null){
            return "article";
        }

        List<Article> returnArticleList = new ArrayList<Article>();
        Integer unreadArticleNum = 0; //未读文章计数
        for(Article article:articleList){
            Article returnArticle = new Article();
            returnArticle.setId(article.getId());
            returnArticle.setTitle(article.getTitle());
            returnArticle.setIsRead(article.getIsRead());

            //获取redis存储的已读用户列表
            Set<String> personSet = jedisUtil.getSet(0,article.getId().toString());
            boolean b = personSet.contains(personId.toString());
            if(b == true){
                returnArticle.setIsRead(1); //如果存在则更改已读标志
            }else{
                unreadArticleNum++; //如果不存在未读计数加1
            }
            returnArticleList.add(returnArticle);
        }

        model.addAttribute("returnArticleList",returnArticleList);
        model.addAttribute("unreadArticleNum",unreadArticleNum);
        model.addAttribute("personId",personId);

        return "article";
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "redirect:articleList";
    }

}

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325445069&siteId=291194637
Recommended