加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()
{
echo "111";
exit();
$list = Db::name('user')->where('status','normal')->select();
foreach ($list as $key => $value) {
$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 think\Log;
use addons\shopro\traits\CouponSend;
use think\Cache;
/**
* 首页接口
*/
@ -27,7 +27,7 @@ class Order extends Api
use CouponSend;
protected $noNeedLogin = ['callback'];
protected $noNeedRight = ['*'];
const CACHE_TIME = 5;
/**
* 首页
*
@ -237,19 +237,23 @@ class Order extends Api
* @return [type] [description]
*/
public function createOrder(){
$address_id = $this->request->post("address_id", 0);
$goods_id = $this->request->post("goods_id", 0);
$num = $this->request->post("num", 1);
$coupon_id = $this->request->post("coupon_id", 0);
$user_id = $this->auth->id;
$user = $this->auth->getUserinfo();
//var_dump($user_id);
$address = Address::where('id', $address_id)->where('user_id', $user_id)->find();
if (!$address) {
$this->error("收货地址不存在");
}
$goods = Goods::where('id', $goods_id)->where('status','normal')->find();
if (!$goods) {
$this->error("商品不存在");
$this->error("商品已被抢完");
}
$warehouse = Warehouse::where('id', $goods['warehouse_id'])->find();
$this->getBuyTime($warehouse, $user);
@ -259,11 +263,17 @@ class Order extends Api
if (time() > strtotime($warehouse['end'])) {
$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();
$order_amount = $goods['price'] * $num;
$coupon_price = 0;
$pay_amount = $order_amount - $coupon_price;
$order_sn = "ZH".date("YmdHis").rand(1000,9999);
//创建订单
@ -301,21 +311,17 @@ class Order extends Api
if ($ret === false) {
$this->error("下单失败");
}
//使用优惠卷
$ret = UserCouponModel::where("id", $coupon_id)->update(['use_order_id' => $order->id, 'use_time' => time()]);
if ($ret === false) {
$this->error("下单失败");
}
//更新商品状态为下架
Goods::where('id', $goods_id)->update(['status' => 'hidden', "updatetime" => time()]);
//分佣给买家上级
$site = Config::get("site");
$commission = bcmul($order_amount, $site['primary_distribution'] * 0.01, 2);
$obj = new UserService();
$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");
}
/**

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

@ -185,4 +185,25 @@ class Redis extends Driver
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