Browse Source

增加缓存优化开关

magicblack 3 years ago
parent
commit
8289ff5739

+ 5 - 0
application/admin/controller/System.php

@@ -92,6 +92,7 @@ class System extends Base
 
             $config['app']['search_vod_rule'] = join('|', $config['app']['search_vod_rule']);
             $config['app']['search_art_rule'] = join('|', $config['app']['search_art_rule']);
+            $config['app']['vod_search_optimise'] = join('|', $config['app']['vod_search_optimise']);
 
             $config['extra'] = [];
             if(!empty($config['app']['extra_var'])){
@@ -152,6 +153,10 @@ class System extends Base
         if (!isset($config['app']['input_type'])) {
             $config['app']['input_type'] = 1;
         }
+        // 视频搜索优化,默认frontend
+        if (!isset($config['app']['vod_search_optimise'])) {
+            $config['app']['vod_search_optimise'] = 'frontend';
+        }
         $this->assign('config', $config);
         $this->assign('title', lang('admin/system/config/title'));
         return $this->fetch('admin@system/config');

+ 9 - 0
application/admin/view/system/config.html

@@ -353,6 +353,15 @@
                     <div class="layui-form-mid layui-word-aux">{:lang('admin/system/config/search_rule_tip')}</div>
                 </div>
 
+                <div class="layui-form-item">
+                    <label class="layui-form-label">{:lang('admin/system/config/vod_search_optimise')}:</label>
+                    <div class="layui-input-inline w600">
+                        <input type="checkbox" lay-skin="primary" name="app[vod_search_optimise][]" value="frontend" title="{:lang('admin/system/config/vod_search_optimise/frontend')}" {if condition="strpos($config['app']['vod_search_optimise'],'frontend') !==false"}checked {/if}>
+                        <input type="checkbox" lay-skin="primary" name="app[vod_search_optimise][]" value="collect" title="{:lang('admin/system/config/vod_search_optimise/collect')}" {if condition="strpos($config['app']['vod_search_optimise'],'collect') !==false"}checked {/if}>
+                    </div>
+                    <div class="layui-form-mid layui-word-aux">{:lang('admin/system/config/vod_search_optimise_tip')}</div>
+                </div>
+
                 <div class="layui-form-item">
                     <label class="layui-form-label">{:lang('admin/system/config/copyright_status')}:</label>
                     <div class="layui-input-block">

+ 23 - 14
application/common/model/Collect.php

@@ -315,12 +315,12 @@ class Collect extends Base {
         }
         $html = mac_curl_get($url);
         if(empty($html)){
-            return ['code'=>1001, 'msg'=>lang('model/collect/get_html_err')];
+            return ['code'=>1001, 'msg'=>lang('model/collect/get_html_err') . ', url: ' . $url];
         }
         $html = mac_filter_tags($html);
         $json = json_decode($html,true);
         if(!$json){
-            return ['code'=>1002, 'msg'=>lang('model/collect/json_err') . ': ' . mb_substr($html, 0, 15)];
+            return ['code'=>1002, 'msg'=>lang('model/collect/json_err') . ', url: ' . $url . ', response: ' . mb_substr($html, 0, 15)];
         }
 
         $array_page = [];
@@ -422,6 +422,7 @@ class Collect extends Base {
         $players = config('vodplayer');
         $downers = config('voddowner');
         $vod_search = model('VodSearch');
+        $vod_search_enabled = $vod_search->isCollectEnabled();
 
         $type_list = model('Type')->getCache('type_list');
         $filter_arr = explode(',',$config['filter']);
@@ -561,13 +562,13 @@ class Collect extends Base {
                 if (strpos($config['inrule'], 'e')!==false) {
                     $where['vod_lang'] = $v['vod_lang'];
                 }
-                $vod_search_actor = false;
                 $search_actor_id_list = [];
                 if (strpos($config['inrule'], 'f')!==false) {
-                    // $where['vod_actor'] = ['like', mac_like_arr($v['vod_actor']), 'OR'];
-                    $vod_search_actor = true;
-                    $search_actor_id_list = $vod_search->getResultIdList($v['vod_actor'], 'vod_actor', true);
-                    $search_actor_id_list = empty($search_actor_id_list) ? [0] : $search_actor_id_list;
+                    $where['vod_actor'] = ['like', mac_like_arr($v['vod_actor']), 'OR'];
+                    if ($vod_search_enabled) {
+                        $search_actor_id_list = $vod_search->getResultIdList($v['vod_actor'], 'vod_actor', true);
+                        $search_actor_id_list = empty($search_actor_id_list) ? [0] : $search_actor_id_list;
+                    }
                 }
                 if (strpos($config['inrule'], 'g')!==false) {
                     $where['vod_director'] = $v['vod_director'];
@@ -576,12 +577,17 @@ class Collect extends Base {
                     $v['vod_tag'] = mac_get_tag($v['vod_name'], $v['vod_content']);
                 }
 
-                if($vod_search_actor === true && !empty($where['vod_director'])){
+                if(!empty($where['vod_actor']) && !empty($where['vod_director'])){
                     $blend = true;
                     $GLOBALS['blend'] = [
-                        'vod_id' => ['IN', $search_actor_id_list],
-                        'vod_director' => $where['vod_director']
+                        'vod_actor'    => $where['vod_actor'],
+                        'vod_director' => $where['vod_director'],
                     ];
+                    // 结果太大时,筛选更耗时。仅在结果数量较小时,才加入
+                    $GLOBALS['blend']['vod_id'] = null;
+                    if ($vod_search_enabled && count($search_actor_id_list) < 1000) {
+                        $GLOBALS['blend']['vod_id'] = ['IN', $search_actor_id_list];
+                    }
                     unset($where['vod_actor'],$where['vod_director']);
                 }
 
@@ -677,13 +683,16 @@ class Collect extends Base {
                 else{
                     $info = model('Vod')->where($where)
                         ->where(function($query) {
-                            $query->where('vod_director',$GLOBALS['blend']['vod_director'])
-                                    ->whereOr('vod_id', $GLOBALS['blend']['vod_id']);
+                            $query->where('vod_director',$GLOBALS['blend']['vod_director']);
+                            if (!empty($GLOBALS['blend']['vod_id'])) {
+                                $query->whereOr('vod_id', $GLOBALS['blend']['vod_id']);
+                            } else {
+                                $query->whereOr('vod_actor', $GLOBALS['blend']['vod_actor']);
+                            }
                         })
                         ->find();
                 }
 
-
                 if (!$info) {
                     // 新增
                     if($param['opt'] == 2){
@@ -706,7 +715,7 @@ class Collect extends Base {
                         $v = model('Vod')->formatDataBeforeDb($v);
                         $vod_id = model('Vod')->insert($v, false, true);
                         if ($vod_id > 0) {
-                            $vod_search->checkAndUpdateTopResults(['vod_id' => $vod_id] + $v, true);
+                            $vod_search_enabled && $vod_search->checkAndUpdateTopResults(['vod_id' => $vod_id] + $v, true);
                             $color = 'green';
                             $des = lang('model/collect/add_ok');
                         } else {

+ 54 - 26
application/common/model/Vod.php

@@ -399,32 +399,60 @@ class Vod extends Base {
         }
 
         $vod_search = model('VodSearch');
-        $search_id_list = [];
-        if(!empty($wd)) {
-            $role = 'vod_name';
-            if(!empty($GLOBALS['config']['app']['search_vod_rule'])){
-                $role .= '|'.$GLOBALS['config']['app']['search_vod_rule'];
+        $vod_search_enabled = $vod_search->isFrontendEnabled();
+        if ($vod_search_enabled) {
+            // 开启搜索优化,查询并缓存Id
+            $search_id_list = [];
+            if(!empty($wd)) {
+                $role = 'vod_name';
+                if(!empty($GLOBALS['config']['app']['search_vod_rule'])){
+                    $role .= '|'.$GLOBALS['config']['app']['search_vod_rule'];
+                }
+                $search_id_list += $vod_search->getResultIdList($wd, $role);
+            }
+            if(!empty($name)) {
+                $search_id_list += $vod_search->getResultIdList($name, 'vod_name');
+            }
+            if(!empty($tag)) {
+                $search_id_list += $vod_search->getResultIdList($tag, 'vod_tag', true);
+            }
+            if(!empty($class)) {
+                $search_id_list += $vod_search->getResultIdList($class, 'vod_class', true);
+            }
+            if(!empty($actor)) {
+                $search_id_list += $vod_search->getResultIdList($actor, 'vod_actor', true);
+            }
+            if(!empty($director)) {
+                $search_id_list += $vod_search->getResultIdList($director, 'vod_director', true);
+            }
+            $search_id_list = array_unique($search_id_list);
+            if (!empty($search_id_list)) {
+                $where['_string'] = "vod_id IN (" . join(',', $search_id_list) . ")";
+            }
+        } else {
+            // 不开启搜索优化,使用默认条件
+            if(!empty($wd)) {
+                $role = 'vod_name';
+                if(!empty($GLOBALS['config']['app']['search_vod_rule'])){
+                    $role .= '|'.$GLOBALS['config']['app']['search_vod_rule'];
+                }
+                $where[$role] = ['like', '%' . $wd . '%'];
+            }
+            if(!empty($name)) {
+                $where['vod_name'] = ['like',mac_like_arr($name),'OR'];
+            }
+            if(!empty($tag)) {
+                $where['vod_tag'] = ['like',mac_like_arr($tag),'OR'];
+            }
+            if(!empty($class)) {
+                $where['vod_class'] = ['like',mac_like_arr($class), 'OR'];
+            }
+            if(!empty($actor)) {
+                $where['vod_actor'] = ['like', mac_like_arr($actor), 'OR'];
+            }
+            if(!empty($director)) {
+                $where['vod_director'] = ['like',mac_like_arr($director),'OR'];
             }
-            $search_id_list += $vod_search->getResultIdList($wd, $role);
-        }
-        if(!empty($name)) {
-            $search_id_list += $vod_search->getResultIdList($name, 'vod_name');
-        }
-        if(!empty($tag)) {
-            $search_id_list += $vod_search->getResultIdList($tag, 'vod_tag', true);
-        }
-        if(!empty($class)) {
-            $search_id_list += $vod_search->getResultIdList($class, 'vod_class', true);
-        }
-        if(!empty($actor)) {
-            $search_id_list += $vod_search->getResultIdList($actor, 'vod_actor', true);
-        }
-        if(!empty($director)) {
-            $search_id_list += $vod_search->getResultIdList($director, 'vod_director', true);
-        }
-        $search_id_list = array_unique($search_id_list);
-        if (!empty($search_id_list)) {
-            $where['_string'] = "vod_id IN (" . join(',', $search_id_list) . ")";
         }
         if(in_array($plot,['0','1'])){
             $where['vod_plot'] = $plot;
@@ -639,7 +667,7 @@ class Vod extends Base {
             $data['vod_time_add'] = time();
             $data['vod_time'] = time();
             $res = $this->allowField(true)->insert($data, false, true);
-            if ($res > 0) {
+            if ($res > 0 && model('VodSearch')->isFrontendEnabled()) {
                 model('VodSearch')->checkAndUpdateTopResults(['vod_id' => $res] + $data);
             }
         }

+ 30 - 2
application/common/model/VodSearch.php

@@ -7,8 +7,8 @@ use think\Cache;
 class VodSearch extends Base {
     // 设置数据表(不含前缀)
     protected $name = 'vod_search';
-    private $resultCacheDays = 30;
-    private $updateTopCount  = 100000;
+    private $resultCacheDays = 14;
+    private $updateTopCount  = 50000;
 
     /**
      * 获取结果Id列表
@@ -61,6 +61,34 @@ class VodSearch extends Base {
         return $id_list;
     }
 
+    /**
+     * 前端是否开启
+     */
+    public function isFrontendEnabled()
+    {
+        $config = config('maccms');
+        // 未设置时,默认打开
+        if (!isset($config['app']['vod_search_optimise'])) {
+            return true;
+        }
+        $list = explode('|', $config['app']['vod_search_optimise']);
+        return in_array('frontend', $list);
+    }
+
+    /**
+     * 采集是否开启
+     */
+    public function isCollectEnabled()
+    {
+        $config = config('maccms');
+        // 未设置时,默认关闭
+        if (!isset($config['app']['vod_search_optimise'])) {
+            return false;
+        }
+        $list = explode('|', $config['app']['vod_search_optimise']);
+        return in_array('collect', $list);
+    }
+
     /**
      * 检查更新搜索结果
      */

+ 4 - 0
application/lang/zh-cn.php

@@ -901,6 +901,10 @@ https://www.baidu.com/123.jpg
     'admin/system/config/search_vod_rule'=>'视频搜索规则',
     'admin/system/config/search_rule_tip'=>'注意,仅影响wd参数,勾选过多影响性能,建议3个以内',
     'admin/system/config/search_art_rule'=>'文章搜索规则',
+    'admin/system/config/vod_search_optimise'=>'视频搜索优化',
+    'admin/system/config/vod_search_optimise/frontend'=>'前台',
+    'admin/system/config/vod_search_optimise/collect'=>'采集',
+    'admin/system/config/vod_search_optimise_tip'=>'缓存LIKE模糊查询的结果',
     'admin/system/config/copyright_status'=>'版权提示',
     'admin/system/config/copyright_msg'=>'提示信息',
     'admin/system/config/copyright_jump_detail'=>'内容页跳转',

+ 4 - 0
application/lang/zh-tw.php

@@ -901,6 +901,10 @@ https://www.baidu.com/123.jpg
     'admin/system/config/search_vod_rule'=>'視頻搜索規則',
     'admin/system/config/search_rule_tip'=>'註意,僅影響wd參數,勾選過多影響性能,建議3個以內',
     'admin/system/config/search_art_rule'=>'文章搜索規則',
+    'admin/system/config/vod_search_optimise'=>'視頻搜索優化',
+    'admin/system/config/vod_search_optimise/frontend'=>'前臺',
+    'admin/system/config/vod_search_optimise/collect'=>'採集',
+    'admin/system/config/vod_search_optimise_tip'=>'緩存LIKE模糊查詢的結果',
     'admin/system/config/copyright_status'=>'版權提示',
     'admin/system/config/copyright_msg'=>'提示信息',
     'admin/system/config/copyright_jump_detail'=>'內容頁跳轉',