php combination Redis project to achieve 100 million users to vote and to view real-time voting case

Scene: a site to do a voting system for its project, is expected to have 100 million users to vote within one hour of on-line voting project, hoping users will be able to see real-time voting finished voting

 This scenario can be used redis + mysql hot and cold data exchange to resolve.

What is hot and cold data exchange?

Cold data: data before using thermal data: The data currently used.
Exchange: The data stored in the cycle to MySQL Redis

 Business Process

After the user vote, first save the data to a vote Redis, the data is hot data, and then periodically (eg 5s) data to MySQL to save heat, the data becomes cold data, then data will be deleted from the cold in Redis , again and again, knowing that the end of one hour to vote.

 

Program structure.

 

index.html file

It is home to vote, there are three voting buttons, analog to three users to vote, click on the button, using ajax call vote.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
</head>
<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<body>
<p><span id="uid1">0</span><input type="button" value="用户1" onclick="vote(1);" /></p>
<p><span id="uid2">0</span><input type="button" value="用户2" onclick="vote(2);" /></p>
<p><span id="uid3">0</span><input type="button" value="用户3" onclick="vote(3);" /></p>
</body>
<script>
    function vote(i){
        $.get('./vote.php?uid='+i,function(rs){
            var span = '#uid'+i;
            $(span).html(rs);
        });
    }
</script>
</html>

 

vote.php

This file is the main achievement of the logic vote. First connect the Redis server, and then save the voter id, then the voter id as key votes recorded for each user, and then returned to the index.html file, last used as a key global_voteid record the total number of votes, can be used as self-growth MySQL key. Then recorded uid, ip, time and other data.

Note that the format has certain requirements:

If voteid 3, records the IP, then the key is the vote: 3: ip: 127.0.0.1

<? PHP
 $ Redis = new new the Redis ();
 $ Redis -> Connect ( '127.0.0.1', 6379 );
 $ Redis -> the auth ( '123456' );
 $ Redis -> SELECT (. 1); // Select Database 1

// Calculate the total number of votes for each user 
$ UID = the intval ( $ _GET [ 'UID' ]);
 // $ UID = the mt_rand (l, 3); // specified random vote art, stress testing facilitate 
echo  $ Redis - > incr ( $ UID );
 $ voteid = $ Redis -> incr ( 'global_voteid' );
 $ Redis -> SET ( 'vote:'. $ voteid . ': UID', $ UID );
 $ IP = $ _SERVER [ 'REMOTE_ADDR' ];
 $ Redis -> SET ( 'vote:'. $ voteid ': IP',. $ IP );
 $ Redis -> SET ( 'vote:'. $ voteid .':time', time());

 

Important content

This file is the main achievement of hot and cold data exchange, first connect MySQL database and redis server, and then every five seconds to execute the while loop, obtained from the growth of the primary key and the last vote is inserted vote mysql primary key (position) in a while loop. Determining whether there is inserted location, if there is no re-insertion, if all the insertion is completed, it waits, if not completely inserted, insertion proceeds.

<? PHP
 // connect to the database 
$ PDO = new new the PDO ( 'MySQL: Host = 39.98.81.13; dbname = the try', 'the try', 'yn3emW6ksYhwwseh' );
 $ PDO -> Query ( 'names SET UTF8' );

// connect Redis 
$ Redis = new new the Redis ();
 $ Redis -> Connect ( '127.0.0.1', 6379 );
 $ Redis -> the auth ( '123456' );
 $ Redis -> SELECT (. 1); // Select database. 1 
$ time = time ; () + 3600 // after the setting time to an hour
// infinite loop 
the while ( $ Time > Time ()) {
     $ VID = $ Redis -> GET ( 'global_voteid'); // auto increment primary key 
    $ Last = $ Redis -> GET ( 'Last'); // last inserted mysql primary key vote
    // If the database is not inserted, beginning affirmative to true 
    IF (! $ Last ) {
         $ Last = 0; // set to 0 
    }
     // If all the data has been inserted to MySQL 
    IF ( $ VID == $ Last ) {
         echo "the wait \ n-"; // output waiting 
    } the else {
         // insert into the database operations 
        $ SQL = 'iNSERT iNTO vote (VID, UID, IP, Time) values' ;
         for ( $ I = $ VID ; $ I > $ Last ; $ I - ) {
            $k1 = 'vote:' . $i . ':uid';
            $k2 = 'vote:' . $i . ':ip';
            $k3 = 'vote:' . $i . ':time';
            $row = $redis->mget([$k1, $k2, $k3]);
            $sql .= "($i,$row[0],'$row[1]',$row[2]),";
            $redis->delete($k1, $k2, $k3);
        }
        SQL $ = substr ( $ SQL , 0, -1 );
         $ PDO -> Exec ( $ SQL );
         $ Redis -> SET ( 'Last', $ VID ); // set the insertion position of the primary key 
        echo 'the OK' ;
    }
    SLEEP (20 is); // every 20 seconds execution cycle 
}

Table vote

CREATE TABLE `vote` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `vid` int(11) unsigned NOT NULL,
  `uid` int(11) DEFAULT NULL,
  `ip` char(20) DEFAULT NULL,
  `time` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB AUTO_INCREMENT = 27 the DEFAULT CHARSET = utf8 the COMMENT = 'PHP Redis achieve 100 million users combined voting items table';

 

Run steps:

1, run swap.php file, redis monitor voting

linux system using php command-line tool to call swap.php ( the recommended way )

 

 2. Analog request to vote

View redis library has requested record

 

20s review database, data synchronization to vote mysql

 

 Link: https: //mp.weixin.qq.com/s/7_5ICy-9ZbOD-BcFG2reZQ

Guess you like

Origin www.cnblogs.com/clubs/p/11806603.html