From affa8517b6a307c1ebd827397bab839f3c438bb5 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 21 May 2008 23:00:49 +0000 Subject: [PATCH] Cache image API lookups in-process and, if available, in memcached for up to 1 hour. Cuts down on duplicate lookups... --- includes/filerepo/ForeignAPIRepo.php | 39 +++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index b08c47a851..43b2ab1058 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -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 ); -- 2.20.1