Daniel Kinzler has uploaded a new change for review. https://gerrit.wikimedia.org/r/179914
Change subject: Cache type ahead results for short prefixes. ...................................................................... Cache type ahead results for short prefixes. This causes wbsearchentities to cache results for short prefixes for 15 minutes. Bug: T78156 Change-Id: Ic7ab957ba8f2cfbcd8bdf0e840e8f981ffdf8240 --- M repo/includes/api/SearchEntities.php 1 file changed, 47 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/14/179914/1 diff --git a/repo/includes/api/SearchEntities.php b/repo/includes/api/SearchEntities.php index 2a464d4..9aa8e69 100644 --- a/repo/includes/api/SearchEntities.php +++ b/repo/includes/api/SearchEntities.php @@ -4,6 +4,7 @@ use ApiBase; use ApiMain; +use BagOStuff; use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\EntityIdParser; use Wikibase\DataModel\Entity\EntityIdParsingException; @@ -44,6 +45,23 @@ protected $idParser; /** + * @var BagOStuff + */ + private $cache; + + /** + * @var int + * @todo get from config + */ + private $cacheDuration = 900; // 15 min + + /** + * @var int + * @todo get from config + */ + private $cacheThreshold = 3; + + /** * @param ApiMain $mainModule * @param string $moduleName * @param string $modulePrefix @@ -56,6 +74,7 @@ //TODO: provide a mechanism to override the services $this->titleLookup = WikibaseRepo::getDefaultInstance()->getEntityTitleLookup(); $this->idParser = WikibaseRepo::getDefaultInstance()->getEntityIdParser(); + $this->cache = wfGetMainCache(); } /** @@ -100,6 +119,20 @@ return $ids; } + private function getCacheKey( array $params ) { + if ( mb_strlen( $params['search'] ) > $this->cacheThreshold ) { + return false; + } + + return wfMemcKey( 'wbsearchentities', + $params['type'], + $params['language'], + $params['search'], + $params['continue'], + $params['limit'] + ); + } + /** * Populates the search result returning the number of requested matches plus one additional * item for being able to determine if there would be any more results. @@ -114,6 +147,16 @@ */ private function getSearchEntries( array $params ) { wfProfileIn( __METHOD__ ); + + $cacheKey = $this->getCacheKey( $params ); + + if ( $cacheKey ) { + $entries = $this->cache->get( $cacheKey ); + + if ( is_array( $entries ) ) { + return $entries; + } + } $ids = array(); $required = $params['continue'] + $params['limit'] + 1; @@ -131,6 +174,10 @@ $entries = $this->getEntries( $ids, $params['search'], $params['type'], $params['language'] ); + if ( $cacheKey ) { + $this->cache->set( $cacheKey, $entries, $this->cacheDuration ); + } + wfProfileOut( __METHOD__ ); return $entries; } -- To view, visit https://gerrit.wikimedia.org/r/179914 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic7ab957ba8f2cfbcd8bdf0e840e8f981ffdf8240 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits