Преглед на файлове

所有cache存储全部改为Redis存储

duqinya преди 5 години
родител
ревизия
75fc942a4d
променени са 1 файла, в които са добавени 70 реда и са изтрити 16 реда
  1. 70 16
      app/Repositories/CmsContentTemplateSetRepository.php

+ 70 - 16
app/Repositories/CmsContentTemplateSetRepository.php

@@ -7,6 +7,7 @@ use App\Models\CmsContentTemplateSet;
 use App\Models\CmsSubjectProduct;
 use App\Models\CmsSubject;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Tymon\JWTAuth\Facades\JWTAuth;
@@ -44,16 +45,32 @@ class CmsContentTemplateSetRepository
         $market_key = config('constants.CMS_MARKET');
         //团购首页
         if ($request['type'] == 0) {
-            if (Cache::has($group_key)) {
-                return Cache::store('redis')->get($group_key);
+//            if (Cache::has($group_key)) {
+//                return Cache::store('redis')->get($group_key);
+//            }
+            Log::info(Redis::exists($group_key));
+            if (Redis::exists($group_key)) {
+                $result = Redis::get($group_key);
+                $res = json_decode($result, true);
+                Log::info($res);
+                return $res;
             }
+
             $group_array['apply_type'] = "group";
             $group_array['title'] = $temalates->title;
             $group_array['content'] = [];
         } else {//菜市场首页
-            if (Cache::has($market_key)) {
-                return Cache::store('redis')->get($market_key);
+//            if (Cache::has($market_key)) {
+//                return Cache::store('redis')->get($market_key);
+//            }
+            Log::info(Redis::exists($market_key));
+            if (Redis::exists($market_key)) {
+                $result = Redis::get($market_key);
+                $res = json_decode($result, true);
+                Log::info($res);
+                return $res;
             }
+
             $group_array['apply_type'] = "market";
             $group_array['title'] = $temalates->title;
             $group_array['content'] = [];
@@ -130,8 +147,13 @@ class CmsContentTemplateSetRepository
             $group_array['content'][3]['area_type'] = "floor";
             $group_array['content'][3]['rule'] = $new_rule2;
 
-            if (!Cache::has($market_key)) {
-                Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期
+//            if (!Cache::has($market_key)) {
+//                Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期
+//            }
+
+            if (!Redis::exists($market_key)) {
+                Redis::setex($market_key, 600, json_encode($group_array));//10分钟过期
+                Log::debug('菜市场首页缓存存储成功-' . json_encode($group_array));
             }
 
         } else {
@@ -140,9 +162,16 @@ class CmsContentTemplateSetRepository
             $group_array['content'][2]['area_type'] = "floor";
             $group_array['content'][2]['rule'] = $new_rule2;
 
-            if (!Cache::has($group_key)) {
-                Cache::store('redis')->put($group_key, $group_array, 600);
+//            if (!Cache::has($group_key)) {
+//                Cache::store('redis')->put($group_key, $group_array, 600);
+//            }
+
+            if (!Redis::exists($group_key)) {
+                Redis::setex($group_key, 600, json_encode($group_array));//10分钟过期
+                Log::debug('团购首页缓存存储成功-' . json_encode($group_array));
             }
+
+
         }
         return $group_array;
     }
@@ -194,8 +223,16 @@ class CmsContentTemplateSetRepository
         $group_array['title'] = $temalates->title;
         $group_array['content'] = [];
         $exchange_key = config('constants.CMS_EXCHANGE');
-        if (Cache::has($exchange_key)) {
-            return Cache::store('redis')->get($exchange_key);
+//        if (Cache::has($exchange_key)) {
+//            return Cache::store('redis')->get($exchange_key);
+//        }
+
+        Log::info(Redis::exists($exchange_key));
+        if (Redis::exists($exchange_key)) {
+            $result = Redis::get($exchange_key);
+            $res = json_decode($result, true);
+            Log::info($res);
+            return $res;
         }
 
         $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0, 'status' => 1])->limit(1)->first();
@@ -269,8 +306,13 @@ class CmsContentTemplateSetRepository
             $group_array['content'][$count + $key + 1]['rule'] = $val['rule'];
         }
 
-        if (!Cache::has($exchange_key)) {
-            Cache::store('redis')->put($exchange_key, $group_array, 600);//10分钟过期
+//        if (!Cache::has($exchange_key)) {
+//            Cache::store('redis')->put($exchange_key, $group_array, 600);//10分钟过期
+//        }
+
+        if (!Redis::exists($exchange_key)) {
+            Redis::setex($exchange_key, 600, json_encode($group_array));//10分钟过期
+            Log::debug('兑换商城缓存存储成功-' . json_encode($group_array));
         }
 
         return $group_array;
@@ -299,8 +341,16 @@ class CmsContentTemplateSetRepository
 
         $product_key = config('constants.CMS_PRODUCTS');
         $product_key = sprintf($product_key,md5($res_id));
-        if (Cache::has($product_key)) {
-            return Cache::store('redis')->get($product_key);
+//        if (Cache::has($product_key)) {
+//            return Cache::store('redis')->get($product_key);
+//        }
+
+        Log::info(Redis::exists($product_key));
+        if (Redis::exists($product_key)) {
+            $result = Redis::get($product_key);
+            $res = json_decode($result, true);
+            Log::info($res);
+            return $res;
         }
 
         try {
@@ -319,8 +369,12 @@ class CmsContentTemplateSetRepository
 
             $url = config("customer.app_service_url") . '/product/exchange/subject';
             $http = http($url, $array, 'get');
-            if (!Cache::has($product_key)) {
-                Cache::store('redis')->put($product_key, $http, 600);//10分钟过期
+//            if (!Cache::has($product_key)) {
+//                Cache::store('redis')->put($product_key, $http, 600);//10分钟过期
+//            }
+            if (!Redis::exists($product_key)) {
+                Redis::setex($product_key, 600, json_encode($product_key));//10分钟过期
+                Log::debug('专题商品缓存存储成功-' . json_encode($product_key));
             }
             return $http;