如何利用Redis实现长尾关键词秒杀活动的高效处理?
- 内容介绍
- 文章标签
- 相关推荐
本文共计132个文字,预计阅读时间需要1分钟。
使用Redis进行元素操作与HashSet的简单应用示例
不知道这样写能扛得住多少,我没测过
1.[代码][PHP]代码
<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $UserID = (int) $_GET['UserID']; $ActivityID = (int) $_GET['ActivityID']; // 库存检查 if ($redis->decr('Amount') < 0) { $redis->incr('Amount'); echo '{ "exception" : "InvalidNumberException" }'; exit; } // 直接入库 $NoRepeat = $redis->hSet('UserList', $UserID, sprintf('{ "ActivityID" : %d, "Milliseconds" : "%.0f" }', $ActivityID, round(microtime(true) * 1000)) ); if (!$NoRepeat) { // 还原库存 $redis->incr('Amount'); echo '{ "exception" : "SaveInvalidException" }'; exit; } echo sprintf('{ "errno" : 0 , "data" : { "UserID" : %d, "Milliseconds": %.0f } }', $UserID, round(microtime(true) * 1000)); exit;
本文共计132个文字,预计阅读时间需要1分钟。
使用Redis进行元素操作与HashSet的简单应用示例
不知道这样写能扛得住多少,我没测过
1.[代码][PHP]代码
<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $UserID = (int) $_GET['UserID']; $ActivityID = (int) $_GET['ActivityID']; // 库存检查 if ($redis->decr('Amount') < 0) { $redis->incr('Amount'); echo '{ "exception" : "InvalidNumberException" }'; exit; } // 直接入库 $NoRepeat = $redis->hSet('UserList', $UserID, sprintf('{ "ActivityID" : %d, "Milliseconds" : "%.0f" }', $ActivityID, round(microtime(true) * 1000)) ); if (!$NoRepeat) { // 还原库存 $redis->incr('Amount'); echo '{ "exception" : "SaveInvalidException" }'; exit; } echo sprintf('{ "errno" : 0 , "data" : { "UserID" : %d, "Milliseconds": %.0f } }', $UserID, round(microtime(true) * 1000)); exit;

