r44476 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44475‎ | r44476 | r44477 >
Date:23:18, 11 December 2008
Author:demon
Status:ok
Tags:
Comment:
Add some caching of Http::get() (and potentially DB) results. Save us a whole lot of processing.
Modified paths:
  • /trunk/extensions/Interlanguage/Interlanguage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Interlanguage/Interlanguage.php
@@ -1,7 +1,7 @@
22 <?php
33 # MediaWiki Interlanguage extension v1.1
44 #
5 -# Copyright � 2008 Nikola Smolenski <smolensk@eunet.yu>
 5+# Copyright © 2008 Nikola Smolenski <smolensk@eunet.yu>
66 #
77 # This program is free software; you can redistribute it and/or modify
88 # it under the terms of the GNU General Public License as published by
@@ -42,22 +42,24 @@
4343 }
4444
4545 function InterlanguageExtension( &$parser, $param) {
46 - global
47 - $wgInterlanguageExtensionApiUrl,
48 - $wgInterlanguageExtensionSort,
49 - $wgInterlanguageExtensionPrefix,
50 - $wgInterlanguageExtensionInterwiki,
51 - $wgLanguageCode,
52 - $wgTitle;
 46+ global $wgInterlanguageExtensionApiUrl, $wgInterlanguageExtensionSort,
 47+ $wgInterlanguageExtensionPrefix, $wgInterlanguageExtensionInterwiki,
 48+ $wgLanguageCode, $wgTitle, $wgMemc;
5349
5450 if(isset($wgInterlanguageExtensionPrefix)) {
5551 $param = "$wgInterlanguageExtensionPrefix$param";
5652 }
5753
58 - $res = false;
59 - if($a = Http::get("$wgInterlanguageExtensionApiUrl?action=query&prop=langlinks&lllimit=500&format=php&redirects&titles=".strtr($param,' ','_'))) {
 54+ $url = $wgInterlanguageExtensionApiUrl . "?action=query&prop=langlinks&" .
 55+ "lllimit=500&format=php&redirects&titles=" . strtr( $param, ' ', '_' );
 56+ $key = wfMemc( 'Interlanguage', md5( $url ) );
 57+ $res = $wgMemc->get( $key );
6058
61 - $a = @unserialize($a);
 59+ if ( !$res ) {
 60+ # be sure to set $res back to bool false, we do a strict compare below
 61+ $res = false;
 62+ $a = Http::get( $url );
 63+ $a = @unserialize( $a );
6264 if(isset($a['query']['pages']) && is_array($a['query']['pages'])) {
6365 $a = array_shift($a['query']['pages']);
6466
@@ -114,7 +116,8 @@
115117 }
116118 }
117119 }
118 -
 120+ # cache the final result so we can skip all of this
 121+ $wgMemc->set( $key, $res, time() + 3600 );
119122 return $res;
120123 }
121124

Status & tagging log