Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memcache support, initially for tweets #211

Merged
merged 3 commits into from
May 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions includes/creds.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
getDBUser()
getDBPass()
getDBHost()
getMCHost()
getMCPort()
getAdmins()
getGroupID()
getGroupID32()
Expand Down
41 changes: 41 additions & 0 deletions includes/functions_memcache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Function collection for fetching and storing variables in our memcache.
*/

include_once( 'creds.php' );

/**
* This connects to the memcache store
* @return object handle to the memcache store, or false if there was a failure
*/
function connectMemcache( ) {

$conn = false;
try {
$conn = memcache_connect( getMCHost( ), getMCPort( ) );
} catch( Exception $e ) {
echo $e->getMessage( );
}
return $conn;
}

/**
* This fetches a variable from the memcache store, if the variable is not found
* it will call the provided function, and store that in the system with the
* expiry time supplied
* @param object $memcache a connection to the memcache daemon
* @param object $variable a handle to the name of the data which it is stored under
* @param integer $expiry number in seconds for the data to persist
* @param function $function a function that returns the data you wish to store, in case the memcache data has expired
* @return object the data requested, either fresh or < expiry
*/
function fetchOrStore( $memcache, $variable, $expiry, $function ) {
$value = @memcache_get( $memcache, $variable );
if ( ! $value ) {
$value = $function( );
@memcache_set( $memcache, $variable, $value, false, $expiry );
// TODO this can fail to set, though we return $value so nothing will be lost?
}
return $value;
}
8 changes: 7 additions & 1 deletion news.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
include_once( 'includes/header.php' );
include_once( 'includes/lastRSS.php' );
include_once( 'includes/functions_twitter.php' );
include_once( 'includes/functions_memcache.php' );

