Merge pull request #21052 from nextcloud/backport/21050/stable19

[stable19] Caching and compression for app store requests
This commit is contained in:
Morris Jobke 2020-05-20 13:23:00 +02:00 committed by GitHub
commit 703d8f2adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 17 deletions

View File

@ -41,7 +41,7 @@ use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
abstract class Fetcher { abstract class Fetcher {
public const INVALIDATE_AFTER_SECONDS = 300; public const INVALIDATE_AFTER_SECONDS = 3600;
/** @var IAppData */ /** @var IAppData */
protected $appData; protected $appData;
@ -98,12 +98,11 @@ abstract class Fetcher {
$options = [ $options = [
'timeout' => 10, 'timeout' => 10,
'headers' => ['Accept-Encoding' => 'gzip'],
]; ];
if ($ETag !== '') { if ($ETag !== '') {
$options['headers'] = [ $options['headers']['If-None-Match'] = $ETag;
'If-None-Match' => $ETag,
];
} }
$client = $this->clientService->newClient(); $client = $this->clientService->newClient();
@ -153,7 +152,7 @@ abstract class Fetcher {
// No caching when the version has been updated // No caching when the version has been updated
if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) { if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
// If the timestamp is older than 300 seconds request the files new // If the timestamp is older than 3600 seconds request the files new
if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) { if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
return $jsonBlob['data']; return $jsonBlob['data'];
} }

View File

@ -239,7 +239,7 @@ abstract class FetcherBase extends TestCase {
$this->timeFactory $this->timeFactory
->expects($this->at(0)) ->expects($this->at(0))
->method('getTime') ->method('getTime')
->willReturn(1501); ->willReturn(4801);
$client = $this->createMock(IClient::class); $client = $this->createMock(IClient::class);
$this->clientService $this->clientService
->expects($this->once()) ->expects($this->once())
@ -249,7 +249,15 @@ abstract class FetcherBase extends TestCase {
$client $client
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with($this->endpoint) ->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willReturn($response); ->willReturn($response);
$response $response
->expects($this->once()) ->expects($this->once())
@ -342,7 +350,15 @@ abstract class FetcherBase extends TestCase {
$client $client
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with($this->endpoint) ->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willReturn($response); ->willReturn($response);
$response $response
->expects($this->once()) ->expects($this->once())
@ -430,7 +446,15 @@ abstract class FetcherBase extends TestCase {
$client $client
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with($this->endpoint) ->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willReturn($response); ->willReturn($response);
$response $response
->expects($this->once()) ->expects($this->once())
@ -495,7 +519,15 @@ abstract class FetcherBase extends TestCase {
$client $client
->expects($this->once()) ->expects($this->once())
->method('get') ->method('get')
->with($this->endpoint) ->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willThrowException(new \Exception()); ->willThrowException(new \Exception());
$this->assertSame([], $this->fetcher->get()); $this->assertSame([], $this->fetcher->get());
@ -533,11 +565,11 @@ abstract class FetcherBase extends TestCase {
$this->timeFactory $this->timeFactory
->expects($this->at(0)) ->expects($this->at(0))
->method('getTime') ->method('getTime')
->willReturn(1501); ->willReturn(4801);
$this->timeFactory $this->timeFactory
->expects($this->at(1)) ->expects($this->at(1))
->method('getTime') ->method('getTime')
->willReturn(1502); ->willReturn(4802);
$client = $this->createMock(IClient::class); $client = $this->createMock(IClient::class);
$this->clientService $this->clientService
->expects($this->once()) ->expects($this->once())
@ -552,14 +584,15 @@ abstract class FetcherBase extends TestCase {
$this->equalTo([ $this->equalTo([
'timeout' => 10, 'timeout' => 10,
'headers' => [ 'headers' => [
'If-None-Match' => '"myETag"' 'Accept-Encoding' => 'gzip',
'If-None-Match' => '"myETag"',
] ]
]) ])
)->willReturn($response); )->willReturn($response);
$response->method('getStatusCode') $response->method('getStatusCode')
->willReturn(304); ->willReturn(304);
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}'; $newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
$file $file
->expects($this->at(1)) ->expects($this->at(1))
->method('putContent') ->method('putContent')
@ -624,6 +657,7 @@ abstract class FetcherBase extends TestCase {
$this->equalTo([ $this->equalTo([
'timeout' => 10, 'timeout' => 10,
'headers' => [ 'headers' => [
'Accept-Encoding' => 'gzip',
'If-None-Match' => '"myETag"', 'If-None-Match' => '"myETag"',
] ]
]) ])
@ -638,7 +672,7 @@ abstract class FetcherBase extends TestCase {
$response->method('getHeader') $response->method('getHeader')
->with($this->equalTo('ETag')) ->with($this->equalTo('ETag'))
->willReturn('"newETag"'); ->willReturn('"newETag"');
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"newETag\""}'; $fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
$file $file
->expects($this->at(1)) ->expects($this->at(1))
->method('putContent') ->method('putContent')
@ -650,11 +684,11 @@ abstract class FetcherBase extends TestCase {
$this->timeFactory $this->timeFactory
->expects($this->at(0)) ->expects($this->at(0))
->method('getTime') ->method('getTime')
->willReturn(1501); ->willReturn(4801);
$this->timeFactory $this->timeFactory
->expects($this->at(1)) ->expects($this->at(1))
->method('getTime') ->method('getTime')
->willReturn(1502); ->willReturn(4802);
$expected = [ $expected = [
[ [
@ -710,6 +744,9 @@ abstract class FetcherBase extends TestCase {
$this->equalTo($this->endpoint), $this->equalTo($this->endpoint),
$this->equalTo([ $this->equalTo([
'timeout' => 10, 'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
],
]) ])
) )
->willReturn($response); ->willReturn($response);