-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a main switch for caching in CachingDownloader. #4274
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,20 +47,6 @@ class CachingDownloader implements \VuFindHttp\HttpServiceAwareInterface | |
{ | ||
use \VuFindHttp\HttpServiceAwareTrait; | ||
|
||
/** | ||
* CacheManager to update caches if necessary. | ||
* | ||
* @var CacheManager | ||
*/ | ||
protected $cacheManager; | ||
|
||
/** | ||
* ConfigManager to get additional settings if necessary. | ||
* | ||
* @var ConfigManager | ||
*/ | ||
protected $configManager; | ||
|
||
/** | ||
* Cache to use for downloads | ||
* | ||
|
@@ -87,21 +73,26 @@ class CachingDownloader implements \VuFindHttp\HttpServiceAwareInterface | |
* | ||
* @param CacheManager $cacheManager VuFind Cache Manager | ||
* @param ConfigManager $configManager VuFind Config Manager | ||
* @param bool $cacheEnabled Main toggle for enabling caching | ||
*/ | ||
public function __construct(CacheManager $cacheManager, ConfigManager $configManager) | ||
{ | ||
$this->cacheManager = $cacheManager; | ||
$this->configManager = $configManager; | ||
public function __construct( | ||
protected CacheManager $cacheManager, | ||
protected ConfigManager $configManager, | ||
protected bool $cacheEnabled | ||
) { | ||
$this->setUpCache('default'); | ||
} | ||
|
||
/** | ||
* Get cache and initialize it, if necessary. | ||
* | ||
* @return StorageInterface | ||
* @return ?StorageInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add "Return null if cache is disabled." to the description above. |
||
*/ | ||
protected function getDownloaderCache() | ||
protected function getDownloaderCache(): ?StorageInterface | ||
{ | ||
if (!$this->cacheEnabled) { | ||
return null; | ||
} | ||
if ($this->cache == null) { | ||
$cacheName = $this->cacheManager->addDownloaderCache( | ||
$this->cacheId, | ||
|
@@ -151,7 +142,7 @@ public function download( | |
$cache = $this->getDownloaderCache(); | ||
$cacheItemKey = md5($url . http_build_query($params)); | ||
|
||
if ($cache->hasItem($cacheItemKey)) { | ||
if ($cache && $cache->hasItem($cacheItemKey)) { | ||
return $cache->getItem($cacheItemKey); | ||
} | ||
|
||
|
@@ -180,7 +171,9 @@ public function download( | |
|
||
$finalValue = $decodeCallback !== null | ||
? $decodeCallback($response, $url) : $response->getBody(); | ||
$cache->addItem($cacheItemKey, $finalValue); | ||
if ($cache) { | ||
$cache->addItem($cacheItemKey, $finalValue); | ||
} | ||
return $finalValue; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,12 +45,29 @@ | |
*/ | ||
class CachingDownloaderTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Data provider for testDownload | ||
* | ||
* @return array | ||
*/ | ||
public static function downloadProvider(): array | ||
{ | ||
return [ | ||
'cache enabled' => [true], | ||
'cache disabled' => [false], | ||
]; | ||
} | ||
|
||
/** | ||
* Test a download | ||
* | ||
* @param bool $cacheEnabled Is the cache enabled? | ||
* | ||
* @return void | ||
* | ||
* @dataProvider downloadProvider | ||
*/ | ||
public function testDownload() | ||
public function testDownload(bool $cacheEnabled): void | ||
{ | ||
$container = new \VuFindTest\Container\MockContainer($this); | ||
|
||
|
@@ -75,27 +92,39 @@ public function testDownload() | |
$storage = $this->getMockBuilder(\Laminas\Cache\Storage\StorageInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$storage->expects($this->once())->method('hasItem')->with($testCacheKey)->willReturn(false); | ||
$storage->expects($this->once())->method('addItem')->with($testCacheKey, $testBody); | ||
|
||
$cacheManagerMock = $container->createMock(\VuFind\Cache\Manager::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we're changing code anyway, the line above could be simplified to use |
||
$cacheManagerMock | ||
->expects($this->once()) | ||
->method('addDownloaderCache') | ||
->with('default') | ||
->willReturn('downloader-default'); | ||
$cacheManagerMock | ||
->expects($this->once()) | ||
->method('getCache') | ||
->with('downloader-default') | ||
->willReturn($storage); | ||
|
||
if ($cacheEnabled) { | ||
$storage->expects($this->once())->method('hasItem')->with($testCacheKey)->willReturn(false); | ||
$storage->expects($this->once())->method('addItem')->with($testCacheKey, $testBody); | ||
|
||
$cacheManagerMock | ||
->expects($this->once()) | ||
->method('addDownloaderCache') | ||
->with('default') | ||
->willReturn('downloader-default'); | ||
$cacheManagerMock | ||
->expects($this->once()) | ||
->method('getCache') | ||
->with('downloader-default') | ||
->willReturn($storage); | ||
} else { | ||
$storage->expects($this->never())->method('hasItem'); | ||
$storage->expects($this->never())->method('addItem'); | ||
|
||
$cacheManagerMock | ||
->expects($this->never()) | ||
->method('addDownloaderCache'); | ||
$cacheManagerMock | ||
->expects($this->never()) | ||
->method('getCache'); | ||
} | ||
|
||
// configManager | ||
$configManagerMock = $this->createMock(\VuFind\Config\PluginManager::class); | ||
|
||
// downloader | ||
$downloader = new CachingDownloader($cacheManagerMock, $configManagerMock); | ||
$downloader = new CachingDownloader($cacheManagerMock, $configManagerMock, $cacheEnabled); | ||
$downloader->setHttpService($service); | ||
|
||
$body = $downloader->download( | ||
|
@@ -109,7 +138,7 @@ public function testDownload() | |
* | ||
* @return void | ||
*/ | ||
public function testException() | ||
public function testException(): void | ||
{ | ||
$this->expectException(HttpDownloadException::class); | ||
|
||
|
@@ -148,7 +177,7 @@ public function testException() | |
$configManagerMock = $this->createMock(\VuFind\Config\PluginManager::class); | ||
|
||
// downloader | ||
$downloader = new CachingDownloader($cacheManagerMock, $configManagerMock); | ||
$downloader = new CachingDownloader($cacheManagerMock, $configManagerMock, true); | ||
$downloader->setHttpService($service); | ||
|
||
$downloader->download( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth defaulting to true for stronger backward compatibility?