Merge pull request #21056 from nextcloud/backport/21050/stable16

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

View File

@ -39,7 +39,7 @@ use OCP\ILogger;
use OCP\Util;
abstract class Fetcher {
const INVALIDATE_AFTER_SECONDS = 300;
const INVALIDATE_AFTER_SECONDS = 3600;
/** @var IAppData */
protected $appData;
@ -96,12 +96,11 @@ abstract class Fetcher {
$options = [
'timeout' => 10,
'headers' => ['Accept-Encoding' => 'gzip'],
];
if ($ETag !== '') {
$options['headers'] = [
'If-None-Match' => $ETag,
];
$options['headers']['If-None-Match'] = $ETag;
}
$client = $this->clientService->newClient();
@ -151,7 +150,7 @@ abstract class Fetcher {
// No caching when the version has been updated
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)) {
return $jsonBlob['data'];
}

View File

@ -234,7 +234,7 @@ abstract class FetcherBase extends TestCase {
$this->timeFactory
->expects($this->at(0))
->method('getTime')
->willReturn(1501);
->willReturn(4801);
$client = $this->createMock(IClient::class);
$this->clientService
->expects($this->once())
@ -244,7 +244,15 @@ abstract class FetcherBase extends TestCase {
$client
->expects($this->once())
->method('get')
->with($this->endpoint)
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willReturn($response);
$response
->expects($this->once())
@ -332,7 +340,15 @@ abstract class FetcherBase extends TestCase {
$client
->expects($this->once())
->method('get')
->with($this->endpoint)
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willReturn($response);
$response
->expects($this->once())
@ -415,7 +431,15 @@ abstract class FetcherBase extends TestCase {
$client
->expects($this->once())
->method('get')
->with($this->endpoint)
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willReturn($response);
$response
->expects($this->once())
@ -480,7 +504,15 @@ abstract class FetcherBase extends TestCase {
$client
->expects($this->once())
->method('get')
->with($this->endpoint)
->with(
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
]
])
)
->willThrowException(new \Exception());
$this->assertSame([], $this->fetcher->get());
@ -518,11 +550,11 @@ abstract class FetcherBase extends TestCase {
$this->timeFactory
->expects($this->at(0))
->method('getTime')
->willReturn(1501);
->willReturn(4801);
$this->timeFactory
->expects($this->at(1))
->method('getTime')
->willReturn(1502);
->willReturn(4802);
$client = $this->createMock(IClient::class);
$this->clientService
->expects($this->once())
@ -537,14 +569,15 @@ abstract class FetcherBase extends TestCase {
$this->equalTo([
'timeout' => 10,
'headers' => [
'If-None-Match' => '"myETag"'
'Accept-Encoding' => 'gzip',
'If-None-Match' => '"myETag"',
]
])
)->willReturn($response);
$response->method('getStatusCode')
->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
->expects($this->at(1))
->method('putContent')
@ -609,6 +642,7 @@ abstract class FetcherBase extends TestCase {
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
'If-None-Match' => '"myETag"',
]
])
@ -623,7 +657,7 @@ abstract class FetcherBase extends TestCase {
$response->method('getHeader')
->with($this->equalTo('ETag'))
->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
->expects($this->at(1))
->method('putContent')
@ -635,11 +669,11 @@ abstract class FetcherBase extends TestCase {
$this->timeFactory
->expects($this->at(0))
->method('getTime')
->willReturn(1501);
->willReturn(4801);
$this->timeFactory
->expects($this->at(1))
->method('getTime')
->willReturn(1502);
->willReturn(4802);
$expected = [
[
@ -695,6 +729,9 @@ abstract class FetcherBase extends TestCase {
$this->equalTo($this->endpoint),
$this->equalTo([
'timeout' => 10,
'headers' => [
'Accept-Encoding' => 'gzip',
],
])
)
->willReturn($response);