From 3f2af6becc721a2f4ecba3b4f229bd1a5f1463fe Mon Sep 17 00:00:00 2001 From: John Drinkwater Date: Thu, 11 May 2017 13:14:52 +0100 Subject: [PATCH 1/3] Memcache support, initially for tweets This could be expanded to support: servers.php and stream.php. Work is part of #208 --- includes/creds.php | 2 ++ includes/functions_memcache.php | 41 +++++++++++++++++++++++++++++++++ news.php | 12 +++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 includes/functions_memcache.php diff --git a/includes/creds.php b/includes/creds.php index 6bc0700..5162006 100644 --- a/includes/creds.php +++ b/includes/creds.php @@ -4,6 +4,8 @@ getDBUser() getDBPass() getDBHost() + getMCHost() + getMCPort() getAdmins() getGroupID() getGroupID32() diff --git a/includes/functions_memcache.php b/includes/functions_memcache.php new file mode 100644 index 0000000..142b9af --- /dev/null +++ b/includes/functions_memcache.php @@ -0,0 +1,41 @@ +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; +} diff --git a/news.php b/news.php index e89c719..5bc39b8 100644 --- a/news.php +++ b/news.php @@ -3,9 +3,19 @@ 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( ); +if ( $varCache ) { + // Set expiry to 20 minutes + $recentTweets = fetchOrStore( $varCache, 'recent-tweets', 20 * 60, function ( ) { return getRecentTweets( 3 ); } ); +} else { + $recentTweets = getRecentTweets( 3 ); +} + +foreach ( $recentTweets as $tweet ) { $time = humanTime ( $tweet[ 'created_at' ] ); $user = $tweet[ 'user' ]; From 2cba8f00c7849581c0d08a60eeed64a70db948f8 Mon Sep 17 00:00:00 2001 From: John Drinkwater Date: Fri, 12 May 2017 13:14:18 +0100 Subject: [PATCH 2/3] Support memcache for servers.php --- servers.php | 115 ++++++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/servers.php b/servers.php index 9196a98..5a88ff9 100644 --- a/servers.php +++ b/servers.php @@ -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' ); ?>

Game Servers

@@ -42,64 +42,73 @@ 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 << - - - - Server Unresponsive - {$data['gq_address']}​:{$data['gq_port']} - 0 ⁄ 0 - N/A - - + foreach ( $results as $id => $data ) { + if ( ! $data['gq_online'] ) { + $data['gq_address'] = preg_replace( '/.steamlug.org/', '​.steamlug.org', $data['gq_address'], 1 ); + $serverlist .= << + + + + Server Unresponsive + {$data['gq_address']}​:{$data['gq_port']} + 0 ⁄ 0 + N/A + + 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'] ) ? '' : ''; - $serverPass = ! empty( $data['gq_password'] ) ? '' : ''; - $serverDesc = ! empty( $data['gq_name'] ) ? $data['gq_name'] : ''; - // TODO commented out until our new DB stuff is done - // $serverDesc = ! empty( $data['gq_steamappid'] ) ? '' . $data['gq_name'] . '' : $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 << - {$serverLoc} - {$serverSec} - {$serverPass} - {$serverDesc} - {$data['gq_hostname']} - {$serverNum} - {$serverMap} - - + } else { + /* this block of code should be better… TODO it please */ + $serverLoc = geoip_country_code_by_name( $data['gq_address'] ); + $serverSec = ! empty( $data['secure'] ) ? '' : ''; + $serverPass = ! empty( $data['gq_password'] ) ? '' : ''; + $serverDesc = ! empty( $data['gq_name'] ) ? $data['gq_name'] : ''; + // TODO commented out until our new DB stuff is done + // $serverDesc = ! empty( $data['gq_steamappid'] ) ? '' . $data['gq_name'] . '' : $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 .= << + {$serverLoc} + {$serverSec} + {$serverPass} + {$serverDesc} + {$data['gq_hostname']} + {$serverNum} + {$serverMap} + + SERVERSTRING; + } } - } + + return $serverlist; + } ); ?> From 67c59dc87aa02134a4bfd99136c58d3cd24b83e8 Mon Sep 17 00:00:00 2001 From: John Drinkwater Date: Fri, 12 May 2017 13:15:01 +0100 Subject: [PATCH 3/3] Avoid the else branch for memcache use, makes code clearer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When access to memcache is offline, fetchOrStore will still return the result of the function call; this makes the code more readable and prevents copy/paste errors for the false branch. This mirrors the behaviour added for the servers memcache update. This does remove error reporting for memcache calls… because otherwise passing in false here would produce errors in log for every visit. --- includes/functions_memcache.php | 4 ++-- news.php | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/includes/functions_memcache.php b/includes/functions_memcache.php index 142b9af..99f1aa8 100644 --- a/includes/functions_memcache.php +++ b/includes/functions_memcache.php @@ -31,10 +31,10 @@ function connectMemcache( ) { * @return object the data requested, either fresh or < expiry */ function fetchOrStore( $memcache, $variable, $expiry, $function ) { - $value = memcache_get( $memcache, $variable ); + $value = @memcache_get( $memcache, $variable ); if ( ! $value ) { $value = $function( ); - memcache_set( $memcache, $variable, $value, false, $expiry ); + @memcache_set( $memcache, $variable, $value, false, $expiry ); // TODO this can fail to set, though we return $value so nothing will be lost? } return $value; diff --git a/news.php b/news.php index 5bc39b8..669c67c 100644 --- a/news.php +++ b/news.php @@ -8,12 +8,8 @@ $tweetblob = ''; $recentTweets = array( ); $varCache = connectMemcache( ); -if ( $varCache ) { - // Set expiry to 20 minutes - $recentTweets = fetchOrStore( $varCache, 'recent-tweets', 20 * 60, function ( ) { return getRecentTweets( 3 ); } ); -} else { - $recentTweets = getRecentTweets( 3 ); -} +// Set expiry to 20 minutes +$recentTweets = fetchOrStore( $varCache, 'recent-tweets', 20 * 60, function ( ) { return getRecentTweets( 3 ); } ); foreach ( $recentTweets as $tweet ) {