加redis锁

ymwl
lqmac 1 year ago
parent 7e3949936f
commit 29a858757d
  1. 2
      application/api/controller/Index.php
  2. 32
      application/api/controller/Order.php
  3. 18
      application/config.php
  4. 21
      thinkphp/library/think/cache/driver/Redis.php

@ -18,6 +18,8 @@ class Index extends Api
*/ */
public function index() public function index()
{ {
echo "111";
exit();
$list = Db::name('user')->where('status','normal')->select(); $list = Db::name('user')->where('status','normal')->select();
foreach ($list as $key => $value) { foreach ($list as $key => $value) {
$amount = Db::name('transaction_record')->where('type',2)->where('user_id', $value['id'])->sum('amount'); $amount = Db::name('transaction_record')->where('type',2)->where('user_id', $value['id'])->sum('amount');

@ -18,7 +18,7 @@ use Yansongda\Pay\Exceptions\GatewayException;
use app\admin\model\transaction\Record; use app\admin\model\transaction\Record;
use think\Log; use think\Log;
use addons\shopro\traits\CouponSend; use addons\shopro\traits\CouponSend;
use think\Cache;
/** /**
* 首页接口 * 首页接口
*/ */
@ -27,7 +27,7 @@ class Order extends Api
use CouponSend; use CouponSend;
protected $noNeedLogin = ['callback']; protected $noNeedLogin = ['callback'];
protected $noNeedRight = ['*']; protected $noNeedRight = ['*'];
const CACHE_TIME = 5;
/** /**
* 首页 * 首页
* *
@ -237,19 +237,23 @@ class Order extends Api
* @return [type] [description] * @return [type] [description]
*/ */
public function createOrder(){ public function createOrder(){
$address_id = $this->request->post("address_id", 0); $address_id = $this->request->post("address_id", 0);
$goods_id = $this->request->post("goods_id", 0); $goods_id = $this->request->post("goods_id", 0);
$num = $this->request->post("num", 1); $num = $this->request->post("num", 1);
$coupon_id = $this->request->post("coupon_id", 0); $coupon_id = $this->request->post("coupon_id", 0);
$user_id = $this->auth->id; $user_id = $this->auth->id;
$user = $this->auth->getUserinfo(); $user = $this->auth->getUserinfo();
//var_dump($user_id);
$address = Address::where('id', $address_id)->where('user_id', $user_id)->find(); $address = Address::where('id', $address_id)->where('user_id', $user_id)->find();
if (!$address) { if (!$address) {
$this->error("收货地址不存在"); $this->error("收货地址不存在");
} }
$goods = Goods::where('id', $goods_id)->where('status','normal')->find(); $goods = Goods::where('id', $goods_id)->where('status','normal')->find();
if (!$goods) { if (!$goods) {
$this->error("商品不存在"); $this->error("商品已被抢完");
} }
$warehouse = Warehouse::where('id', $goods['warehouse_id'])->find(); $warehouse = Warehouse::where('id', $goods['warehouse_id'])->find();
$this->getBuyTime($warehouse, $user); $this->getBuyTime($warehouse, $user);
@ -259,11 +263,17 @@ class Order extends Api
if (time() > strtotime($warehouse['end'])) { if (time() > strtotime($warehouse['end'])) {
$this->error("抢购时间已结束!"); $this->error("抢购时间已结束!");
} }
//使用redis锁,限制下单
$lock_key = "createOrder_".$goods_id;
$res = Cache::store('redis')->setnx($lock_key, 1);
if (!$res) {
$this->error("您的手速太慢了,商品已被抢!");
}
Cache::store('redis')->expire($lock_key, self::CACHE_TIME);
//exit(); //exit();
$order_amount = $goods['price'] * $num; $order_amount = $goods['price'] * $num;
$coupon_price = 0; $coupon_price = 0;
$pay_amount = $order_amount - $coupon_price; $pay_amount = $order_amount - $coupon_price;
$order_sn = "ZH".date("YmdHis").rand(1000,9999); $order_sn = "ZH".date("YmdHis").rand(1000,9999);
//创建订单 //创建订单
@ -301,21 +311,17 @@ class Order extends Api
if ($ret === false) { if ($ret === false) {
$this->error("下单失败"); $this->error("下单失败");
} }
//使用优惠卷 //更新商品状态为下架
$ret = UserCouponModel::where("id", $coupon_id)->update(['use_order_id' => $order->id, 'use_time' => time()]); Goods::where('id', $goods_id)->update(['status' => 'hidden', "updatetime" => time()]);
if ($ret === false) {
$this->error("下单失败");
}
//分佣给买家上级 //分佣给买家上级
$site = Config::get("site"); $site = Config::get("site");
$commission = bcmul($order_amount, $site['primary_distribution'] * 0.01, 2); $commission = bcmul($order_amount, $site['primary_distribution'] * 0.01, 2);
$obj = new UserService(); $obj = new UserService();
$obj->userCommission(0, $this->auth->getUserinfo(), $order_sn, $commission); $obj->userCommission(0, $this->auth->getUserinfo(), $order_sn, $commission);
//更新商品状态为下架
Goods::where('id', $goods_id)->update(['status' => 'hidden', "updatetime" => time()]);
Cache::store('redis')->rm($lock_key);
$this->success("succ"); $this->success("succ");
} }
/** /**

@ -183,11 +183,23 @@ return [
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | 缓存设置 // | 缓存设置
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// 'cache' => [
// // 驱动方式
// 'type' => 'File',
// // 缓存保存目录
// 'path' => CACHE_PATH,
// // 缓存前缀
// 'prefix' => '',
// // 缓存有效期 0表示永久缓存
// 'expire' => 0,
// ],
'cache' => [ 'cache' => [
// 驱动方式 // 驱动方式
'type' => 'File', 'type' => 'redis',
// 缓存保存目录 'host' => '127.0.0.1',
'path' => CACHE_PATH, 'port' => '6379',
'password' => '',
// 缓存前缀 // 缓存前缀
'prefix' => '', 'prefix' => '',
// 缓存有效期 0表示永久缓存 // 缓存有效期 0表示永久缓存

@ -185,4 +185,25 @@ class Redis extends Driver
return $this->handler->flushDB(); return $this->handler->flushDB();
} }
/**
* 设置一个key,如果key存在,不做任何操作.
* @param unknown $key
* @param unknown $value
*/
public function setnx($name, $value)
{
$key = $this->getCacheKey($name);
return $this->handler->setnx($key, $value);
}
/**
* 设置一个key,如果key存在,不做任何操作.
* @param unknown $key
* @param unknown $value
*/
public function expire($name, $expire)
{
$key = $this->getCacheKey($name);
return $this->handler->expire($key, $expire);
}
} }

Loading…
Cancel
Save