Redis in Action note (i) article votes (1) Initialize data + vote + Publish article

Source: Internet
Author: User
Tags redis server

The original book uses Python to interact with Redis, and I use PHP to do it.

Environment: LNMP (CentOS 6.6 + Nginx 1.8.0 + MySQL 5.6.23 + PHP 5.6.9) + Redis 3.0.7 + Phpredis 2.2.4

Start the Redis service on Linux first:

[Email protected] ~]# cd/usr/local/redis/[[email protected] redis]#/bin/redis-server./etc/redis.conf [Email Protected] redis]# PS aux|grep Redis

If displayed:

Root      2239  0.2  0.1  35556  1744?        SSL  12:08   0:00./bin/redis-server *:6379          root      2243  0.0   0.0 5976 724 pts/0    s+   12:08   0:00 grep redis

Description Redis service is turned on, port number 6379

  

redis.php

<?php//the number of seconds in a week $seconds = 7 * 86400;define (One_week_in_seconds, $seconds);//each vote of the article plus the score define (Vote_score, 432);// Instantiate Redis Object $redis = new Redis ();//Connect to Redis server $conn = $redis->connect (' localhost ', 6379);

  

init_data.php data for adding a case

<?php/* preparing data for the polling site */require ' redis.php ';//1. Ordered collection of articles sorted by publication time Zset$article_info = [' 100408 ' =>[' time ' = Strtotime (' 2016-4-10 '), ' score ' =>1332164063.49], ' 100635 ' =>[' time ' =>strtotime (' 2016-4-28 '), ' score ' = 1332174713.47], ' 100716 ' =>[' time ' =>strtotime (' 2016-4-29 '), ' score ' =>1332225027.26]];foreach ($article _ info as $key = = $val) {$redis->zadd (' Time: ', $val [' Time '], ' article: '. $key);} 2. Sorting the ordered collection of articles according to the score Zsetforeach ($article _info as $key + = $val) {$redis->zadd (' Score: ', $val [' Score '], ' article: '. $ key);} 3. Set of users who voted for an article (id:100408) set$users = [234487, 253378, 364680, 132097, 350917];foreach ($users as $key + = $val) {$red Is->sadd (' voted:100408 ', $val);}

  

Vote.php is used to vote for articles, where article ID (article_id) and voting User ID (user_id) are passed by get ( code listing 1-6 article_vote () function )

<?phpheader (' Content-type:text/html;charset=utf-8 '); require ' redis.php '; $user _id = Empty ($_get[' user_id '])? 0: (int) $_get[' user_id '); $article _id = Empty ($_get[' article_id '])? 0: (int) $_get[' article_id '];function article_vote ($redis, $user _id, $article _id) {$cutoff = time ()-One_week_in_ seconds;//127.0.0.1:6379> zscore time article:100408//"1460217600" if (Intval ($redis->zscore (' Time: ', ' article : '. $article _id)) < $cutoff) {return false;} if ($redis->sadd (' voted: '. $article _id, $user _id)) {//Zincrby key increment member//as an ordered set score member article:100408 SCO The RE value plus increment increment$score_new = $redis->zincrby (' Score: ', Vote_score, ' article: '. $article _id); Echo $score _new;// Hincrby key field increment//adds an incremental increment to the value of the field field in the hash table key. If the user is voting for this article for the first time, increase the number of votes and the rating of this article} else {return false;} return true;} if (! Article_vote ($redis, $user _id, $article _id)) {echo ' votes failed ';} else {echo ' vote succeeded ';}

  

To perform http://yourdomain/init_data.php, complete the Redis connection and initialize the data, you can enter the Redis client query article to poll for an ordered set of points (Zset) and the collection of poll users for article 100408 (set):

[[email protected] redis]# ./bin/redis-cli127.0.0.1:6379> zrange score:0-1 withscores1) "article:100408" 2) " 1332164063.49 "3)" article:100635 "4)" 1332174713.47 "5)" article:100716 "6)" 1332225027.26 "127.0.0.1:6379> Zrange time:0-1 withscores1) "article:100408" 2) "1460217600" 3) "article:100635" 4) "1461772800" 5) "article:100716" 6) " 1461859200 "

  

Then visit http://yourdomain/vote.php?user_id=100&article_id=100408 let user_id for 100 users to vote for the article number 100408.

Re-enter the Redis client query article to poll for an ordered set of points (Zset) and the collection of poll users for article 100408 (set):

  

Publish article post_article.php ( Code Listing 1-7 post_article () function )

<?phprequire ' redis.php ';//@param object $redis Redis object//@param int $user _id user number//@param string $title article title//@par  AM string $link article link function post_article ($redis, $user _id, $title, $link) {$article _id = $redis->incr (' article: ');// Generate a new article id$voted = ' voted: '. $article _id; $redis->sadd ($voted, $user _id); Add the user who posted the article to the list of users who have voted on the article $redis->expire ($voted, one_week_in_seconds); Set the voting list expiration to one week $now = time (), $article = ' article: '. $article _id, $redis->hmset ($article, [' title ' = + $title, ' Link ' = ' $link, ' poster ' = $user _id, ' time ' = $now, ' votes ' + 1]); The information of the article is stored in a loose joins $redis->zadd (' score: ', $now + Vote_score, $article); Add the article to $redis->zadd (' Time: ', $now, $article) in an ordered collection sorted by score; Add the article to the ordered collection sorted according to the release time return $article _id;} $user _id = isset ($_get[' user_id ')? $_get[' user_id ': 0; $mtid = Mt_rand (0,999); $title = ' article title '. $mtid; $link = ' http://www.youdomain.com/article/' $mtid; if (Post_article ($redis, $user _id, $title, $link)) {echo ' success ';} else {echo ' error ';} 

Visit: http://yourdomain/post_article.php

Because the URL does not have parameters and article is not present in Redis: There will be a user user_id 0 published Article:1

This is a query for the ordered collection of time and fractions in Redis, the Article:1 collection of voting users, and the hash of article:1:

127.0.0.1:6379> zrange time:0-1 withscores1) "article:100408" 2) "1460217600" 3) "article:100635" 4) "1461772800" 5) " article:100716 "6)" 1461859200 "7)" Article:1 "8)" 1465105632 "127.0.0.1:6379> zrange score:0-1 withscores1)" article : 100408 "2)" 1332164495.49 "3)" article:100635 "4)" 1332174713.47 "5)" article:100716 "6)" 1332225027.26 "7)" Article:1 "8) "1465106064" 127.0.0.1:6379> smembers voted:11) "0" 127.0.0.1:6379> hgetall article:1 1) "title" 2) "\xe6\x96\x87\  xe7\xab\xa0\xe6\xa0\x87\xe9\xa2\x9868 "3)" link "4)" HTTP://WWW.YOUDOMAIN.COM/ARTICLE/68 "5)" poster "6)" 0 "7)" Time "8) "1465105632" 9) "votes" 10) "1"

  

   

Report:

The case comes from "Redis combat"

Redis Command Reference: http://doc.redisfans.com/sorted_set/zrange.html

Php-redis Chinese Document: http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html

Redis in Action note (i) article votes (1) Initialize data + vote + Publish article

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.