l1n6yun's Blog

记录学习的技能和遇到的问题

0%

PHP Redis 秒杀

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Redis 初始化
*
* @param int $goodsId 商品ID
* @param int $number 商品数量
*/
function initRedis($goodsId, $number)
{
$storeKey = "goods_{$goodsId}_store";

$redis = new redis();
$redis->connect('127.0.0.1', 6379);

$redis->del($storeKey);

for ($i = 0; $i < $number; $i++) {
$redis->lpush($storeKey, 1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 秒杀入口
*
* @param int $goodsId 商品ID
* @return bool
*/
function kills($goodsId)
{
$storeKey = "goods_{$goodsId}_store";

$redis = new redis();
$redis->connect('127.0.0.1', 6379);

$count = $redis->lpop($storeKey);
if ($count) {
$data = "";
$data["order_sn"] = rand(1000, 9999); // 测试订单号

// 1. 直接进行数据库操作
$orderModel = new orderModel();
$result = $orderModel->save($data);

if ($result !== false) {
return true;
} else {
$redis->lpush($storeKey, 1);
return false;
}

// 2. 添加队列后继操作
Queue::push($job, $data, 'seckill');
}
}