From 6bb95c20fcc77c5b64607f3c9705f03603484f77 Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Tue, 22 May 2018 16:44:19 +0200 Subject: [PATCH] FEATURE: Introduce a query result cache --- Classes/Api/MediaWikiClient.php | 40 +++++++++++++++++--- Classes/AssetSource/MediaWikiAssetSource.php | 5 ++- Configuration/Caches.yaml | 3 ++ Configuration/Objects.yaml | 9 +++++ Configuration/Settings.yaml | 1 + Readme.md | 8 ++-- 6 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 Configuration/Caches.yaml create mode 100644 Configuration/Objects.yaml diff --git a/Classes/Api/MediaWikiClient.php b/Classes/Api/MediaWikiClient.php index 96ebc77..8732cec 100644 --- a/Classes/Api/MediaWikiClient.php +++ b/Classes/Api/MediaWikiClient.php @@ -9,8 +9,9 @@ * source code. */ -use DL\AssetSource\MediaWiki\Api\Dto\ImageSearchResult; use Neos\Flow\Annotations as Flow; +use DL\AssetSource\MediaWiki\Api\Dto\ImageSearchResult; +use Neos\Cache\Frontend\VariableFrontend; use GuzzleHttp\Client; use Neos\Flow\Log\PsrSystemLoggerInterface; use Neos\Utility\Arrays; @@ -49,13 +50,27 @@ class MediaWikiClient */ protected $logger; + /** + * @var VariableFrontend + * @Flow\Inject + */ + protected $queryResultCache; + + /** + * @var bool + */ + protected $useQueryResultCache = false; + /** * MediaWikiClient constructor. + * * @param string $domain + * @param bool $useQueryResultCache */ - public function __construct(string $domain) + public function __construct(string $domain, bool $useQueryResultCache) { $this->domain = $domain; + $this->useQueryResultCache = $useQueryResultCache; } /** @@ -123,7 +138,7 @@ public function getAssetDetails(ImageSearchResult $imageSearchResult, $thumbSize $pages = Arrays::getValueByPath($assetDetails, 'query.pages'); foreach ($pages as $key => $page) { - if(!isset($page['imageinfo'])) { + if (!isset($page['imageinfo'])) { continue; } @@ -131,7 +146,7 @@ public function getAssetDetails(ImageSearchResult $imageSearchResult, $thumbSize $items[$identifier] = current($page['imageinfo']); $items[$identifier]['identifier'] = $identifier; - $items[$identifier]['filename'] = explode(':',$page['title'])[1]; + $items[$identifier]['filename'] = explode(':', $page['title'])[1]; } return new MediaWikiQueryResult($items, $imageSearchResult->getTotalResults()); @@ -145,11 +160,24 @@ public function getAssetDetails(ImageSearchResult $imageSearchResult, $thumbSize public function executeQuery(array $data): array { $queryUrl = $this->buildQueryUrl($data); + + if ($this->useQueryResultCache) { + $queryHash = sha1($queryUrl); + if ($this->queryResultCache->has($queryHash)) { + $this->logger->debug('Received result for API-Query "' . $queryUrl . '" from cache'); + return $this->queryResultCache->get($queryHash); + } + } + $result = $this->getClient()->request('GET', $queryUrl); + $resultData = \GuzzleHttp\json_decode($result->getBody(), true); - $this->logger->debug('Executed Query to mediaWiki API "' . $queryUrl . '"'); + $this->logger->debug('Executed Query to mediawiki API "' . $queryUrl . '"'); + if ($this->useQueryResultCache) { + $this->queryResultCache->set($queryHash, $resultData); + } - return \GuzzleHttp\json_decode($result->getBody(), true); + return $resultData; } /** diff --git a/Classes/AssetSource/MediaWikiAssetSource.php b/Classes/AssetSource/MediaWikiAssetSource.php index 41bcbde..034d316 100644 --- a/Classes/AssetSource/MediaWikiAssetSource.php +++ b/Classes/AssetSource/MediaWikiAssetSource.php @@ -99,7 +99,10 @@ public function getAssetProxyRepository(): AssetProxyRepositoryInterface public function getMediaWikiClient(): MediaWikiClient { if ($this->mediaWikiClient === null) { - $this->mediaWikiClient = new MediaWikiClient($this->getOption('domain')); + $this->mediaWikiClient = new MediaWikiClient( + $this->getOption('domain'), + $this->getOption('useQueryResultCache') ?? false + ); } return $this->mediaWikiClient; diff --git a/Configuration/Caches.yaml b/Configuration/Caches.yaml new file mode 100644 index 0000000..5651485 --- /dev/null +++ b/Configuration/Caches.yaml @@ -0,0 +1,3 @@ +DlAssetSourceMediaWiki_QueryResultCache: + frontend: Neos\Cache\Frontend\VariableFrontend + defaultLifetime: 86400 diff --git a/Configuration/Objects.yaml b/Configuration/Objects.yaml new file mode 100644 index 0000000..16c3c59 --- /dev/null +++ b/Configuration/Objects.yaml @@ -0,0 +1,9 @@ +DL\AssetSource\MediaWiki\Api\MediaWikiClient: + properties: + queryResultCache: + object: + factoryObjectName: Neos\Flow\Cache\CacheManager + factoryMethodName: getCache + arguments: + 1: + value: DlAssetSourceMediaWiki_QueryResultCache diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 8b4c536..60bcd05 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -6,6 +6,7 @@ Neos: assetSourceOptions: domain: en.wikipedia.org searchStrategy: DL\AssetSource\MediaWiki\Api\SearchStrategies\ArticleSearchStrategy + useQueryResultCache: true excludedIdentifierPatterns: - '*Wikiquote-logo.svg' - '*Wikinews-logo.svg' diff --git a/Readme.md b/Readme.md index a952862..1fe9c85 100644 --- a/Readme.md +++ b/Readme.md @@ -6,7 +6,7 @@ This asset source uses the public API of [MediaWiki](https://www.mediawiki.org/w ## Installation -Just Install the package via composer +Install the package via composer: composer require dl/assetsource-mediawiki @@ -31,9 +31,9 @@ Just Install the package via composer ## Search Strategies -Searching in the wikipedia for images can be kind of tricky. First there is not only one wikipedia instance, but one for each available language. Second an image can be stored in the language specific wikipedia or in Wikimedia Commons and included from there. +Searching in the wikipedia for images is a bit tricky. First there is not only one wikipedia instance, but one for each available language. Second an image can be stored in the language specific wikipedia or in Wikimedia Commons and included from there. -I implemented two different search strategies with different pros and cons. +The package brings two different search strategies with different pros and cons. ### Direct Image Search Strategy @@ -53,4 +53,4 @@ Please take care of the [correct attribution](https://wiki.creativecommons.org/w ## Known Issues -See the issue list for [known issues](https://github.com/daniellienert/assetsource-mediawiki/issues) an missing features. \ No newline at end of file +See the issue list for [known issues](https://github.com/daniellienert/assetsource-mediawiki/issues) and missing features.