Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use redis to make the counter of Voting system

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces in detail how to use redis to make the counter of the voting system, the sample code in this article is very detailed, zero basis can also refer to this article, interested friends can refer to it.

Check

First of all, check the number of votes per user. Users have three opportunities to vote every day, so we can set up a button to set his validity period to early tomorrow morning. Users add 1 for every vote, and after 3 votes, they will indicate that the number of times today has been used up.

/ / check the number of votes function checkVote ($uid) {$key = "uid:$uid:cnt"; $tomrTime = mktime (0exsits 0, date ('m'), date ('d') + 1, date ('Y')); if (! $redis- > exsits ($key)) {$redis- > set ($key, 0, ['ex' = > $tomrTime-time ()]); return true;} else if (($cnt = $redis- > get ($key))

< 3 ) { return true; } else { return false; }} 统计投票 次数校验通过后,就需要统计用户的得票数了。 // 得票数计数// uid表示投票人id// touid表示得票人idfunction vote ($uid, $toUid){ $key = "uid:$toUid:vote"; // 投票数校验 if (checkVote($uid)) { // 统计投票数及参选人得票数 $redis->

Incr ($key); $redis- > incr ("uid:$uid:cnt"); return true;} else {return false;}}

My own Ali CVM configuration only has a 1-core cpu1G memory configuration, and the incr performance can reach more than 100000 times per second. Redis performance is really scary.

# redis-benchmark-t incr-qINCR: 105708.25 requests per second

Mysql asynchronously updates the number of votes obtained by users

Finally, we only need to do a scheduled task to let mysql synchronize the user's votes at regular intervals.

The way to do this is to do an endless loop in which 50 users get votes for each loop, and then exit the loop and end the script after traversing all the users.

$start = 0position while (true) {/ / take 50 users per cycle id $users = $DB- > query ("SELECT uid,votes FROM users LIMIT $start, 50"); if (! $users) {exit ();} $keys = []; foreach ($users as $userinfo) {$keys [] = "uid: {$userinfo ['uid']}: vote";} $votes = $redis- > mget ($keys) Foreach ($votes as $index = > $vote) {if ($vote! = $users [$index] ['votes']) {$DB- > query ("UPDATE users SET votes =' $vote' WHERE uid=' {$users [$index] ['uid']}");} $start + = 50;}

The mget command of Redis gets the values of more than one key at a time. After obtaining the number of votes obtained by the redis stored in the redis, compare it with the number of votes in the Mysql first. Update Mysql data only when it is different.

After reading the above, have you mastered the method of using redis to make the counter of the voting system? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report