Search suggestion API requests make up a substantial portion of our total apache...
authorTim Starling <tstarling@users.mediawiki.org>
Sun, 14 Feb 2010 23:52:45 +0000 (23:52 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sun, 14 Feb 2010 23:52:45 +0000 (23:52 +0000)
* 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.

includes/DefaultSettings.php
includes/api/ApiMain.php
includes/api/ApiOpenSearch.php

index 2c964db..c38c380 100644 (file)
@@ -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
  *
index 8d083ce..ae31c2c 100644 (file)
@@ -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();
index 606005c..567f36b 100644 (file)
@@ -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 );