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

Commit

Permalink
perf(User): Quickly get user peer status
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 16, 2019
1 parent 0ad2d64 commit 020a96f
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions framework/User/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ trait UserTrait
private $seedtime;
private $leechtime;

protected $peer_status;
protected $infoCacheKey;

public function loadUserContentById($id)
Expand Down Expand Up @@ -266,30 +267,28 @@ public function getTimeRatio()
return max(1, $this->seedtime) / max(1, $this->leechtime);
}

public function getActiveSeed()
private function getPeerStatus($seeder = null)
{
$active_seed = app()->redis->hGet('User:' . $this->id . ':peer_count', 'active_seed_count');
if ($active_seed === false) {
$active_seed = app()->pdo->createCommand("SELECT COUNT(id) FROM `peers` WHERE `user_id` = :uid AND `seeder`='yes'")->bindParams([
$peer_status = $this->peer_status ?? app()->redis->get('User:' . $this->id . ':peer_count');
if (is_null($peer_status) || $peer_status === false) {
$peer_count = app()->pdo->createCommand("SELECT `seeder`, COUNT(id) FROM `peers` WHERE `user_id` = :uid GROUP BY seeder")->bindParams([
'uid' => $this->id
])->queryScalar() ?: 0;
app()->redis->hSet('User:' . $this->id . ':peer_count', 'active_seed_count', $active_seed);
app()->redis->expire('User:' . $this->id . ':peer_count',60);
])->queryAll() ?: [];
$peer_status = array_merge(['yes' => 0, 'no' => 0, 'partial' => 0], $peer_count);
$this->peer_status = $peer_status;
app()->redis->set('User:' . $this->id . ':peer_count', $peer_status, 60);
}
return $active_seed;
return $seeder ? (int)$peer_status[$seeder] : $peer_status;
}

public function getActiveSeed()
{
return $this->getPeerStatus('yes');
}

public function getActiveLeech()
{
$active_leech = app()->redis->hGet('User:' . $this->id . ':peer_count', 'active_leech_count');
if ($active_leech === false) {
$active_leech = app()->pdo->createCommand("SELECT COUNT(id) FROM `peers` WHERE `user_id` = :uid AND `seeder`='no'")->bindParams([
'uid' => $this->id
])->queryScalar() ?: 0;
app()->redis->hSet('User:' . $this->id . ':peer_count', 'active_leech_count', $active_leech);
app()->redis->expire('User:' . $this->id . ':peer_count',60);
}
return $active_leech;
return $this->getPeerStatus('no') + $this->getPeerStatus('partial');
}

/**
Expand Down

0 comments on commit 020a96f

Please sign in to comment.