Golang using Redis to vote on articles

Source: Internet
Author: User

Recently Learning Redis, I am a rookie. The code is process-based and does not consider optimization. It's just a piece of paper. It feels like Redis is still strong.

Rules

    1. Score = support votes * (86400/200) + time stamp, article obtained for 200 votes can be displayed on the first page of the day, with the passage of time, when the score is less than the current time stamp, from the home page.
    2. Use a hash to record the title, link, author, article release time, and number of votes that the article gets.
    3. Use two ordered collections to store the article in an orderly fashion: the first ordered set of members is the article ID, the score is the publication time of the article; the second ordered set of members is also the article ID, and the score is the rating of the article. Through these two ordered collections, the site can be published according to the time to display the article, The article can also be displayed according to the height of the article rating.
    4. To prevent users from making multiple polls of the same article, the site needs to record a list of voting users for each article, and for that reason, the program will create a collection for each article and use this collection to record the IDs of all voting users.
    5. In order to save memory, an article will not be able to vote on it after a week has been published, the score will be fixed, and the collection of the user list that recorded the vote will be deleted.

Publish an article

//创建文章func CreateArticle(conn redis.Conn, title, content, link string, userId int) (err error) {    articleId, err := redis.Int(conn.Do("incr", "articleid:")) //生成文章ID    if nil != err {        return    }    now := time.Now().Unix()    _, err = conn.Do( //将文章详情存入redis中        "hmset",        "article:"+strconv.Itoa(articleId),        "title", title,        "content", content,        "link", link,        "votes", 1,        "author", userId,        "time", now,    )    if nil != err {        return    }    _, err = conn.Do("sadd", "voted:"+strconv.Itoa(articleId), userId) //将已投票用户存入集合中    if nil != err {        return    }    exTime := time.Now().Unix() + 7*86400    _, err = conn.Do("expireAt", "voted:"+strconv.Itoa(articleId), exTime) //记录过期时间为一周    if nil != err {        return    }    _, err = conn.Do("zadd", "score:", now+VOTE, articleId) //记录文章分值    if nil != err {        return    }    return nil}

Vote on an article

//对文章进行投票func VoteArticle(conn redis.Conn, articleId, userId int) (err error) {    cutoff := time.Now().Unix() - 7*86400    reply, err := redis.Int64(conn.Do("hget", "article:"+strconv.Itoa(articleId), "time"))    if nil != err {        return    }    if reply < cutoff { //验证投票是否截止        fmt.Println("投票已截止")        return    }    bool, err := redis.Bool(conn.Do("sismember", "voted:"+strconv.Itoa(articleId), userId)) //检查是否已投过票    if nil != err {        return    }    if bool {        fmt.Println("你已投过票")        return    }    _, err = conn.Do("sadd", "voted:"+strconv.Itoa(articleId), userId) //将投票人ID加入集合中    if nil != err {        return    }    _, err = conn.Do("hincrby", "article:"+strconv.Itoa(articleId), "votes", 1) //文章得票 +1    if nil != err {        return    }    _, err = conn.Do("zincrby", "score:", VOTE, articleId)        //增加文章得分    if nil != err {        return    }    fmt.Println(11)    return nil}

Get articles by score high or low

//设置分页const (    ARTICLES_PER_PAGE = 2)//获取文章func GetArticles(conn redis.Conn, page int) (ids []int, err error) {    start := (page - 1) * ARTICLES_PER_PAGE    end := start + ARTICLES_PER_PAGE - 1    ids, err = redis.Ints(conn.Do("zrevrange", "score:", start, end)) //获取文章排名,分数由高到低    if nil != err {        return    }    return ids, nil}

Main

const (    VOTE = 86400 / 200        //分值)func main() {    conn, err := redis.Dial("tcp", ":6379")    if nil != err {        fmt.Println(err)        os.Exit(1)    }    //创建文章    err = CreateArticle(conn, "第一篇", "内容1", "www.baidu.com", 1)     if nil != err {        fmt.Println(err)    }        //对文章进行投票    err = VoteArticle(conn, 1, 2)    if nil != err {        fmt.Println(err)    }    //获取分数最高的文章,并进行分页    ids, err := GetArticles(conn, 1)    if nil !=err {        fmt.Println(err)    }    fmt.Println(ids)}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.