Home > php教程 > PHP源码 > Redis version flash kill exercise

Redis version flash kill exercise

WBOY
Release: 2016-07-06 13:28:30
Original
1270 people have browsed it
Jump to [1] [Full screen preview]
<?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;
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template