lqmac 1 year ago
parent d883d148b7
commit ee693af9f8
  1. 14
      application/api/controller/Order.php
  2. 2
      application/api/controller/User.php
  3. 17
      application/config.php
  4. 20
      thinkphp/library/think/cache/driver/Redis.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");
}
/**

@ -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'));
}

@ -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表示永久缓存

@ -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);
}
}

Loading…
Cancel
Save