Redis In Action:文章投票

1. 投票规则

  • 一个用户对一篇文章只能投一票
  • 一篇文章发布 7 天后不能再进行投票
  • 文章得分计算规则:得票数*432

2. 数据结构

image.png
  • 创建有序集合zset存放文章发布时间
127.0.0.1:6379> zadd time: "1583948289.47" "article:redis" 
(integer) 1
127.0.0.1:6379> zadd time: "1583738289.48" "article:python"
(integer) 1
127.0.0.1:6379> zrange time: 0 1 withscores
1) "article:python"
2) "1583738289.48"
3) "article:redis"
4) "1583948289.47"
127.0.0.1:6379> 
  • 创建有序集合zset存放文章分数
127.0.0.1:6379> zadd score: 1000 "article:redis" 
(integer) 1
127.0.0.1:6379> zadd score: 500 "article:python" 
(integer) 1
127.0.0.1:6379> zrange score: 0 1 withscores
1) "article:python"
2) "500"
3) "article:redis"
4) "1000"
  • 创建散列hash存放文章投票总数
127.0.0.1:6379> hset article:redis votes 10
(integer) 1
127.0.0.1:6379> hset article:python votes 5
(integer) 1
127.0.0.1:6379> hget article:redis votes
"10"
127.0.0.1:6379> hget article:python votes
"5"
  • 创建无序集合set存放已投票用户
127.0.0.1:6379> sadd voted:redis "user:chengzw"
(integer) 1
127.0.0.1:6379> sadd voted:python "user:jack"
(integer) 1
127.0.0.1:6379> smembers voted:redis
1) "user:chengzw"
127.0.0.1:6379> smembers voted:python
1) "user:jack"
  • 查看创建后的数据结构


    image.png

3. 文章投票脚本

#coding: utf-8
import time
import redis

ONE_WEEK_IN_SECONDS = 7 * 86400   #投票截止时间为1周
VOTE_SCORE = 432  #每个投票432分

def article_vote(conn, user, article):
    #time.time()可以得出UTC时区1970年1月1日到现在为止经过的秒数
    cutoff = time.time() - ONE_WEEK_IN_SECONDS  # 计算文章的投票截止时间。
    # 检查是否还可以对文章进行投票
    #(虽然使用散列也可以获取文章的发布时间,
    # 但有序集合返回的文章发布时间为浮点数,
    # 可以不进行转换直接使用)。
    if conn.zscore('time:', article) < cutoff:  #当投票的时间减去一周大于文章发布的时间则说明已经超过投票截止日期
        print("投票已经超过截止日期")
        return
    print("投票还未超过截止日期")
    #从article:id标识符(identifier)里面取出文章的ID。
    article_id = article.partition(':')[-1]
    # 如果用户是第一次为这篇文章投票,那么增加这篇文章的投票数量和评分。
    if conn.sadd('voted:' + article_id, user):  #如果这个用户已经在已投票用户中,返回值是"0",例如执行命令: sadd voted:redis user:chengzw
         print("增加文章"+article+"评分:"+"zincrby score: "+str(VOTE_SCORE)+" "+'"'+article+'"')
         conn.zincrby('score:',VOTE_SCORE,article)
         print("增加文章"+article+"投票数量:"+"hincrby "+'"'+article+'"'+" votes 1")
         conn.hincrby(article, 'votes', 1)
    else:
       print("该用户已经为这篇文章投过票了!")
if __name__ == "__main__":
   conn = redis.Redis(password="redis的密码")
   user = input("请输入用户名(格式user:xxxxx):")
   print(user)
   article = input("请输入要投票的文章(article:redis,article:python):")
   print(article)
   article_vote(conn,user,article)

4. 验证

  • 首先尝试对超过投票截止日期的文章"article:python"投票,投票失败
[root@redis python-redis]# python article_vote.py 
请输入用户名(格式user:xxxxx):"user:chengzw"
user:chengzw
请输入要投票的文章(article:redis,article:python):"article:python"
article:python
投票已经超过截止日期

  • 接着尝试用已经对文章"article:redis"投过票的用户"user:chengzw"对"article:python"再次进行投票,投票失败*
[root@redis python-redis]# python article_vote.py 
请输入用户名(格式user:xxxxx):"user:chengzw"
user:chengzw
请输入要投票的文章(article:redis,article:python):"article:redis"
article:redis
投票还未超过截止日期
redis
该用户已经为这篇文章投过票了!
  • 最后使用一个新用户"user:tom"对未超过投票截止日期的文章"article:redis"进行投票,投票成功
[root@redis python-redis]# python article_vote.py 
请输入用户名(格式user:xxxxx):"user:tom"
user:tom
请输入要投票的文章(article:redis,article:python):"article:redis"
article:redis
投票还未超过截止日期
增加文章article:redis评分:zincrby score: 432 "article:redis"
增加文章article:redis投票数量:hincrby "article:redis" votes 1

查看文章"article:redis"已投票用户,可以看到新增了"user:tom"

127.0.0.1:6379> smembers voted:redis
1) "user:tom"
2) "user:chengzw"

查看查看文章"article:redis"分数,可以看到增加了432分

127.0.0.1:6379> zrange score: 0 1 withscores
1) "article:python"
2) "500"
3) "article:redis"
4) "1432"

查看查看文章"article:redis"投票总数,可以看到增加了1个

127.0.0.1:6379> hget article:redis votes
"11"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容