Bläddra i källkod

预售到期自动取消更新--使用redis缓存来记录预售开启,查询预售从redis缓存中查询

shizhongqi 4 månader sedan
förälder
incheckning
48bdd7c72f
2 ändrade filer med 71 tillägg och 34 borttagningar
  1. 40 20
      app-ghs/controllers/ProductController.php
  2. 31 14
      console/controllers/ProductController.php

+ 40 - 20
app-ghs/controllers/ProductController.php

@@ -108,29 +108,49 @@ class ProductController extends BaseController
         $id = $post['id'] ?? 0;
         $info = ProductClass::getById($id, true);
         ProductClass::check($info, $this->mainId);
-        $str = $post['date'] ?? '';
-        $presell = $post['presell'] ?? 0;
-        $newStr = '';
-        if ($presell == 1) {
-            if (empty($str)) {
-                util::fail('请填写预售日期');
-            }
-            $arr = json_decode($str, true);
-            if (empty($arr)) {
-                util::fail('请填写预售日期');
+
+        try{
+            $str = $post['date'] ?? '';
+            $presell = $post['presell'] ?? 0;
+            $newStr = '';
+            if ($presell == 1) {
+                if (empty($str)) {
+                    util::fail('请填写预售日期');
+                }
+                $arr = json_decode($str, true);
+                if (empty($arr)) {
+                    util::fail('请填写预售日期');
+                }
+                $new = [];
+                foreach ($arr as $date) {
+                    $new[] = strtotime($date);
+                }
+                $new = array_unique(array_filter($new));
+                sort($new);
+                $newStr = implode(',', $new);
+
+                //使用redis缓存来保存某用户设置了某花材的预售
+                $redis = Yii::$app->redis;
+                $redis->set('product_presell:'.$id, $new[0]);
+            } else {
+                $redis = Yii::$app->redis;
+                if ($redis->exists('product_presell:'.$id)) {
+                    $redis->del('product_presell:'.$id);
+                }
             }
-            $new = [];
-            foreach ($arr as $date) {
-                $new[] = strtotime($date);
+            $info->presell = $presell;
+            $info->presellDate = $newStr;
+            $info->discountPrice = 0;
+            $info->save();
+        }catch(\Exception $e){
+            // 失败情况,要把缓存删除
+            if ($presell == 1) {
+                $redis = Yii::$app->redis;
+                if ($redis->exists('product_presell:'.$id)) {
+                    $redis->del('product_presell:'.$id);
+                }
             }
-            $new = array_unique(array_filter($new));
-            asort($new);
-            $newStr = implode(',', $new);
         }
-        $info->presell = $presell;
-        $info->presellDate = $newStr;
-        $info->discountPrice = 0;
-        $info->save();
         util::complete();
     }
 

+ 31 - 14
console/controllers/ProductController.php

@@ -293,23 +293,40 @@ class ProductController extends Controller
     //预售到期自动取消
     public function actionCancelPresell()
     {
-        $now = time(); //注意脚本执行时间不能是凌晨0点,否则会出问题,这个比较时间要修改为当前时间+1小时
-        $list = ProductClass::getAllByCondition(['presell' => 1], 'addTime DESC', 'id, presell, presellDate', null, true);
-        if (!empty($list)) {
-            foreach ($list as $item) {
-                $presellDate = $item->presellDate;
-                $dateArr = explode(',', $presellDate);
-                if (empty($dateArr)) {
+        $now = time(); //注意脚本执行时间要在凌晨0点之后,否则会出问题
+        $redis = Yii::$app->redis;
+        $cursor = '0';
+        do {
+            $scanResult = $redis->executeCommand('SCAN', [$cursor, 'MATCH', 'product_presell:*', 'COUNT', 500]);
+            $cursor = $scanResult[0] ?? '0';
+            $keys = $scanResult[1] ?? [];
+            if (empty($keys)) {
+                continue;
+            }
+            foreach ($keys as $key) {
+                $firstDate = (int)$redis->get($key);
+                if ($firstDate >= $now) {
+                    continue;
+                }
+                $id = (int)str_replace('product_presell:', '', $key);
+                if ($id <= 0) {
+                    $redis->del($key);
                     continue;
                 }
-                $firstDate = $dateArr[0];
-                $firstDate = strtotime($firstDate);
-                if ($firstDate < $now) {
-                    $item->presell = 0;
-                    $item->presellDate = '';
-                    $item->save();
+                $item = ProductClass::getById($id, true);
+                if (empty($item)) {
+                    $redis->del($key);
+                    continue;
+                }
+                if ((int)$item->presell !== 1) {
+                    $redis->del($key);
+                    continue;
                 }
+                $item->presell = 0;
+                $item->presellDate = '';
+                $item->save();
+                $redis->del($key);
             }
-        }
+        } while ($cursor !== '0');
     }
 }