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

Commit

Permalink
feat(Torrent): Add Torrent Content Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 8, 2019
1 parent df5d57e commit 0038a7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/controllers/TorrentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TorrentsController extends Controller
public function actionIndex()
{
// TODO add pagination support
$fetch = app()->pdo->createCommand('SELECT `id` FROM torrents ORDER BY added_at DESC LIMIT 50;')->queryAll();
$fetch = app()->pdo->createCommand('SELECT `id` FROM torrents ORDER BY added_at DESC LIMIT 50;')->queryColumn();

$torrents = array_map(function ($id) {
return new Torrent($id);
Expand Down
38 changes: 25 additions & 13 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

use Rid\Bencode\Bencode;
use Rid\Exceptions\NotFoundException;
use Rid\Utils\AttributesImportUtils;

class Torrent
{
private $id;
use AttributesImportUtils;

private $id = null;

private $owner_id;
private $info_hash;

Expand Down Expand Up @@ -41,19 +45,25 @@ class Torrent

public function __construct($id = null)
{
# TODO Add redis hash type cache
$fetch = app()->pdo->createCommand("SELECT * FROM `torrents` WHERE id=:id LIMIT 1;")->bindParams([
"id" => $id
])->queryOne();
if ($fetch) {
# TODO Only allow admins see deleted torrents
foreach ($fetch as $key => $value)
$this->$key = $value;
} else {
$this->loadTorrentContentById($id);
if ($this->id == null) {
throw new NotFoundException("Not Found");
}
}

public function loadTorrentContentById($id)
{
$self = app()->redis->hGetAll('Torrent:id_' . $id . '_content');
if (empty($self)) {
$self = app()->pdo->createCommand("SELECT * FROM `torrents` WHERE id=:id LIMIT 1;")->bindParams([
"id" => $id
])->queryOne() ?? [];
app()->redis->hMset('Torrent:id_' . $id . '_content', $self);
app()->redis->expire('Torrent:id_' . $id . '_content', 3 * 60);
}
$this->importAttributes($self);
}

public static function TorrentFileLoc($id = 0)
{
return app()->getPrivatePath('torrents') . DIRECTORY_SEPARATOR . $id . ".torrent";
Expand Down Expand Up @@ -144,13 +154,15 @@ public function getDescr()
return $this->descr;
}

public function getRawDict() {
public function getRawDict()
{
$file = self::TorrentFileLoc($this->id);
$dict = Bencode::load($file);
return $dict;
}

public function getDownloadDict($encode = true) {
public function getDownloadDict($encode = true)
{
$userInfo = app()->session->get('userInfo'); // FIXME add remote download by &passkey= (Add change our BeforeMiddle) or token ?
$scheme = "http://";
Expand Down Expand Up @@ -191,7 +203,7 @@ public function getDownloadDict($encode = true) {
* @param bool $raw
* @return mixed
*/
public function getInfoHash($raw=false)
public function getInfoHash($raw = false)
{
if ($raw) {
return $this->info_hash;
Expand Down

0 comments on commit 0038a7d

Please sign in to comment.