From 74fcb93f97fbd18824c2ac81cd06ec15e2e2a960 Mon Sep 17 00:00:00 2001 From: m1saka <1287708285@qq.com> Date: Sat, 16 Jul 2022 22:59:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20redis=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pulling up a nice pile of shit on a high shit mountain (#36) --- config.php | 8 +- utils/fuck_search.php | 23 ++-- utils/functions.php | 2 +- utils/functions_cache.php | 232 +++++++++++++++++++++++++++----------- utils/redis-class.php | 98 ++++++++++++++++ 5 files changed, 290 insertions(+), 73 deletions(-) create mode 100644 utils/redis-class.php diff --git a/config.php b/config.php index 479b1a6..eed595e 100644 --- a/config.php +++ b/config.php @@ -33,7 +33,7 @@ define('BAN_CID', 0); // 是否开启ban特定视频 $cid_list = array(); // 缓存 -define('SAVE_CACHE', 0); // 开启缓存,须配置MySQL数据库。0 否, 1 是 +define('SAVE_CACHE', 0); // 开启缓存,须配置MySQL数据库(必须)及redis(可选)。0 否, 1 是 define('CACHE_TIME', 60*60*0.5); // Playurl缓存时长(秒),目前最长可到14400秒,建议7200秒以下(部分视频只有2小时) define('CACHE_TIME_SEASON', 60*60*0.5); // 泰国 season 的缓存时长(秒) // 发生错误时的缓存时长 @@ -51,6 +51,12 @@ define('DB_USER', '这里改成登录的用户名'); // 登录的用户名 define('DB_PASS', '登录的密码'); // 登录的密码 define('DB_NAME', '数据库名称'); // 数据库名称 +// redis数据库 +define('REDIS_ON', 0); // 是否开启redis缓存,0 否, 1 是 不开启缓存时,此开关无效 +define('REDIS_HOST', 'localhost'); // redis服务器地址 +define('REDIS_PORT', 6379); // redis服务器端口 +define('REDIS_PASS', ''); // redis服务器密码 + // 服务器所在的地区 /* 可不填,填写后可以锁区。 diff --git a/utils/fuck_search.php b/utils/fuck_search.php index fcc6848..ec5af2a 100644 --- a/utils/fuck_search.php +++ b/utils/fuck_search.php @@ -28,13 +28,22 @@ // 开始替换 $array = json_decode($output, true); - @$trackid = $array['data']['trackid']; - @$exp_str = $array['data']['exp_str']; - @$total = $array['data']['total']; - @$pages = $array['data']['pages']; - @$items_old = $array['data']['items']; - $items_new= trim(json_encode($items_old, 320),'[]'); - $items = str_replace('["追番","已追番"]','{"0":"追番","1":"已追番"}',$items_new); + if (is_array($array)) { + @$trackid = $array['data']['trackid']; + @$exp_str = $array['data']['exp_str']; + @$total = $array['data']['total']; + @$pages = $array['data']['pages']; + @$items_old = $array['data']['items']; + $items_new= trim(json_encode($items_old, 320),'[]'); + $items = str_replace('["追番","已追番"]','{"0":"追番","1":"已追番"}',$items_new); + } else { + $trackid = ""; + $exp_str = ""; + $total = ""; + $pages = ""; + $items_old = ""; + $items = ""; + } $items0 = '{"title":"'.$set_title.'","cover":"'.$set_cover.'","uri":"'.$set_uri.'","param":"1","goto":"bangumi","ptime":1500000000,"season_id":1,"season_type":1,"season_type_name":"番剧","media_type":1,"style":"'.$set_style.'","styles":"'.$set_style.'","cv":"","rating":'.$set_rating.',"vote":'.$set_vote.',"area":"漫游","staff":"无","is_selection":1,"badge":"公告","episodes":[{"position":1,"uri":"https://www.bilibili.com/video/av928861104","param":"1","index":"1"}],"label":"'.$set_label.'","watch_button":{"title":"'.$set_watch_button_title.'","link":"'.$set_watch_button_link.'"},"follow_button":{"icon":"http://i0.hdslb.com/bfs/bangumi/154b6898d2b2c20c21ccef9e41fcf809b518ebb4.png","texts":{"0":"'.$set_follow_button_title.'","1":"'.$set_unfollow_button_title.'"},"status_report":"bangumi"},"selection_style":"'.$set_selection_style.'","episodes_new":['.$set_episodes.'],"badges":[{"text":"'.$set_badges.'","text_color":"#FFFFFF","text_color_night":"#E5E5E5","bg_color":"#00C0FF","bg_color_night":"#0B91BE","bg_style":1}]}'; if (!$items_old) { $items = ''; diff --git a/utils/functions.php b/utils/functions.php index 78182af..1e8b7df 100644 --- a/utils/functions.php +++ b/utils/functions.php @@ -222,7 +222,7 @@ function check_sign($appkey, $sign, $query) { // 按 key 排序 ksort($query_arr); $query_new = http_build_query($query_arr); - if ($sign != md5($query_new.$appsec)) { + if ($sign != md5($query_new.$appsec)) { block(41, "参数sign错误"); } } diff --git a/utils/functions_cache.php b/utils/functions_cache.php index c4cebdf..4baf7d3 100644 --- a/utils/functions_cache.php +++ b/utils/functions_cache.php @@ -15,6 +15,14 @@ try { //echo $e; echo '数据库连接失敗'; } +if (REDIS_ON) { + include 'redis-class.php'; + try { + $redisConn = new redisFunc(REDIS_HOST,REDIS_PORT,REDIS_PASS); + } catch(Exception $e) { + echo 'redis连接失敗'; + } +} // 参数(判断是否刷新缓存) $refresh_cache = 0; @@ -78,14 +86,27 @@ function get_cache() { global $member_type; global $cache_type; global $refresh_cache; - $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".AREA."' AND `type` = '".$member_type."' AND `cache_type` = '".$cache_type."' AND `cid` = '".CID."' AND `ep_id` = '".EP_ID."'"; - $cres = $dbh -> query($sqlco); - $vnum = $cres -> fetch(); - if (!$vnum){ - return ""; + if (REDIS_ON) { + $redisConn = new redisFunc(REDIS_HOST,REDIS_PORT,REDIS_PASS); + if ($redisConn->check(AREA.'-'.$member_type.'-'.$cache_type.'-'.CID.'-'.EP_ID)) { + @$cache = $redisConn->get(AREA.'-'.$member_type.'-'.$cache_type.'-'.CID.'-'.EP_ID); + @$expired_time = $redisConn->ttl(AREA.'-'.$member_type.'-'.$cache_type.'-'.CID.'-'.EP_ID)+time(); + } else { + $cache = ""; + } + if (!$cache) { + return ""; + } + } else { + $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".AREA."' AND `type` = '".$member_type."' AND `cache_type` = '".$cache_type."' AND `cid` = '".CID."' AND `ep_id` = '".EP_ID."'"; + $cres = $dbh -> query($sqlco); + $vnum = $cres -> fetch(); + if (!$vnum){ + return ""; + } + @$cache = $vnum['cache']; + @$expired_time = $vnum['expired_time']; } - @$cache = $vnum['cache']; - @$expired_time = $vnum['expired_time']; //修复读取问题 $cache = str_replace("u0026", "&", $cache); $cache = str_replace("\r", "\\r", $cache); @@ -115,7 +136,11 @@ function write_cache() { global $refresh_cache; $ts = time(); $array = json_decode($output, true); - $code = $array['code']; + if (is_array($array)) { + $code = $array['code']; + } else { + $code = -404; + } switch ($code) { case "0": // 删掉用户mid @@ -146,13 +171,18 @@ function write_cache() { $ts = $ts + CACHE_TIME_OTHER; } if ($output !== "") { //没有获取到输出内容不写入缓存 - $sql = "INSERT INTO `cache` (`expired_time`,`area`,`type`,`cache_type`,`cid`,`ep_id`,`cache`) VALUES ('".$ts."','".AREA."','".$member_type."','".$cache_type."','".CID."','".EP_ID."','".$output."')"; - // 刷新缓存 - if ($refresh_cache == 1) { - $sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$output."' WHERE `area` = '".AREA."' AND `type` = '".$member_type."' AND `cache_type` = '".$cache_type."' AND `cid` = '".CID."' AND `ep_id` = '".EP_ID."';"; + if (REDIS_ON) { + $redisConn = new redisFunc(REDIS_HOST,REDIS_PORT,REDIS_PASS); + $redisConn->add(AREA.'-'.$member_type.'-'.$cache_type.'-'.CID.'-'.EP_ID, $output, $ts); + } else { + $sql = "INSERT INTO `cache` (`expired_time`,`area`,`type`,`cache_type`,`cid`,`ep_id`,`cache`) VALUES ('".$ts."','".AREA."','".$member_type."','".$cache_type."','".CID."','".EP_ID."','".$output."')"; + // 刷新缓存 + if ($refresh_cache == 1) { + $sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$output."' WHERE `area` = '".AREA."' AND `type` = '".$member_type."' AND `cache_type` = '".$cache_type."' AND `cid` = '".CID."' AND `ep_id` = '".EP_ID."';"; + } + $dbh -> exec($sql); } - $dbh -> exec($sql); - } + } } // 获取season缓存 @@ -161,46 +191,91 @@ function get_cache_season() { global $member_type; global $cache_type; global $refresh_cache_season; - + if (AREA == "th") { $area = "th"; //泰区 } else { $area = "main"; //主站 } - if (EP_ID != "") { - $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".$area."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '0' AND `ep_id` = '".EP_ID."'"; - } elseif (SS_ID != "") { - $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".$area."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".SS_ID."' AND `ep_id` = '0'"; - } else { - return ""; - } - $cres = $dbh -> query($sqlco); - $vnum = $cres -> fetch(); - if (!$vnum) { - //给主站的一次机会获取自身 AREA 可能 code!=0 的缓存 - if ($area == "main") { - if (EP_ID != "") { - $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".AREA."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '0' AND `ep_id` = '".EP_ID."'"; - } elseif (SS_ID != "") { - $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".AREA."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".SS_ID."' AND `ep_id` = '0'"; + if (REDIS_ON) { + $redisConn = new redisFunc(REDIS_HOST,REDIS_PORT,REDIS_PASS); + if (EP_ID != "") { + if ($redisConn->check($area.'-season-'.$cache_type.'-0-'.EP_ID)){ + @$cache = $redisConn->get($area.'-season-'.$cache_type.'-0-'.EP_ID); + @$expired_time = $redisConn->ttl($area.'-season-'.$cache_type.'-0-'.EP_ID)+time(); + } else { + @$cache = ""; + @$expired_time = time(); + } + } elseif (SS_ID != "") { + if ($redisConn->check($area.'-season-'.$cache_type.'-0-'.EP_ID)){ + @$cache = $redisConn->get($area.'-season-'.$cache_type.'-'.SS_ID.'-0'); + @$expired_time = $redisConn->ttl($area.'-season-'.$cache_type.'-'.SS_ID.'-0')+time(); + } else { + @$cache = ""; + @$expired_time = time(); } - $cres = $dbh -> query($sqlco); - $vnum2 = $cres -> fetch(); } - } - @$cache1 = $vnum['cache']; - @$expired_time1 = $vnum['expired_time']; - @$cache2 = $vnum2['cache']; - @$expired_time2 = $vnum2['expired_time']; - - if ($cache1 != "") { - $cache = $cache1; - $expired_time = $expired_time1; - } elseif ($cache2 != "") { - $cache = $cache2; - $expired_time = $expired_time2; + if (!$cache) { + if ($area == "main") { + if (EP_ID != "") { + if ($redisConn->check($area.'-season-'.$cache_type.'-0-'.EP_ID)){ + @$cache = $redisConn->get($area.'-season-'.$cache_type.'-0-'.EP_ID); + @$expired_time = $redisConn->ttl($area.'-season-'.$cache_type.'-0-'.EP_ID)+time(); + } else { + @$cache = ""; + @$expired_time = time(); + } + } elseif (SS_ID != "") { + if ($redisConn->check($area.'-season-'.$cache_type.'-0-'.EP_ID)){ + @$cache = $redisConn->get(AREA.'-season-'.$cache_type.'-'.SS_ID.'-0'); + @$expired_time = $redisConn->ttl(AREA.'-season-'.$cache_type.'-'.SS_ID.'-0')+time(); + } else { + @$cache = ""; + @$expired_time = time(); + } + } + } + } + if (!$cache) { + return ""; + } } else { - return ""; + if (EP_ID != "") { + $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".$area."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '0' AND `ep_id` = '".EP_ID."'"; + } elseif (SS_ID != "") { + $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".$area."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".SS_ID."' AND `ep_id` = '0'"; + } else { + return ""; + } + $cres = $dbh -> query($sqlco); + $vnum = $cres -> fetch(); + if (!$vnum) { + //给主站的一次机会获取自身 AREA 可能 code!=0 的缓存 + if ($area == "main") { + if (EP_ID != "") { + $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".AREA."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '0' AND `ep_id` = '".EP_ID."'"; + } elseif (SS_ID != "") { + $sqlco = "SELECT `cache`,`expired_time` FROM `cache` WHERE `area` = '".AREA."' AND `type` = '0' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".SS_ID."' AND `ep_id` = '0'"; + } + $cres = $dbh -> query($sqlco); + $vnum2 = $cres -> fetch(); + } + } + @$cache1 = $vnum['cache']; + @$expired_time1 = $vnum['expired_time']; + @$cache2 = $vnum2['cache']; + @$expired_time2 = $vnum2['expired_time']; + + if ($cache1 != "") { + $cache = $cache1; + $expired_time = $expired_time1; + } elseif ($cache2 != "") { + $cache = $cache2; + $expired_time = $expired_time2; + } else { + return ""; + } } if (time() <= (int)$expired_time) { //修复读取问题 @@ -221,10 +296,14 @@ function write_cache_season() { global $output; global $cache_type; global $refresh_cache_season; - + $ts = time(); $array = json_decode($output, true); - $code = $array['code']; + if (is_array($array)) { + $code = $array['code']; + } else { + $code = -404; + } switch ($code) { case "0": $ts = $ts + CACHE_TIME_SEASON; @@ -259,29 +338,48 @@ function write_cache_season() { // 修复转义问题 $output = str_replace("\\", "\\\\", $output); // 当 code==0 缓存成 area=main - $sql = "INSERT INTO `cache` (`expired_time`,`area`,`type`,`cache_type`,`cid`,`ep_id`,`cache`) VALUES ('".$ts."','".$area."','0','season_".$cache_type."','".$ss_id."','".$ep_id."','".$output."')"; - // 刷新缓存 - if ($refresh_cache_season == 1) { - $sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$output."' WHERE `area` = '".$area."' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".$ss_id."' AND `ep_id` = '".$ep_id."';"; + if (REDIS_ON) { + $redisConn = new redisFunc(REDIS_HOST,REDIS_PORT,REDIS_PASS); + $redisConn->add($area.'-season-'.$cache_type.'-'.$ss_id.'-'.$ep_id, $output, $ts); + } else { + $sql = "INSERT INTO `cache` (`expired_time`,`area`,`type`,`cache_type`,`cid`,`ep_id`,`cache`) VALUES ('".$ts."','".$area."','0','season_".$cache_type."','".$ss_id."','".$ep_id."','".$output."')"; + // 刷新缓存 + if ($refresh_cache_season == 1) { + $sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$output."' WHERE `area` = '".$area."' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".$ss_id."' AND `ep_id` = '".$ep_id."';"; + } + $dbh -> exec($sql); } } elseif ($code !== "") { // 修复转义问题 $output = str_replace("\\", "\\\\", $output); // 缓存到自身 AREA 里面 - $sql = "INSERT INTO `cache` (`expired_time`,`area`,`type`,`cache_type`,`cid`,`ep_id`,`cache`) VALUES ('".$ts."','".AREA."','0','season_".$cache_type."','".$ss_id."','".$ep_id."','".$output."')"; - // 刷新缓存 - if ($refresh_cache_season == 1) { - $sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$output."' WHERE `area` = '".AREA."' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".$ss_id."' AND `ep_id` = '".$ep_id."';"; + if (REDIS_ON) { + $redisConn = new redisFunc(REDIS_HOST,REDIS_PORT,REDIS_PASS); + $redisConn->add($area.'-season-'.$cache_type.'-'.$ss_id.'-'.$ep_id, $output, $ts); + } else { + $sql = "INSERT INTO `cache` (`expired_time`,`area`,`type`,`cache_type`,`cid`,`ep_id`,`cache`) VALUES ('".$ts."','".AREA."','0','season_".$cache_type."','".$ss_id."','".$ep_id."','".$output."')"; + // 刷新缓存 + if ($refresh_cache_season == 1) { + $sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$output."' WHERE `area` = '".AREA."' AND `cache_type` = 'season_".$cache_type."' AND `cid` = '".$ss_id."' AND `ep_id` = '".$ep_id."';"; + } } } - $dbh -> exec($sql); // 缓存泰区字幕 if (AREA == "th") { $array = json_decode($output, true); - $code = $array['code']; + if (is_array($array)) { + $code = $array['code']; + } else { + $code = -404; + } if (($code == "0" || $code == 0) && isset($array['result']['modules'][0]['data']['episodes'])) { - $ss_id = $array['result']['season_id']; - $items = $array['result']['modules'][0]['data']['episodes']; + if (is_array($array)) { + $ss_id = $array['result']['season_id']; + $items = $array['result']['modules'][0]['data']['episodes']; + } else { + $ss_id = ""; + $items = ""; + } for ($i=0; $i exec($sql); - } } } @@ -322,7 +423,7 @@ function get_cache_subtitle() { global $dbh; global $cache_type; global $refresh_cache_subtitle; - + if (AREA == "th") { $area = "th"; //泰区 } else { @@ -341,7 +442,7 @@ function get_cache_subtitle() { @$cache = $vnum['cache']; @$expired_time = $vnum['expired_time']; @$ss_id = $vnum['cid']; - + if ($cache != "") { if (time() <= (int)$expired_time) { //修复读取问题 @@ -471,6 +572,9 @@ function read_status($area){ $sqlco = "SELECT `code` FROM `status_code` WHERE `area` = '".$area."'"; $result = $dbh -> query($sqlco); $code = $result -> fetch(); + if ($code == false) { + return 0; + } return $code['code']; } else { return 0; diff --git a/utils/redis-class.php b/utils/redis-class.php new file mode 100644 index 0000000..b9d7809 --- /dev/null +++ b/utils/redis-class.php @@ -0,0 +1,98 @@ +SERVER = $SERVER; + $this->PORT = $PORT; + $this->PASSWORD = $PASSWORD; + } + public function ping(){ + $redis = new Redis(); + try { + $redis->connect($this->SERVER,$this->PORT); + if($this->PASSWORD != ""){ + $redis->auth($this->PASSWORD); + } + } catch(Exception $e) { + return false; + } + if($redis->ping()){ + return true; + }else{ + return false; + } + } + public function check($key){ + $redis = new Redis(); + try { + $redis->connect($this->SERVER,$this->PORT); + if($this->PASSWORD != ""){ + $redis->auth($this->PASSWORD); + } + } catch(Exception $e) { + return false; + } + if($redis->exists($key)){ + return true; + }else{ + return false; + } + } + public function add($key,$value,$expire){ + $redis = new Redis(); + $time = $expire; + try { + $redis->connect($this->SERVER,$this->PORT); + if($this->PASSWORD != ""){ + $redis->auth($this->PASSWORD); + } + $redis->set($key,$value); + $redis->expireAt($key,$time); + } catch(Exception $e) { + return false; + } + return true; + } + public function get($key){ + $redis = new Redis(); + try { + $redis->connect($this->SERVER,$this->PORT); + if($this->PASSWORD != ""){ + $redis->auth($this->PASSWORD); + } + $value = $redis->get($key); + } catch(Exception $e) { + return null; + } + return $value; + } + public function del($key){ + $redis = new Redis(); + try { + $redis->connect($this->SERVER,$this->PORT); + if($this->PASSWORD != ""){ + $redis->auth($this->PASSWORD); + } + $redis->del($key); + } catch(Exception $e) { + return false; + } + return true; + } + public function ttl($key){ + $redis = new Redis(); + try { + $redis->connect($this->SERVER,$this->PORT); + if($this->PASSWORD != ""){ + $redis->auth($this->PASSWORD); + } + $ttl = $redis->ttl($key); + } catch(Exception $e) { + return -3; + } + return $ttl; + } + }; +?>