From 8a4f4a77cb55174623e43c80982d23aa0dcf38d6 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 29 Jul 2022 03:34:55 +0800 Subject: [PATCH] rss and ip seed box use string key to cache --- include/globalfunctions.php | 27 +++++++++++---------------- nexus/Install/Update.php | 2 ++ public/torrentrss.php | 26 +++++++++++--------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 67098d29..68ad43f8 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -767,16 +767,11 @@ function do_action($name, ...$args) function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool { - $redis = \Nexus\Database\NexusDB::redis(); - $key = "nexus_is_ip_seed_box"; - $hashKey = "ip:$ip:uid:$uid"; - $cacheData = $redis->hGet($key, $hashKey); - if ($cacheData && !$withoutCache) { - $cacheDataOriginal = unserialize($cacheData); - if ($cacheDataOriginal['deadline'] > time()) { - do_log("$hashKey, get result from cache: " . json_encode($cacheDataOriginal)); - return $cacheDataOriginal['data']; - } + $key = "nexus_is_ip_seed_box:ip:$ip:uid:$uid"; + $cacheData = \Nexus\Database\NexusDB::cache_get($key); + if (in_array($cacheData, [0, 1], true) && !$withoutCache) { + do_log("$key, get result from cache: $cacheData"); + return (bool)$cacheData; } $ipObject = \PhpIP\IP::create($ip); $ipNumeric = $ipObject->numeric(); @@ -787,8 +782,8 @@ function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool ); $res = \Nexus\Database\NexusDB::select($checkSeedBoxAdminSql); if (!empty($res)) { - $redis->hSet($key, $hashKey, serialize(['data' => true, 'deadline' => time() + 3600])); - do_log("$hashKey, get result from admin, true"); + \Nexus\Database\NexusDB::cache_put($key, 1); + do_log("$key, get result from admin, true"); return true; } if ($uid !== null) { @@ -798,13 +793,13 @@ function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool ); $res = \Nexus\Database\NexusDB::select($checkSeedBoxUserSql); if (!empty($res)) { - $redis->hSet($key, $hashKey, serialize(['data' => true, 'deadline' => time() + 3600])); - do_log("$hashKey, get result from user, true"); + \Nexus\Database\NexusDB::cache_put($key, 1); + do_log("$key, get result from user, true"); return true; } } - $redis->hSet($key, $hashKey, serialize(['data' => false, 'deadline' => time() + 3600])); - do_log("$hashKey, no result, false"); + \Nexus\Database\NexusDB::cache_put($key, 0); + do_log("$key, no result, false"); return false; } diff --git a/nexus/Install/Update.php b/nexus/Install/Update.php index e8d0b6f4..05600a6f 100644 --- a/nexus/Install/Update.php +++ b/nexus/Install/Update.php @@ -238,6 +238,8 @@ class Update extends Install * @since 1.7.19 */ $this->removeMenu(['freeleech.php']); + NexusDB::cache_del('nexus_rss'); + NexusDB::cache_del('nexus_is_ip_seed_box'); } diff --git a/public/torrentrss.php b/public/torrentrss.php index c8e3482d..021b5065 100644 --- a/public/torrentrss.php +++ b/public/torrentrss.php @@ -1,26 +1,22 @@ getRedis(); -$cacheData = $redis->hGet($cacheKey, $hashKey); +$passkey = $_GET['passkey'] ?? $CURUSER['passkey'] ?? ''; +if (!$passkey) { + die("require passkey"); +} +$cacheKey = "nexus_rss:$passkey:" . md5(http_build_query($_GET)); +$cacheData = \Nexus\Database\NexusDB::cache_get($cacheKey); 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']); - } + do_log("rss get from cache"); + header ("Content-type: text/xml"); + die($cacheData); } dbconn(); function hex_esc($matches) { return sprintf("%02x", ord($matches[0])); } $dllink = false; -$passkey = $_GET['passkey'] ?? $CURUSER['passkey'] ?? ''; -if (!$passkey) { - die("require passkey"); -} + $where = ""; if ($passkey){ $res = sql_query("SELECT id, enabled, parked, passkey FROM users WHERE passkey=". sqlesc($passkey)." LIMIT 1"); @@ -192,7 +188,7 @@ $xml .= ''.$row[ $xml .= ' '; do_log("rss cache generated"); -$redis->hSet($cacheKey, $hashKey, serialize(['data' => $xml, 'deadline' => time() + 300])); +\Nexus\Database\NexusDB::cache_put($cacheKey, $xml, 300); header ("Content-type: text/xml"); echo $xml; ?>