支持 redis 数据库

Pulling up a nice pile of shit on a high shit mountain (#36)
pull/37/head
m1saka 2 years ago committed by GitHub
parent 4cf564b6ea
commit 74fcb93f97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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服务器密码
// 服务器所在的地区
/*
可不填,填写后可以锁区。

@ -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 = '';

@ -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错误");
}
}

@ -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<count($items); $i++) {
$ep_id = $items[$i]['id'];
$sqlco = "SELECT `expired_time`,`cid` FROM `cache` WHERE `area` = '".$area."' AND `cache_type` = 'subtitle_".$cache_type."' AND `ep_id` = '".$ep_id."'";
@ -296,7 +394,11 @@ function write_cache_season() {
} else {
$refresh_cache_subtitle = 0; // INSERT
}
$sub_arr = $array['result']['modules'][0]['data']['episodes'][$i]['subtitles'];
if (is_array($array)) {
$sub_arr = $array['result']['modules'][0]['data']['episodes'][$i]['subtitles'];
} else {
$sub_arr = array();
}
$sub_count = count($sub_arr);
$sub_init = '{"code":0,"message":"0","ttl":1,"data":{"suggest_key":"en","subtitles":null}}';
$sub_json = json_decode($sub_init, true);
@ -311,7 +413,6 @@ function write_cache_season() {
$sql = "UPDATE `cache` SET `expired_time` = '".$ts."', `cache` = '".$sub."', `cid` = '".$ss_id."' WHERE `area` = '".AREA."' AND `cache_type` = 'subtitle_".$cache_type."' AND `ep_id` = '".$ep_id."';";
}
$dbh -> 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;

@ -0,0 +1,98 @@
<?php
class redisFunc{
private $SERVER = "localhost";
private $PORT = "6379";
private $PASSWORD = "";
public function __construct($SERVER,$PORT,$PASSWORD){
$this->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;
}
};
?>
Loading…
Cancel
Save