From: Tim Starling Date: Sun, 14 Feb 2010 23:52:45 +0000 (+0000) Subject: Search suggestion API requests make up a substantial portion of our total apache... X-Git-Tag: 1.31.0-rc.0~37760 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/operations/recherche.php?a=commitdiff_plain;h=278a489ffe278e84b57ea948c19f442a11be1448;p=lhc%2Fweb%2Fwiklou.git 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. --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 2c964dbc1b..c38c380ceb 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -2074,6 +2074,11 @@ $wgEnableMWSuggest = false; */ $wgEnableOpenSearchSuggest = true; +/** + * Expiry time for search suggestion responses + */ +$wgSearchSuggestCacheExpiry = 1200; + /** * Template for internal MediaWiki suggestion engine, defaults to API action=opensearch * diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 8d083ce272..ae31c2c844 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -126,6 +126,8 @@ class ApiMain extends ApiBase { private $mResult, $mAction, $mShowVersions, $mEnableWrite, $mRequest; private $mInternalMode, $mSquidMaxage, $mModule; + private $mCacheControl = array( 'must-revalidate' => true ); + /** * Constructs an instance of ApiMain that utilizes the module and format specified by $request. * @@ -214,7 +216,19 @@ class ApiMain extends ApiBase { * Set how long the response should be cached. */ public function setCacheMaxAge( $maxage ) { - $this->mSquidMaxage = $maxage; + $this->setCacheControl( array( + 'max-age' => $maxage, + 's-maxage' => $maxage + ) ); + } + + /** + * Set directives (key/value pairs) for the Cache-Control header. + * Boolean values will be formatted as such, by including or omitting + * without an equals sign. + */ + public function setCacheControl( $directives ) { + $this->mCacheControl = $directives + $this->mCacheControl; } /** @@ -282,21 +296,35 @@ class ApiMain extends ApiBase { $this->printResult( true ); } - if ( $this->mSquidMaxage == - 1 ) - { - // Nobody called setCacheMaxAge(), use the (s)maxage parameters - $smaxage = $this->getParameter( 'smaxage' ); - $maxage = $this->getParameter( 'maxage' ); + // If nobody called setCacheMaxAge(), use the (s)maxage parameters + if ( !isset( $this->mCacheControl['s-maxage'] ) ) { + $this->mCacheControl['s-maxage'] = $this->getParameter( 'smaxage' ); + } + if ( !isset( $this->mCacheControl['max-age'] ) ) { + $this->mCacheControl['max-age'] = $this->getParameter( 'maxage' ); } - else - $smaxage = $maxage = $this->mSquidMaxage; // Set the cache expiration at the last moment, as any errors may change the expiration. // if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch - $exp = min( $smaxage, $maxage ); + $exp = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] ); $expires = ( $exp == 0 ? 1 : time() + $exp ); header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expires ) ); - header( 'Cache-Control: s-maxage=' . $smaxage . ', must-revalidate, max-age=' . $maxage ); + + // Construct the Cache-Control header + $ccHeader = ''; + $separator = ''; + foreach ( $this->mCacheControl as $name => $value ) { + if ( is_bool( $value ) ) { + if ( $value ) { + $ccHeader .= $separator . $name; + } + } else { + $ccHeader .= $separator . "$name=$value"; + } + $separator = ', '; + } + + header( "Cache-Control: $ccHeader" ); if ( $this->mPrinter->getIsHtml() ) echo wfReportTime(); diff --git a/includes/api/ApiOpenSearch.php b/includes/api/ApiOpenSearch.php index 606005cbfd..567f36bc0f 100644 --- a/includes/api/ApiOpenSearch.php +++ b/includes/api/ApiOpenSearch.php @@ -42,7 +42,7 @@ class ApiOpenSearch extends ApiBase { } public function execute() { - global $wgEnableOpenSearchSuggest; + global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry; $params = $this->extractRequestParams(); $search = $params['search']; $limit = $params['limit']; @@ -55,7 +55,8 @@ class ApiOpenSearch extends ApiBase { else { // Open search results may be stored for a very long // time - $this->getMain()->setCacheMaxAge( 1200 ); + $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry ); + $this->getMain()->setCacheControl( array( 'must-revalidate' => false ) ); $srchres = PrefixSearch::titleSearch( $search, $limit, $namespaces );