scrape add cache

This commit is contained in:
xiaomlove
2023-03-04 00:52:08 +08:00
parent 285c6defdf
commit ae7634a8db
4 changed files with 79 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ namespace App\Repositories;
use App\Models\Claim;
use App\Models\Message;
use App\Models\Peer;
use App\Models\Snatch;
use App\Models\Torrent;
use App\Models\User;
@@ -26,6 +27,20 @@ class ClaimRepository extends BaseRepository
}
public function store($uid, $torrentId)
{
$snatch = $this->canBeClaimByUser($torrentId, $uid);
$insert = [
'uid' => $uid,
'torrent_id' => $torrentId,
'snatched_id' => $snatch->id,
'seed_time_begin' => $snatch->seedtime,
'uploaded_begin' => $snatch->uploaded,
];
$this->clearStatsCache($uid);
return Claim::query()->create($insert);
}
public function canBeClaimByUser(int $torrentId, int $uid)
{
$isEnabled = Claim::getConfigIsEnabled();
if (!$isEnabled) {
@@ -35,6 +50,15 @@ class ClaimRepository extends BaseRepository
if ($exists) {
throw new \RuntimeException(nexus_trans("torrent.claim_already"));
}
$snatch = Snatch::query()->where('userid', $uid)->where('torrentid', $torrentId)->first();
if (!$snatch) {
throw new \RuntimeException(nexus_trans("torrent.no_snatch"));
}
$claimTorrentTTL = Claim::getConfigTorrentTTL();
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
if ($torrent->added->addDays($claimTorrentTTL)->gte(Carbon::now())) {
throw new \RuntimeException(nexus_trans("torrent.can_no_be_claimed_yet"));
}
if (!apply_filter('user_has_role_work_seeding', false, $uid)) {
$max = Claim::getConfigTorrentUpLimit();
$count = Claim::query()->where('uid', $uid)->count();
@@ -47,24 +71,7 @@ class ClaimRepository extends BaseRepository
throw new \RuntimeException(nexus_trans("torrent.claim_number_reach_user_maximum"));
}
}
$snatch = Snatch::query()->where('userid', $uid)->where('torrentid', $torrentId)->first();
if (!$snatch) {
throw new \RuntimeException(nexus_trans("torrent.no_snatch"));
}
$claimTorrentTTL = Claim::getConfigTorrentTTL();
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
if ($torrent->added->addDays($claimTorrentTTL)->gte(Carbon::now())) {
throw new \RuntimeException(nexus_trans("torrent.can_no_be_claimed_yet"));
}
$insert = [
'uid' => $uid,
'torrent_id' => $torrentId,
'snatched_id' => $snatch->id,
'seed_time_begin' => $snatch->seedtime,
'uploaded_begin' => $snatch->uploaded,
];
$this->clearStatsCache($uid);
return Claim::query()->create($insert);
return $snatch;
}
public function update(array $params, $id)
@@ -336,4 +343,47 @@ class ClaimRepository extends BaseRepository
}
}
public function claimAllSeeding(int $uid)
{
$page = 1;
$size = 1000;
$total = 0;
while (true) {
$peers = Peer::query()
->where('uid', $uid)
->where('seeder', 'yes')
->where('to_go', 0)
->groupBy('torrentid')
->forPage($page, $size)
->get(['torrentid']);
if ($peers->isEmpty()) {
break;
}
$torrentIdArr = $peers->pluck('torrentid')->toArray();
$snatches = Snatch::query()
->whereIn('torrentid', $torrentIdArr)
->where('userid', $uid)
->groupBy('torrentid')
->get(['id', 'torrentid', 'seedtime', 'uploaded']);
if ($snatches->isNotEmpty()) {
$values = [];
$nowStr = now()->toDateTimeString();
$sql = "insert into claims (uid, torrent_id, snatched_id, seed_time_begin, uploaded_begin, created_at, uploaded_at)";
foreach ($snatches as $snatch) {
$values[] = sprintf(
"(%s, %s, %s, %s, %s, '%s', '%s')",
$uid, $snatch->torrentid, $snatch->id, $snatch->seedtime, $snatch->uploaded, $nowStr, $nowStr
);
}
$sql .= sprintf(" values %s on duplicate key update updated_at = '%s'", implode(', ', $values), $nowStr);
NexusDB::statement($sql);
}
$count = $snatches->count();
$total += $count;
do_log("page: $page, insert rows count: " . $count);
$page++;
}
return $total;
}
}

View File

@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.0');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-02-21');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-03-04');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");

View File

@@ -1132,7 +1132,7 @@ function get_passkey_by_authkey($authkey)
}
$uid = $arr[1];
$torrentRep = new \App\Repositories\TorrentRepository();
$decrypted = $torrentRep->checkTrackerReportAuthKey($_REQUEST['authkey']);
$decrypted = $torrentRep->checkTrackerReportAuthKey($authkey);
if (empty($decrypted)) {
throw new \InvalidArgumentException("Invalid authkey: $authkey");
}

View File

@@ -1,16 +1,5 @@
<?php
require_once('../include/bittorrent_announce.php');
$apiLocalHost = nexus_env('TRACKER_API_LOCAL_HOST');
if ($apiLocalHost) {
do_log("[TRACKER_API_LOCAL_HOST] $apiLocalHost");
$response = request_local_api(trim($apiLocalHost, '/') . '/api/scrape');
if (empty($response)) {
err("error from TRACKER_API_LOCAL_HOST");
} else {
exit(benc_resp_raw($response));
}
}
require ROOT_PATH . 'include/core.php';
//require_once('../include/benc.php');
dbconn_announce();
@@ -29,6 +18,14 @@ else {
$query = "SELECT $fields FROM torrents WHERE " . hash_where_arr('info_hash', $info_hash_array[1]);
}
$cacheKey = md5($query);
$cacheData = \Nexus\Database\NexusDB::cache_get($cacheKey);
if ($cacheData) {
do_log("[SCRAPE_FROM_CACHE]: " . json_encode($info_hash_array[1]));
benc_resp($cacheData);
exit(0);
}
$res = sql_query($query);
if (mysql_num_rows($res) < 1){
@@ -45,4 +42,5 @@ while ($row = mysql_fetch_assoc($res)) {
}
$d = ['files' => $torrent_details];
\Nexus\Database\NexusDB::cache_put($cacheKey, $d, 900);
benc_resp($d);