Cache image API lookups in-process and, if available, in memcached for up to 1 hour.
authorBrion Vibber <brion@users.mediawiki.org>
Wed, 21 May 2008 23:00:49 +0000 (23:00 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Wed, 21 May 2008 23:00:49 +0000 (23:00 +0000)
Cuts down on duplicate lookups...

includes/filerepo/ForeignAPIRepo.php

index b08c47a..43b2ab1 100644 (file)
@@ -17,6 +17,8 @@
  * @ingroup FileRepo
  */
 class ForeignAPIRepo extends FileRepo {
+       protected $mQueryCache = array();
+       
        function __construct( $info ) {
                parent::__construct( $info );
                $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php
@@ -47,6 +49,21 @@ class ForeignAPIRepo extends FileRepo {
        }
        
        protected function queryImage( $query ) {
+               $data = $this->fetchImageQuery( $query );
+               
+               if( isset( $data['query']['pages'] ) ) {
+                       foreach( $data['query']['pages'] as $pageid => $info ) {
+                               if( isset( $info['imageinfo'][0] ) ) {
+                                       return $info['imageinfo'][0];
+                               }
+                       }
+               }
+               return false;
+       }
+       
+       protected function fetchImageQuery( $query ) {
+               global $wgMemc;
+               
                $url = $this->mApiBase .
                        '?' .
                        wfArrayToCgi(
@@ -55,23 +72,27 @@ class ForeignAPIRepo extends FileRepo {
                                                'format' => 'json',
                                                'action' => 'query',
                                                'prop' => 'imageinfo' ) ) );
-               $json = Http::get( $url );
-               $data = json_decode( $json, true );
                
-               if( isset( $data['query']['pages'] ) ) {
-                       foreach( $data['query']['pages'] as $pageid => $info ) {
-                               if( isset( $info['imageinfo'][0] ) ) {
-                                       return $info['imageinfo'][0];
-                               }
+               if( !isset( $this->mQueryCache[$url] ) ) {
+                       $key = wfMemcKey( 'ForeignAPIRepo', $url );
+                       $data = $wgMemc->get( $key );
+                       if( !$data ) {
+                               $data = Http::get( $url );
+                               $wgMemc->set( $key, $data, 3600 );
                        }
+
+                       if( count( $this->mQueryCache ) > 100 ) {
+                               // Keep the cache from growing infinitely
+                               $this->mQueryCache = array();
+                       }
+                       $this->mQueryCache[$url] = $data;
                }
-               return false;
+               return json_decode( $this->mQueryCache[$url], true );
        }
        
        function findFile( $title, $time = false ) {
                $info = $this->queryImage( array(
                        'titles' => 'Image:' . $title->getText(),
-                       'prop' => 'imageinfo',
                        'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mimetype' ) );
                if( $info ) {
                        return new ForeignAPIFile( $title, $this, $info );