2011-03-28 14:21:28 +00:00
< ?
2013-02-22 08:00:24 +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.
2011-03-28 14:21:28 +00:00
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 " );
2013-07-10 00:08:53 +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 .*
2013-11-17 08:00:47 +00:00
FROM torrents_peerlists AS t1
JOIN torrents_peerlists_compare AS t2
2013-07-10 00:08:53 +00:00
USING ( TorrentID )
WHERE t1 . Seeders != t2 . Seeders
OR t1 . Leechers != t2 . Leechers
OR t1 . Snatches != t2 . Snatches " );
2012-05-18 13:35:17 +00:00
$StepSize = 30000 ;
2013-07-10 00:08:53 +00:00
$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 );
2013-05-04 08:00:48 +00:00
while ( $TorrentID ) {
if ( $LastGroupID != $GroupID ) {
2013-07-10 00:08:53 +00:00
$CachedData = $Cache -> get_value ( " torrent_group_ $GroupID " );
2013-05-04 08:00:48 +00:00
if ( $CachedData !== false ) {
if ( isset ( $CachedData [ 'ver' ]) && $CachedData [ 'ver' ] == CACHE :: GROUP_VERSION ) {
2012-05-18 13:35:17 +00:00
$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
}
2013-05-04 08:00:48 +00:00
while ( $LastGroupID == $GroupID ) {
2012-05-18 13:35:17 +00:00
$RowNum ++ ;
2013-05-04 08:00:48 +00:00
if ( isset ( $CachedStats ) && is_array ( $CachedStats [ $TorrentID ])) {
2012-05-18 13:35:17 +00:00
$OldValues = & $CachedStats [ $TorrentID ];
$OldValues [ 'Seeders' ] = $Seeders ;
$OldValues [ 'Leechers' ] = $Leechers ;
$OldValues [ 'Snatched' ] = $Snatches ;
$Changed = true ;
unset ( $OldValues );
2011-03-28 14:21:28 +00:00
}
2013-05-04 08:00:48 +00:00
if ( ! ( $RowNum % $StepSize )) {
2013-07-10 00:08:53 +00:00
$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
}
2013-05-04 08:00:48 +00:00
if ( $Changed ) {
2013-07-10 00:08:53 +00:00
$Cache -> cache_value ( " torrent_group_ $LastGroupID " , $CachedData , 0 );
2012-05-18 13:35:17 +00:00
unset ( $CachedStats );
$UpdatedKeys ++ ;
$Changed = false ;
2011-03-28 14:21:28 +00:00
}
}
2013-07-10 00:08:53 +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 " );
2013-07-10 00:08:53 +00:00
$DB -> query ( "
INSERT INTO torrents_peerlists
SELECT *
FROM torrents_peerlists_compare " );
2011-03-28 14:21:28 +00:00
2013-02-22 08:00:24 +00:00
if ( check_perms ( 'admin_schedule' )) {
2011-03-28 14:21:28 +00:00
echo '<pre>' ;
2012-10-11 08:00:15 +00:00
View :: show_footer ();
2011-03-28 14:21:28 +00:00
}
2011-09-04 08:00:07 +00:00
?>