From 1759f4abfc607fac5fbbe226916baec7d4e10964 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Mon, 18 Jul 2022 15:13:03 +0800 Subject: [PATCH] cache rss + redis require 2.0 --- README-EN.md | 2 +- README.md | 2 +- .../Commands/{BackuAll.php => BackupAll.php} | 2 +- app/Console/Commands/NexusUpdate.php | 5 ++ app/Repositories/ExamRepository.php | 2 +- app/Repositories/UserRepository.php | 2 +- nexus/Install/Install.php | 10 ++++ nexus/Install/install/install.php | 10 +++- nexus/Install/update/update.php | 10 +++- public/torrentrss.php | 46 ++++++++++++------- 10 files changed, 67 insertions(+), 24 deletions(-) rename app/Console/Commands/{BackuAll.php => BackupAll.php} (97%) diff --git a/README-EN.md b/README-EN.md index 26ea0ac1..faf6e649 100644 --- a/README-EN.md +++ b/README-EN.md @@ -28,7 +28,7 @@ Complete PT website building solution. Based on NexusPHP + Laravel + Filament. ## System Requirements - PHP: 8.0, must have extensions: bcmath, ctype, curl, fileinfo, json, mbstring, openssl, pdo_mysql, tokenizer, xml, mysqli, gd, redis, pcntl, sockets, posix - Mysql: 5.7 latest version or above -- Redis:1.0.0 or above +- Redis:2.0.0 or above ## Quick Start Install docker. diff --git a/README.md b/README.md index 41ad6857..fa90400e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ ## 系统要求 - PHP: 8.0,必须扩展:bcmath, ctype, curl, fileinfo, json, mbstring, openssl, pdo_mysql, tokenizer, xml, mysqli, gd, redis, pcntl, sockets, posix - Mysql: 5.7最新版或以上版本 -- Redis:1.0.0或以上版本 +- Redis:2.0.0或以上版本 ## 快速开始 安装 docker。 diff --git a/app/Console/Commands/BackuAll.php b/app/Console/Commands/BackupAll.php similarity index 97% rename from app/Console/Commands/BackuAll.php rename to app/Console/Commands/BackupAll.php index bc2b5436..f268a4ce 100644 --- a/app/Console/Commands/BackuAll.php +++ b/app/Console/Commands/BackupAll.php @@ -5,7 +5,7 @@ namespace App\Console\Commands; use App\Repositories\ToolRepository; use Illuminate\Console\Command; -class BackuAll extends Command +class BackupAll extends Command { /** * The name and signature of the console command. diff --git a/app/Console/Commands/NexusUpdate.php b/app/Console/Commands/NexusUpdate.php index 1a6b44ac..ced9247d 100644 --- a/app/Console/Commands/NexusUpdate.php +++ b/app/Console/Commands/NexusUpdate.php @@ -91,6 +91,7 @@ class NexusUpdate extends Command $symbolicLinks = $settingTableRows['symbolic_links']; $fails = $settingTableRows['fails']; $mysqlInfo = $this->update->getMysqlVersionInfo(); + $redisInfo = $this->update->getRedisVersionInfo(); if (!empty($fails)) { foreach ($fails as $value) { @@ -102,6 +103,10 @@ class NexusUpdate extends Command $this->doLog("Error: MySQL version: {$mysqlInfo['version']} is too low, please use the newest version of 5.7 or above.", 'error'); return 0; } + if (!$redisInfo['match']) { + $this->doLog("Error: Redis version: {$mysqlInfo['version']} is too low, please use 2.0.0 or above.", 'error'); + return 0; + } $this->doLog("going to createSymbolicLinks..."); $this->update->createSymbolicLinks($symbolicLinks); $this->doLog("createSymbolicLinks done!"); diff --git a/app/Repositories/ExamRepository.php b/app/Repositories/ExamRepository.php index e14d619a..1844be26 100644 --- a/app/Repositories/ExamRepository.php +++ b/app/Repositories/ExamRepository.php @@ -293,7 +293,7 @@ class ExamRepository extends BaseRepository $logPrefix = "uid: $uid, examId: $examId, begin: $begin, end: $end"; $exam = Exam::query()->find($examId); $user = User::query()->findOrFail($uid); - if (Auth::user()->Class <= $user->class) { + if (Auth::user()->class <= $user->class) { throw new NexusException("No permission !"); } if (!$this->isExamMatchUser($exam, $user)) { diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index 47973fa1..be1180ce 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -233,7 +233,7 @@ class UserRepository extends BaseRepository } $sourceField = $fieldMap[$field]; $targetUser = User::query()->findOrFail($uid, User::$commonFields); - if (Auth::user()->Class <= $targetUser->class) { + if (Auth::user()->class <= $targetUser->class) { throw new NexusException("No permission !"); } $old = $targetUser->{$sourceField}; diff --git a/nexus/Install/Install.php b/nexus/Install/Install.php index 52759ace..df6cd8db 100644 --- a/nexus/Install/Install.php +++ b/nexus/Install/Install.php @@ -663,6 +663,16 @@ class Install return compact('version', 'match'); } + public function getRedisVersionInfo(): array + { + global $Cache; + $redis = $Cache->getRedis(); + $result = $redis->info(); + $version = $result['redis_version']; + $match = version_compare($version, '2.0.0', '>='); + return compact('version', 'match'); + } + public function checkLock() { $fullFilename = ROOT_PATH . $this->lockFile; diff --git a/nexus/Install/install/install.php b/nexus/Install/install/install.php index 608d5a9d..30ace48a 100644 --- a/nexus/Install/install/install.php +++ b/nexus/Install/install/install.php @@ -104,6 +104,7 @@ if ($currentStep == 4) { $tableRows = $settingTableRows['table_rows']; $pass = $settingTableRows['pass']; $mysqlInfo = $install->getMysqlVersionInfo(); + $redisInfo = $install->getREdisVersionInfo(); while ($isPost) { set_time_limit(300); try { @@ -136,7 +137,11 @@ if ($currentStep == 5) { ]; } -if (!empty($error) || (isset($mysqlInfo) && !$mysqlInfo['match'])) { +if ( + !empty($error) + || (isset($mysqlInfo) && !$mysqlInfo['match']) + || (isset($redisInfo) && !$redisInfo['match']) +) { $pass = false; } ?> @@ -184,6 +189,9 @@ if (!empty($error) || (isset($mysqlInfo) && !$mysqlInfo['match'])) { if (!$mysqlInfo['match']) { echo sprintf('
MySQL version: %s is too low, please use the newest version of 5.7 or above.
', $mysqlInfo['version']); } + if (!$redisInfo['match']) { + echo sprintf('
Redis version: %s is too low, please use 2.0.0 or above.
', $redisInfo['version']); + } } elseif ($currentStep == 5) { echo $install->renderForm($userFormControls, '1/2', '1/4', '3/4'); } elseif ($currentStep > $maxStep) { diff --git a/nexus/Install/update/update.php b/nexus/Install/update/update.php index c249d1d2..d320da87 100644 --- a/nexus/Install/update/update.php +++ b/nexus/Install/update/update.php @@ -157,6 +157,7 @@ if ($currentStep == 4) { $tableRows = $settingTableRows['table_rows']; $pass = $settingTableRows['pass']; $mysqlInfo = $update->getMysqlVersionInfo(); + $redisInfo = $update->getREdisVersionInfo(); while ($isPost) { set_time_limit(300); try { @@ -175,7 +176,11 @@ if ($currentStep == 4) { } } -if (!empty($error) || (isset($mysqlInfo) && !$mysqlInfo['match'])) { +if ( + !empty($error) + || (isset($mysqlInfo) && !$mysqlInfo['match']) + || (isset($redisInfo) && !$redisInfo['match']) +) { $pass = false; } ?> @@ -224,6 +229,9 @@ if (!empty($error) || (isset($mysqlInfo) && !$mysqlInfo['match'])) { if (!$mysqlInfo['match']) { echo sprintf('
MySQL version: %s is too low, please use the newest version of 5.7 or above.
', $mysqlInfo['version']); } + if (!$redisInfo['match']) { + echo sprintf('
Redis version: %s is too low, please use 2.0.0 or above.
', $redisInfo['version']); + } } elseif ($currentStep > $maxStep) { echo '
Congratulations, everything is ready!
'; echo '
For questions, consult the upgrade log at: ' . $update->getLogFile() . '
'; diff --git a/public/torrentrss.php b/public/torrentrss.php index cd549b8a..c8e3482d 100644 --- a/public/torrentrss.php +++ b/public/torrentrss.php @@ -1,5 +1,17 @@ getRedis(); +$cacheData = $redis->hGet($cacheKey, $hashKey); +if ($cacheData) { + $cacheData = unserialize($cacheData); + if (isset($cacheData['deadline']) && $cacheData['deadline'] > time()) { + do_log("rss get from cache"); + header ("Content-type: text/xml"); + die($cacheData['data']); + } +} dbconn(); function hex_esc($matches) { return sprintf("%02x", ord($matches[0])); @@ -117,15 +129,12 @@ $url = get_protocol_prefix().$BASEURL; $year = substr($datefounded, 0, 4); $yearfounded = ($year ? $year : 2007); $copyright = "Copyright (c) ".$SITENAME." ".(date("Y") != $yearfounded ? $yearfounded."-" : "").date("Y").", all rights reserved"; -header ("Content-type: text/xml"); -print(""); +$xml = ""; //The commented version passed feed validator at http://www.feedvalidator.org /*print(' ');*/ -print(' -'); -print(' - +$xml .= ''; +$xml .= ' ' . addslashes($SITENAME.' Torrents'). ' @@ -144,11 +153,11 @@ print(' 100 100 ' . addslashes($SITENAME.' Torrents') . ' - '); + '; /*print(' ');*/ -print(' -'); +//print(' +//'); foreach ($list as $row) { $title = ""; @@ -165,22 +174,25 @@ foreach ($list as $row) if (!empty($_GET['isize'])) $title .= "[".mksize($row['size'])."]"; if (!empty($_GET['iuplder'])) $title .= "[".$author."]"; $content = format_comment($row['descr'], true, false, false, false); - print(' + $xml .= ' <![CDATA['.$title.']]> '.$itemurl.' -'); +'; //print(' '.$author.''); -print(' '.$author.'@'.$_SERVER['HTTP_HOST'].' ('.$author.')'); -print(' - '.$row['cat_name'].' +$xml .= ''.$author.'@'.$_SERVER['HTTP_HOST'].' ('.$author.')'; +$xml .= ''.$row['cat_name'].' '.preg_replace_callback('/./s', 'hex_esc', hash_pad($row['info_hash'])).' '.date('r',strtotime($row['added'])).' -'); +'; } -print(' -'); +$xml .= ' +'; +do_log("rss cache generated"); +$redis->hSet($cacheKey, $hashKey, serialize(['data' => $xml, 'deadline' => time() + 300])); +header ("Content-type: text/xml"); +echo $xml; ?>