Redis之網站文章投票

來源:互聯網
上載者:User

標籤:ctr   代碼   pass   時間   投票   tle   add   host   ash   

直接上代碼:

# python3# -*- coding: utf-8 -*-import redisimport timeONE_WEEK_IN_SECONDS = 7 * 86400# 如果一篇文章獲得200個贊,那麼這篇文章就是有趣的VOTE_SCORE = 86400 / 200def redis_init(redis):    # article_time記錄文章發布時間    redis.zadd(‘article_time‘, article_100408=1496662197, article_100635=1496669721, article_100716=1496660089)    # article_score記錄文章得分    redis.zadd(‘article_score‘, article_100408=1496666517, article_100635=1496670153, article_100716=1496665705)    # voted_article_id記錄編號為article_id的文章的點贊使用者集合    redis.sadd(‘voted_100408‘, ‘user_234487‘, ‘user_253378‘, ‘user_364680‘,               ‘user_132097‘, ‘user_350917‘)    # 用hash描述每篇文章    article_desc = {‘title‘:‘kunlun‘, ‘link‘:‘www.kunlun.com‘, ‘poster‘:‘fengge‘, ‘time‘:1441728000, ‘votes‘:523}    redis.hmset(‘article_100408‘, article_desc)    article_desc = {‘title‘: ‘zhuxian‘, ‘link‘: ‘www.zhuxian.com‘, ‘poster‘: ‘xiaoding‘, ‘time‘: 1081440000, ‘votes‘: 677}    redis.hmset(‘article_100635‘, article_desc)    article_desc = {‘title‘: ‘soushenji‘, ‘link‘: ‘www.soushenji.com‘, ‘poster‘: ‘shuxiayehu‘,                    ‘time‘: 1187280000, ‘votes‘: 421}    redis.hmset(‘article_100635‘, article_desc)def article_note(conn, user, article):    cutoff = time.time() - ONE_WEEK_IN_SECONDS    if conn.zscore(‘article_time‘, article) < cutoff:        return    article_id = article.partition(‘_‘)[-1]    if conn.sadd(‘voted_‘ + article_id, user):        conn.zincrby(‘article_score‘, article, VOTE_SCORE)        conn.hincrby(article, ‘votes‘, 1)r = redis.StrictRedis(host=‘redis_server_ip‘, port=6379, password=‘redis_passwd‘, db=0)redis_init(r)article_note(r,‘use_115423‘, ‘article_100408‘)

 

參考資料:

《Redis實戰》

Redis之網站文章投票

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.