From ae75c694dc318c72c449eb134d9eff0ea34e0ad6 Mon Sep 17 00:00:00 2001 From: Mark Holmquist Date: Tue, 8 Oct 2013 13:11:54 -0700 Subject: [PATCH] Quick refactor of the http logic in ForeignAPIRepo Automatic caching, will enable slightly smaller patches elsewhere. Resetting after failing at a rebase. Change-Id: I6849107a3af773687a31155b23a2d7b4d7a60645 --- includes/filerepo/ForeignAPIRepo.php | 67 +++++++++++++++++++--------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index 02d83bb892..eb160932de 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -159,33 +159,18 @@ class ForeignAPIRepo extends FileRepo { 'action' => 'query', 'redirects' => 'true' ) ); + if ( !isset( $query['uselang'] ) ) { // uselang is unset or null $query['uselang'] = $wgLanguageCode; } - if ( $this->mApiBase ) { - $url = wfAppendQuery( $this->mApiBase, $query ); - } else { - $url = $this->makeUrl( $query, 'api' ); - } - if ( !isset( $this->mQueryCache[$url] ) ) { - $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) ); - $data = $wgMemc->get( $key ); - if ( !$data ) { - $data = self::httpGet( $url ); - if ( !$data ) { - return null; - } - $wgMemc->set( $key, $data, 3600 ); - } + $data = $this->httpGetCached( 'Metadata', $query ); - if ( count( $this->mQueryCache ) > 100 ) { - // Keep the cache from growing infinitely - $this->mQueryCache = array(); - } - $this->mQueryCache[$url] = $data; + if ( $data ) { + return FormatJson::decode( $data, true ); + } else { + return null; } - return FormatJson::decode( $this->mQueryCache[$url], true ); } /** @@ -466,6 +451,46 @@ class ForeignAPIRepo extends FileRepo { } } + /** + * HTTP GET request to a mediawiki API (with caching) + * @param $target string Used in cache key creation, mostly + * @param $query array The query parameters for the API request + * @param $cacheTTL int Time to live for the memcached caching + */ + public function httpGetCached( $target, $query, $cacheTTL = 3600 ) { + if ( $this->mApiBase ) { + $url = wfAppendQuery( $this->mApiBase, $query ); + } else { + $url = $this->makeUrl( $query, 'api' ); + } + + if ( !isset( $this->mQueryCache[$url] ) ) { + global $wgMemc; + + $key = $this->getLocalCacheKey( get_class( $this ), $target, md5( $url ) ); + $data = $wgMemc->get( $key ); + + if ( !$data ) { + $data = self::httpGet( $url ); + + if ( !$data ) { + return null; + } + + $wgMemc->set( $key, $data, $cacheTTL ); + } + + if ( count( $this->mQueryCache ) > 100 ) { + // Keep the cache from growing infinitely + $this->mQueryCache = array(); + } + + $this->mQueryCache[$url] = $data; + } + + return $this->mQueryCache[$url]; + } + /** * @param $callback Array|string * @throws MWException -- 2.20.1