Skip to content

Commit

Permalink
FEATURE: Introduce a query result cache
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellienert committed May 22, 2018
1 parent a0aceb7 commit 6bb95c2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
40 changes: 34 additions & 6 deletions Classes/Api/MediaWikiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -123,15 +138,15 @@ 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;
}

$identifier = str_replace(' ', '_', $page['title']);
$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());
Expand All @@ -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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Classes/AssetSource/MediaWikiAssetSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Configuration/Caches.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DlAssetSourceMediaWiki_QueryResultCache:
frontend: Neos\Cache\Frontend\VariableFrontend
defaultLifetime: 86400
9 changes: 9 additions & 0 deletions Configuration/Objects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DL\AssetSource\MediaWiki\Api\MediaWikiClient:
properties:
queryResultCache:
object:
factoryObjectName: Neos\Flow\Cache\CacheManager
factoryMethodName: getCache
arguments:
1:
value: DlAssetSourceMediaWiki_QueryResultCache
1 change: 1 addition & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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.
See the issue list for [known issues](https://github.com/daniellienert/assetsource-mediawiki/issues) and missing features.

0 comments on commit 6bb95c2

Please sign in to comment.