$tweetblob = '';
foreach ( getRecentTweets( 3 ) as $tweet ) {
$recentTweets = array( );
$varCache = connectMemcache( );
// Set expiry to 20 minutes
$recentTweets = fetchOrStore( $varCache, 'recent-tweets', 20 * 60, function ( ) { return getRecentTweets( 3 ); } );

foreach ( $recentTweets as $tweet ) {

$time = humanTime ( $tweet[ 'created_at' ] );
$user = $tweet[ 'user' ];
Expand Down
115 changes: 62 additions & 53 deletions servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
include_once( 'includes/header.php' );
include_once( 'includes/GameQ.php' );
include_once( 'includes/paths.php' );
$Servers = file( $serversRepo . '/serverlist' );
include_once( 'includes/functions_memcache.php' );
?>
<h1 class="text-center">Game Servers</h1>
<article class="panel panel-default">
Expand Down Expand Up @@ -42,64 +42,73 @@
<?php
flush( );

foreach ( $Servers as $Server ) {
if ( strlen( $Server ) > 11 and strrpos( $Server, '#', -strlen( $Server ) ) === False ) {
list ( $ServerHost[], $Ports[], $GameType[] ) = preg_split ( '/(:|,)/', $Server );
$varCache = connectMemcache( );
// Set expiry to 10 minutes
echo fetchOrStore( $varCache, 'servers-list', 10 * 60, function ( ) use ( $serversRepo ) {

$serverlist = '';
$Servers = file( $serversRepo . '/serverlist' );
foreach ( $Servers as $Server ) {
if ( strlen( $Server ) > 11 and strrpos( $Server, '#', -strlen( $Server ) ) === False ) {
list ( $ServerHost[], $Ports[], $GameType[] ) = preg_split ( '/(:|,)/', $Server );
}
}
$gq = new GameQ( );
foreach ( $ServerHost as $Index => $Host) {
$gq->addServer( array(
'type' => trim( $GameType[$Index] ),
'host' => trim( $Host ) . ":" . trim( $Ports[$Index] ),
));
}
}
$gq = new GameQ( );
foreach ( $ServerHost as $Index => $Host) {
$gq->addServer( array(
'type' => trim( $GameType[$Index] ),
'host' => trim( $Host ) . ":" . trim( $Ports[$Index] ),
));
}

$results = $gq->setOption( 'timeout', 1 )
->setFilter( 'normalise' )
->requestData( );
$results = $gq->setOption( 'timeout', 1 )
->setFilter( 'normalise' )
->requestData( );

foreach ( $results as $id => $data ) {
if ( ! $data['gq_online'] ) {
$data['gq_address'] = preg_replace( '/.steamlug.org/', '​.steamlug.org', $data['gq_address'], 1 );
echo <<<SERVERSTRING
<tr class="unresponsive">
<td></td>
<td></td>
<td></td>
<td><em>Server Unresponsive</em></td>
<td><em>{$data['gq_address']}​:{$data['gq_port']}</em></td>
<td><em>0 ⁄ 0</em></td>
<td class="hidden-xxs"><em>N/A</em></td>
<td><i class="text-danger fa-circle-o"></i></span></td>
</tr>
foreach ( $results as $id => $data ) {
if ( ! $data['gq_online'] ) {
$data['gq_address'] = preg_replace( '/.steamlug.org/', '​.steamlug.org', $data['gq_address'], 1 );
$serverlist .= <<<SERVERSTRING
<tr class="unresponsive">
<td></td>
<td></td>
<td></td>
<td><em>Server Unresponsive</em></td>
<td><em>{$data['gq_address']}​:{$data['gq_port']}</em></td>
<td><em>0 ⁄ 0</em></td>
<td class="hidden-xxs"><em>N/A</em></td>
<td><i class="text-danger fa-circle-o"></i></span></td>
</tr>
SERVERSTRING;
} else {
/* this block of code should be better… TODO it please */
$serverLoc = geoip_country_code_by_name( $data['gq_address'] );
$serverSec = ! empty( $data['secure'] ) ? '<i class="fa-shield"></i>' : '';
$serverPass = ! empty( $data['gq_password'] ) ? '<i class="fa-lock"></i>' : '';
$serverDesc = ! empty( $data['gq_name'] ) ? $data['gq_name'] : '';
// TODO commented out until our new DB stuff is done
// $serverDesc = ! empty( $data['gq_steamappid'] ) ? '<a href="/app/' . $data['gq_steamappid'] . '">' . $data['gq_name'] . '</a>' : $data['gq_name'];
$serverNum = ( ! empty( $data['gq_numplayers'] ) ? $data['gq_numplayers'] : '0') . ' ⁄ ' . $data['gq_maxplayers'];
$serverMap = substr( $data['gq_mapname'], 0, 18 );
$connectPort = ( ! empty( $data['port'] ) ? $data['port'] : ( isset( $data['gameport'] ) ? $data['gameport'] : $data['gq_port'] ) );
$serverHost = $data['gq_address'] . ":" . $connectPort;
echo <<<SERVERSTRING
<tr>
<td><img src="/images/flags/{$serverLoc}.png" title="Hosted in {$serverLoc}" alt="{$serverLoc}" /></td>
<td>{$serverSec}</td>
<td>{$serverPass}</td>
<td>{$serverDesc}</td>
<td><a href="steam://connect/{$serverHost}">{$data['gq_hostname']}</a>
<td>{$serverNum}</td>
<td class="hidden-xxs">{$serverMap}</td>
<td><i class="text-success fa-circle"></i></td>
</tr>
} else {
/* this block of code should be better… TODO it please */
$serverLoc = geoip_country_code_by_name( $data['gq_address'] );
$serverSec = ! empty( $data['secure'] ) ? '<i class="fa-shield"></i>' : '';
$serverPass = ! empty( $data['gq_password'] ) ? '<i class="fa-lock"></i>' : '';
$serverDesc = ! empty( $data['gq_name'] ) ? $data['gq_name'] : '';
// TODO commented out until our new DB stuff is done
// $serverDesc = ! empty( $data['gq_steamappid'] ) ? '<a href="/app/' . $data['gq_steamappid'] . '">' . $data['gq_name'] . '</a>' : $data['gq_name'];
$serverNum = ( ! empty( $data['gq_numplayers'] ) ? $data['gq_numplayers'] : '0') . ' ⁄ ' . $data['gq_maxplayers'];
$serverMap = substr( $data['gq_mapname'], 0, 18 );
$connectPort = ( ! empty( $data['port'] ) ? $data['port'] : ( isset( $data['gameport'] ) ? $data['gameport'] : $data['gq_port'] ) );
$serverHost = $data['gq_address'] . ":" . $connectPort;
$serverlist .= <<<SERVERSTRING
<tr>
<td><img src="/images/flags/{$serverLoc}.png" title="Hosted in {$serverLoc}" alt="{$serverLoc}" /></td>
<td>{$serverSec}</td>
<td>{$serverPass}</td>
<td>{$serverDesc}</td>
<td><a href="steam://connect/{$serverHost}">{$data['gq_hostname']}</a>
<td>{$serverNum}</td>
<td class="hidden-xxs">{$serverMap}</td>
<td><i class="text-success fa-circle"></i></td>
</tr>
SERVERSTRING;
}
}
}

return $serverlist;
} );
?>
</tbody>
</table>
Expand Down