From 28633df9cdac5108c9befdca3588014028a8b6df Mon Sep 17 00:00:00 2001 From: hant Date: Mon, 30 Jun 2025 22:38:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A2=E5=8D=95=E7=BC=96?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/model/Order.php | 8 +++--- application/common/services/RedisService.php | 28 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 application/common/services/RedisService.php diff --git a/application/admin/model/Order.php b/application/admin/model/Order.php index ec0076f..1036f48 100644 --- a/application/admin/model/Order.php +++ b/application/admin/model/Order.php @@ -3,6 +3,7 @@ namespace app\admin\model; use app\admin\library\Auth; +use app\common\services\RedisService; use think\Cache; use think\Model; use traits\model\SoftDelete; @@ -289,11 +290,8 @@ class Order extends Model $date = date('ymd'); // 6 位日期 $key = 'order_inc:' . date('Ymd'); - $inc = Cache::store('redis')->inc($key); // 自增 - - // 设置 key 过期(防止 key 永久存在) - $handler = Cache::store('redis')->handler(); - $handler->expire($key, 86400); // 设置 1 天过期 + $redis = new RedisService(0); + $inc = $redis->incrWithExpire($key); if ($inc > 9999) { throw new \Exception('订单号已满'); diff --git a/application/common/services/RedisService.php b/application/common/services/RedisService.php new file mode 100644 index 0000000..f1a1840 --- /dev/null +++ b/application/common/services/RedisService.php @@ -0,0 +1,28 @@ +redis = Cache::store('redis')->handler(); + $this->redis->select($db); + } + + public function incrWithExpire(string $key, int $expireSeconds = 86400) + { + $val = $this->redis->incr($key); + if ($val === 1) { + $this->redis->expire($key, $expireSeconds); + } + return $val; + } +} \ No newline at end of file