r62486 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62485‎ | r62486 | r62487 >
Date:23:52, 14 February 2010
Author:tstarling
Status:ok
Tags:
Comment:
Search suggestion API requests make up a substantial portion of our total apache load and thus deserve some optimisation:
* Made the cache expiry time configurable via $wgSearchSuggestCacheExpiry
* Removed must-revalidate from the Cache-Control of such requests. Introduced a generic interface to ApiMain for doing that.
Modified paths:
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiOpenSearch.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiMain.php
@@ -126,6 +126,8 @@
127127 private $mResult, $mAction, $mShowVersions, $mEnableWrite, $mRequest;
128128 private $mInternalMode, $mSquidMaxage, $mModule;
129129
 130+ private $mCacheControl = array( 'must-revalidate' => true );
 131+
130132 /**
131133 * Constructs an instance of ApiMain that utilizes the module and format specified by $request.
132134 *
@@ -214,10 +216,22 @@
215217 * Set how long the response should be cached.
216218 */
217219 public function setCacheMaxAge( $maxage ) {
218 - $this->mSquidMaxage = $maxage;
 220+ $this->setCacheControl( array(
 221+ 'max-age' => $maxage,
 222+ 's-maxage' => $maxage
 223+ ) );
219224 }
220225
221226 /**
 227+ * Set directives (key/value pairs) for the Cache-Control header.
 228+ * Boolean values will be formatted as such, by including or omitting
 229+ * without an equals sign.
 230+ */
 231+ public function setCacheControl( $directives ) {
 232+ $this->mCacheControl = $directives + $this->mCacheControl;
 233+ }
 234+
 235+ /**
222236 * Create an instance of an output formatter by its name
223237 */
224238 public function createPrinterByName( $format ) {
@@ -282,22 +296,36 @@
283297 $this->printResult( true );
284298 }
285299
286 - if ( $this->mSquidMaxage == - 1 )
287 - {
288 - // Nobody called setCacheMaxAge(), use the (s)maxage parameters
289 - $smaxage = $this->getParameter( 'smaxage' );
290 - $maxage = $this->getParameter( 'maxage' );
 300+ // If nobody called setCacheMaxAge(), use the (s)maxage parameters
 301+ if ( !isset( $this->mCacheControl['s-maxage'] ) ) {
 302+ $this->mCacheControl['s-maxage'] = $this->getParameter( 'smaxage' );
291303 }
292 - else
293 - $smaxage = $maxage = $this->mSquidMaxage;
 304+ if ( !isset( $this->mCacheControl['max-age'] ) ) {
 305+ $this->mCacheControl['max-age'] = $this->getParameter( 'maxage' );
 306+ }
294307
295308 // Set the cache expiration at the last moment, as any errors may change the expiration.
296309 // if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch
297 - $exp = min( $smaxage, $maxage );
 310+ $exp = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] );
298311 $expires = ( $exp == 0 ? 1 : time() + $exp );
299312 header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expires ) );
300 - header( 'Cache-Control: s-maxage=' . $smaxage . ', must-revalidate, max-age=' . $maxage );
301313
 314+ // Construct the Cache-Control header
 315+ $ccHeader = '';
 316+ $separator = '';
 317+ foreach ( $this->mCacheControl as $name => $value ) {
 318+ if ( is_bool( $value ) ) {
 319+ if ( $value ) {
 320+ $ccHeader .= $separator . $name;
 321+ }
 322+ } else {
 323+ $ccHeader .= $separator . "$name=$value";
 324+ }
 325+ $separator = ', ';
 326+ }
 327+
 328+ header( "Cache-Control: $ccHeader" );
 329+
302330 if ( $this->mPrinter->getIsHtml() )
303331 echo wfReportTime();
304332
Index: trunk/phase3/includes/api/ApiOpenSearch.php
@@ -42,7 +42,7 @@
4343 }
4444
4545 public function execute() {
46 - global $wgEnableOpenSearchSuggest;
 46+ global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
4747 $params = $this->extractRequestParams();
4848 $search = $params['search'];
4949 $limit = $params['limit'];
@@ -55,7 +55,8 @@
5656 else {
5757 // Open search results may be stored for a very long
5858 // time
59 - $this->getMain()->setCacheMaxAge( 1200 );
 59+ $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
 60+ $this->getMain()->setCacheControl( array( 'must-revalidate' => false ) );
6061
6162 $srchres = PrefixSearch::titleSearch( $search, $limit,
6263 $namespaces );
Index: trunk/phase3/includes/DefaultSettings.php
@@ -2075,6 +2075,11 @@
20762076 $wgEnableOpenSearchSuggest = true;
20772077
20782078 /**
 2079+ * Expiry time for search suggestion responses
 2080+ */
 2081+$wgSearchSuggestCacheExpiry = 1200;
 2082+
 2083+/**
20792084 * Template for internal MediaWiki suggestion engine, defaults to API action=opensearch
20802085 *
20812086 * Placeholders: {searchTerms}, {namespaces}, {dbname}

Follow-up revisions

RevisionCommit summaryAuthorDate
r62548wmf-deployment: MFT r62486catrope21:26, 15 February 2010
r62549Fix broken comma logic in r62486catrope21:34, 15 February 2010
r69986* Merged r69932 from REL1_16: API cache header fix. The base was quite differ...tstarling03:57, 27 July 2010

Status & tagging log