Gazelle/sections/peerupdate/index.php

82 lines
3.0 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
// We keep torrent groups cached. However, the peer counts change ofter, so our solutions are to not cache them for long, or to update them. Here is where we updated them.
if ((!isset($argv[1]) || $argv[1]!=SCHEDULE_KEY) && !check_perms('admin_schedule')) { // authorization, Fix to allow people with perms hit this page.
error(403);
}
if (check_perms('admin_schedule')) {
2012-10-11 08:00:15 +00:00
View::show_header();
2011-03-28 14:21:28 +00:00
echo '<pre>';
}
ignore_user_abort();
2012-05-18 13:35:17 +00:00
ini_set('max_execution_time',300);
2011-03-28 14:21:28 +00:00
ob_end_flush();
gc_enable();
2012-05-18 13:35:17 +00:00
$Cache->InternalCache = false; // We don't want PHP to cache all results internally
2011-03-28 14:21:28 +00:00
$DB->query("TRUNCATE TABLE torrents_peerlists_compare");
2012-05-18 13:35:17 +00:00
$DB->query("INSERT INTO torrents_peerlists_compare
SELECT ID, GroupID, Seeders, Leechers, Snatched FROM torrents
ON DUPLICATE KEY UPDATE Seeders=VALUES(Seeders), Leechers=VALUES(Leechers), Snatches=VALUES(Snatches)");
$DB->query("CREATE TEMPORARY TABLE tpc_temp
(TorrentID int, GroupID int, Seeders int, Leechers int, Snatched int,
PRIMARY KEY (GroupID,TorrentID))");
$DB->query("INSERT INTO tpc_temp SELECT t2.* FROM torrents_peerlists t1 JOIN torrents_peerlists_compare t2 USING(TorrentID)
WHERE t1.Seeders != t2.Seeders OR t1.Leechers != t2.Leechers OR t1.Snatches != t2.Snatches");
$StepSize = 30000;
$DB->query("SELECT * FROM tpc_temp ORDER BY GroupID ASC, TorrentID ASC LIMIT $StepSize");
2011-03-28 14:21:28 +00:00
2012-05-18 13:35:17 +00:00
$RowNum = 0;
$LastGroupID = 0;
$UpdatedKeys = $UncachedGroups = 0;
list($TorrentID, $GroupID, $Seeders, $Leechers, $Snatches) = $DB->next_record(MYSQLI_NUM, false);
while($TorrentID) {
if($LastGroupID != $GroupID) {
$CachedData = $Cache->get_value('torrent_group_'.$GroupID);
if($CachedData !== false) {
if(isset($CachedData['ver']) && $CachedData['ver'] >= 4) {
$CachedStats = &$CachedData['d']['Torrents'];
2011-03-28 14:21:28 +00:00
}
2012-05-18 13:35:17 +00:00
} else {
$UncachedGroups++;
2011-03-28 14:21:28 +00:00
}
2012-05-18 13:35:17 +00:00
$LastGroupID = $GroupID;
2011-03-28 14:21:28 +00:00
}
2012-05-18 13:35:17 +00:00
while($LastGroupID == $GroupID) {
$RowNum++;
if(isset($CachedStats) && is_array($CachedStats[$TorrentID])) {
$OldValues = &$CachedStats[$TorrentID];
$OldValues['Seeders'] = $Seeders;
$OldValues['Leechers'] = $Leechers;
$OldValues['Snatched'] = $Snatches;
$Changed = true;
unset($OldValues);
2011-03-28 14:21:28 +00:00
}
2012-05-18 13:35:17 +00:00
if(!($RowNum % $StepSize)) {
$DB->query("SELECT * FROM tpc_temp WHERE GroupID > $GroupID OR (GroupID = $GroupID AND TorrentID > $TorrentID)
ORDER BY GroupID ASC, TorrentID ASC LIMIT $StepSize");
2011-03-28 14:21:28 +00:00
}
2012-05-18 13:35:17 +00:00
$LastGroupID = $GroupID;
list($TorrentID, $GroupID, $Seeders, $Leechers, $Snatches) = $DB->next_record(MYSQLI_NUM, false);
2011-03-28 14:21:28 +00:00
}
if($Changed) {
2012-05-18 13:35:17 +00:00
$Cache->cache_value('torrent_group_'.$LastGroupID, $CachedData, 0);
unset($CachedStats);
$UpdatedKeys++;
$Changed = false;
2011-03-28 14:21:28 +00:00
}
}
2012-05-18 13:35:17 +00:00
printf("Updated %d keys, skipped %d keys in %.6fs (%d kB memory)\n", $UpdatedKeys, $UncachedGroups, microtime(true)-$ScriptStartTime, memory_get_usage(true)>>10);
2011-03-28 14:21:28 +00:00
$DB->query("TRUNCATE TABLE torrents_peerlists");
2012-05-18 13:35:17 +00:00
$DB->query("INSERT INTO torrents_peerlists SELECT * FROM torrents_peerlists_compare");
2011-03-28 14:21:28 +00:00
if (check_perms('admin_schedule')) {
echo '<pre>';
2012-10-11 08:00:15 +00:00
View::show_footer();
2011-03-28 14:21:28 +00:00
}
?>