Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
perf(User): Simple sql to get user real_transfer from table snatched
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jan 21, 2020
1 parent e734812 commit 547c772
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,15 @@
argumentsSet('config.user'),
);

registerArgumentsSet('view.function',
'format_bytes',
'format_bytes_compact',
'format_bytes_loose',
'format_ubbcode',
'sec2hms'
);

expectedArguments(\League\Plates\Template\Template::escape(), 1, argumentsSet('view.function'));
expectedArguments(\League\Plates\Template\Template::e(), 1, argumentsSet('view.function'));

}
25 changes: 13 additions & 12 deletions application/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,27 +142,28 @@ public function getUploaded(): int
return $this->uploaded;
}

public function getRealUploaded(): int
public function getDownloaded(): int
{
return $this->downloaded;
}

private function getRealTransfer(): array
{
return $this->getCacheValue('true_uploaded', function () {
return app()->pdo->createCommand('SELECT SUM(`true_uploaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([
return $this->getCacheValue('true_transfer', function () {
return app()->pdo->createCommand('SELECT SUM(`true_uploaded`) as `uploaded`, SUM(`true_downloaded`) as `download` FROM `snatched` WHERE `user_id` = :uid')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
])->queryOne() ?? ['uploaded' => 0, 'download' => 0];
});
}

public function getDownloaded(): int
public function getRealUploaded(): int
{
return $this->downloaded;
return (int)$this->getRealTransfer()['uploaded'];
}

public function getRealDownloaded()
public function getRealDownloaded(): int
{
return $this->getCacheValue('true_downloaded', function () {
return app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([
"uid" => $this->id
])->queryScalar() ?? 0;
});
return (int)$this->getRealTransfer()['download'];
}

public function getRatio()
Expand Down
4 changes: 2 additions & 2 deletions application/Process/TrackerAnnounceProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ private function checkUpspeed($userInfo, $torrentInfo, $trueUploaded, $trueDownl

private function getTorrentBuff($userid, $torrentid, $trueUploaded, $trueDownloaded, $upspeed, &$thisUploaded, &$thisDownloaded)
{
$buff = app()->redis->get("TRACKER:user_" . $userid . "_torrent_" . $torrentid . "_buff");
$buff = app()->redis->get('TRACKER:buff:user_' . $userid . ':torrent_' . $torrentid);
if ($buff === false) {
$buff = app()->pdo->createCommand("SELECT COALESCE(MAX(`upload_ratio`),1) as `up_ratio`, COALESCE(MIN(`download_ratio`),1) as `dl_ratio` FROM `torrent_buffs`
WHERE start_at < NOW() AND NOW() < expired_at AND (torrent_id = :tid OR torrent_id = 0) AND (beneficiary_id = :bid OR beneficiary_id = 0);")->bindParams([
'tid' => $torrentid, 'bid' => $userid
])->queryOne();
app()->redis->setex("TRACKER:user_" . $userid . "_torrent_" . $torrentid . "_buff", 350, $buff);
app()->redis->setex('TRACKER:buff:user_' . $userid . ':torrent_' . $torrentid, intval((int) config('tracker.interval')), $buff);
}
$thisUploaded = $trueUploaded * ($buff['up_ratio'] ?: 1);
$thisDownloaded = $trueDownloaded * ($buff['dl_ratio'] ?: 1);
Expand Down

0 comments on commit 547c772

Please sign in to comment.