<?php
namespace Admin\Controller;
use Think\Controller;
class TestController extends Controller
{
public function redis()
{
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$store = 50;
$res = $redis->lLen('goods_store');
$count = $store - $res;
for ($i = 0; $i < $count; $i++) {
$redis->lPush('goods_store', 1);
}
echo $redis->lLen('goods_store');
}
public function index()
{
$id = I('id');
if ( ! $id) {
die('缺少参数');
}
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$count = $redis->lPop('goods_store');
halt($count);
if ( ! $count) {
die('没有货物');
} else {
$ordersn = md5(uniqid(microtime(true), true));
$uid = rand(1, 9999);
$status = 1;
$data = M('goods')->where(['id' => $id])->find();
if ( ! $data) {
die('商品不存在');
}
M()->startTrans();
try {
$res = M('orders')->add(['order_sn' => $ordersn, 'user_id' => $uid, 'goods_id' => $id, 'price' => 1, 'addtime' => date('Y-m-d H:i:s')]);
if ( ! $res) {
throw new \Exception('a');
}
$res = M('Goods')->where(['id' => $id])->setDec('count');
if ( ! $res) {
throw new \Exception('a');
}
M()->commit();
} catch (\Exception $e) {
M()->rollback();
die;
}
}
}
}
php redis 秒杀
最新推荐文章于 2024-07-16 08:17:48 发布