فهرست منبع

getRandomRecommendBooks use redis

zz 6 سال پیش
والد
کامیت
49759e2a3e
1فایلهای تغییر یافته به همراه19 افزوده شده و 8 حذف شده
  1. 19 8
      app/Modules/Book/Models/BookConfig.php

+ 19 - 8
app/Modules/Book/Models/BookConfig.php

@@ -670,17 +670,17 @@ class BookConfig extends Model
     	// 判断渠道男女频
     	// 判断渠道男女频
     	$user = User::getById($uid);
     	$user = User::getById($uid);
     	$distribution_channel_id = isset($user->distribution_channel_id)?$user->distribution_channel_id:'';
     	$distribution_channel_id = isset($user->distribution_channel_id)?$user->distribution_channel_id:'';
-    	// 男频强制推广男频书
+    	// 男频强制推广男频书
     	$channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
     	$channel_sex = ChannelService::getChannelCompanySex($distribution_channel_id);
-    	$channel_name = '';
-    	if($channel_sex == 1){
-    		$channel_name = '男频';
-    	}else{
+    	$channel_name = '';
+    	if($channel_sex == 1){
+    		$channel_name = '男频';
+    	}else{
 	    	$sex = ForceSubscribeService::getSimpleSexByUid($uid);
 	    	$sex = ForceSubscribeService::getSimpleSexByUid($uid);
-	    	$channel_name = $sex==1?'男频':'女频';
+	    	$channel_name = $sex==1?'男频':'女频';
     	}
     	}
-
-    	\Log::info('getH5RecommendBooks:pos:'.$pos.' uid:'.$uid.' num:'.$num.' channel_name:'.$channel_name.' channel_sex:'.$channel_sex.' distribution_channel_id:'.$distribution_channel_id);
+
+    	\Log::info('getH5RecommendBooks:pos:'.$pos.' uid:'.$uid.' num:'.$num.' channel_name:'.$channel_name.' channel_sex:'.$channel_sex.' distribution_channel_id:'.$distribution_channel_id);
     	
     	
     	$bids = [];
     	$bids = [];
     	$random_recommend = true;
     	$random_recommend = true;
@@ -713,6 +713,14 @@ class BookConfig extends Model
      * 获取随机的推荐书籍bid
      * 获取随机的推荐书籍bid
      */
      */
     public static function  getRandomRecommendBooks($channel_name, $num){
     public static function  getRandomRecommendBooks($channel_name, $num){
+        if($channel_name == '男频'){
+            $channel_name_replace = 'male';
+        }else{
+            $channel_name_replace = 'female';
+        }
+        $redis_key = sprintf('channel_name:%s:num%s',$channel_name_replace,$num);
+        $cache = Redis::get($redis_key);
+        if($cache) return explode(',',$cache);
 	    $bids = self::join('books', 'book_configs.bid', '=', 'books.id')
 	    $bids = self::join('books', 'book_configs.bid', '=', 'books.id')
 	    ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
 	    ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
 	    ->select('book_configs.bid')
 	    ->select('book_configs.bid')
@@ -723,6 +731,9 @@ class BookConfig extends Model
 	    ->inRandomOrder()
 	    ->inRandomOrder()
 	    ->limit($num)
 	    ->limit($num)
 	    ->get()->pluck('bid')->all();
 	    ->get()->pluck('bid')->all();
+        if($bids){
+            Redis::setex($redis_key,7200,implode(',',$bids));
+        }
 	    return $bids;
 	    return $bids;
     }
     }