From ee693af9f861680e928c6e2c4dac4d496525790f Mon Sep 17 00:00:00 2001 From: lqmac Date: Thu, 26 Oct 2023 00:38:36 +0800 Subject: [PATCH] 1 --- application/api/controller/Order.php | 14 +++++++++++-- application/api/controller/User.php | 2 +- application/config.php | 17 +++++++++++++--- thinkphp/library/think/cache/driver/Redis.php | 20 +++++++++++++++++++ 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/application/api/controller/Order.php b/application/api/controller/Order.php index d26dba5..1137bfe 100755 --- a/application/api/controller/Order.php +++ b/application/api/controller/Order.php @@ -19,7 +19,7 @@ use Yansongda\Pay\Exceptions\GatewayException; use app\admin\model\transaction\Record; use think\Log; use addons\shopro\traits\CouponSend; - +use think\Cache; /** * 首页接口 */ @@ -28,7 +28,7 @@ class Order extends Api use CouponSend; protected $noNeedLogin = ['callback','test']; protected $noNeedRight = ['*']; - + const CACHE_TIME = 5; /** * 首页 * @@ -288,6 +288,14 @@ class Order extends Api if (time() > strtotime($warehouse['end'])) { $this->error("抢购时间已结束!"); } + //使用redis锁,限制下单 + $lock_key = $this->request->domain()."_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 = $order['pay_amount']; $pay_amount = $order_amount; @@ -338,6 +346,8 @@ class Order extends Api $obj = new UserService(); $obj->userCommission(0, $this->auth->getUserinfo(), $order_sn, $commission); + Cache::store('redis')->rm($lock_key); + $this->success("succ"); } /** diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 8c740a6..9d54eba 100755 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -62,7 +62,7 @@ class User extends Api $account = $this->request->post('account'); $password = $this->request->post('password'); $code = $this->request->post('code'); - if (!$account || !$password) { + if (!$account || !$password || !$code) { $this->error(__('Invalid parameters')); } diff --git a/application/config.php b/application/config.php index aa9281c..f553201 100755 --- a/application/config.php +++ b/application/config.php @@ -183,11 +183,22 @@ 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表示永久缓存 diff --git a/thinkphp/library/think/cache/driver/Redis.php b/thinkphp/library/think/cache/driver/Redis.php index d7bf6d1..9d42c3d 100755 --- a/thinkphp/library/think/cache/driver/Redis.php +++ b/thinkphp/library/think/cache/driver/Redis.php @@ -184,5 +184,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); + } }