rss and ip seed box use string key to cache

This commit is contained in:
xiaomlove
2022-07-29 03:34:55 +08:00
parent c43ded9369
commit 8a4f4a77cb
3 changed files with 24 additions and 31 deletions
+11 -16
View File
@@ -767,16 +767,11 @@ function do_action($name, ...$args)
function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool
{ {
$redis = \Nexus\Database\NexusDB::redis(); $key = "nexus_is_ip_seed_box:ip:$ip:uid:$uid";
$key = "nexus_is_ip_seed_box"; $cacheData = \Nexus\Database\NexusDB::cache_get($key);
$hashKey = "ip:$ip:uid:$uid"; if (in_array($cacheData, [0, 1], true) && !$withoutCache) {
$cacheData = $redis->hGet($key, $hashKey); do_log("$key, get result from cache: $cacheData");
if ($cacheData && !$withoutCache) { return (bool)$cacheData;
$cacheDataOriginal = unserialize($cacheData);
if ($cacheDataOriginal['deadline'] > time()) {
do_log("$hashKey, get result from cache: " . json_encode($cacheDataOriginal));
return $cacheDataOriginal['data'];
}
} }
$ipObject = \PhpIP\IP::create($ip); $ipObject = \PhpIP\IP::create($ip);
$ipNumeric = $ipObject->numeric(); $ipNumeric = $ipObject->numeric();
@@ -787,8 +782,8 @@ function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool
); );
$res = \Nexus\Database\NexusDB::select($checkSeedBoxAdminSql); $res = \Nexus\Database\NexusDB::select($checkSeedBoxAdminSql);
if (!empty($res)) { if (!empty($res)) {
$redis->hSet($key, $hashKey, serialize(['data' => true, 'deadline' => time() + 3600])); \Nexus\Database\NexusDB::cache_put($key, 1);
do_log("$hashKey, get result from admin, true"); do_log("$key, get result from admin, true");
return true; return true;
} }
if ($uid !== null) { if ($uid !== null) {
@@ -798,13 +793,13 @@ function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool
); );
$res = \Nexus\Database\NexusDB::select($checkSeedBoxUserSql); $res = \Nexus\Database\NexusDB::select($checkSeedBoxUserSql);
if (!empty($res)) { if (!empty($res)) {
$redis->hSet($key, $hashKey, serialize(['data' => true, 'deadline' => time() + 3600])); \Nexus\Database\NexusDB::cache_put($key, 1);
do_log("$hashKey, get result from user, true"); do_log("$key, get result from user, true");
return true; return true;
} }
} }
$redis->hSet($key, $hashKey, serialize(['data' => false, 'deadline' => time() + 3600])); \Nexus\Database\NexusDB::cache_put($key, 0);
do_log("$hashKey, no result, false"); do_log("$key, no result, false");
return false; return false;
} }
+2
View File
@@ -238,6 +238,8 @@ class Update extends Install
* @since 1.7.19 * @since 1.7.19
*/ */
$this->removeMenu(['freeleech.php']); $this->removeMenu(['freeleech.php']);
NexusDB::cache_del('nexus_rss');
NexusDB::cache_del('nexus_is_ip_seed_box');
} }
+11 -15
View File
@@ -1,26 +1,22 @@
<?php <?php
require "../include/bittorrent.php"; require "../include/bittorrent.php";
$cacheKey = 'nexus_rss'; $passkey = $_GET['passkey'] ?? $CURUSER['passkey'] ?? '';
$hashKey = md5(http_build_query($_GET)); if (!$passkey) {
$redis = $Cache->getRedis(); die("require passkey");
$cacheData = $redis->hGet($cacheKey, $hashKey); }
$cacheKey = "nexus_rss:$passkey:" . md5(http_build_query($_GET));
$cacheData = \Nexus\Database\NexusDB::cache_get($cacheKey);
if ($cacheData) { if ($cacheData) {
$cacheData = unserialize($cacheData); do_log("rss get from cache");
if (isset($cacheData['deadline']) && $cacheData['deadline'] > time()) { header ("Content-type: text/xml");
do_log("rss get from cache"); die($cacheData);
header ("Content-type: text/xml");
die($cacheData['data']);
}
} }
dbconn(); dbconn();
function hex_esc($matches) { function hex_esc($matches) {
return sprintf("%02x", ord($matches[0])); return sprintf("%02x", ord($matches[0]));
} }
$dllink = false; $dllink = false;
$passkey = $_GET['passkey'] ?? $CURUSER['passkey'] ?? '';
if (!$passkey) {
die("require passkey");
}
$where = ""; $where = "";
if ($passkey){ if ($passkey){
$res = sql_query("SELECT id, enabled, parked, passkey FROM users WHERE passkey=". sqlesc($passkey)." LIMIT 1"); $res = sql_query("SELECT id, enabled, parked, passkey FROM users WHERE passkey=". sqlesc($passkey)." LIMIT 1");
@@ -192,7 +188,7 @@ $xml .= '<category domain="'.$url.'/torrents.php?cat='.$row['cat_id'].'">'.$row[
$xml .= '</channel> $xml .= '</channel>
</rss>'; </rss>';
do_log("rss cache generated"); 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"); header ("Content-type: text/xml");
echo $xml; echo $xml;
?> ?>