Home  >  Article  >  php教程  >  Redis version flash kill exercise

Redis version flash kill exercise

WBOY
WBOYOriginal
2016-07-06 13:28:301246browse
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;
Statement:
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
Previous article:ajax backup recordNext article:ajax backup record