Skip to content

Commit 9ad2b2f

Browse files
authored
Merge pull request #21056 from nextcloud/backport/21050/stable16
[stable16] Caching and compression for app store requests
2 parents ac9c0ea + 758f343 commit 9ad2b2f

2 files changed

Lines changed: 53 additions & 17 deletions

File tree

lib/private/App/AppStore/Fetcher/Fetcher.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use OCP\Util;
4040

4141
abstract class Fetcher {
42-
const INVALIDATE_AFTER_SECONDS = 300;
42+
const INVALIDATE_AFTER_SECONDS = 3600;
4343

4444
/** @var IAppData */
4545
protected $appData;
@@ -96,12 +96,11 @@ protected function fetch($ETag, $content) {
9696

9797
$options = [
9898
'timeout' => 10,
99+
'headers' => ['Accept-Encoding' => 'gzip'],
99100
];
100101

101102
if ($ETag !== '') {
102-
$options['headers'] = [
103-
'If-None-Match' => $ETag,
104-
];
103+
$options['headers']['If-None-Match'] = $ETag;
105104
}
106105

107106
$client = $this->clientService->newClient();
@@ -151,7 +150,7 @@ public function get() {
151150
// No caching when the version has been updated
152151
if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
153152

154-
// If the timestamp is older than 300 seconds request the files new
153+
// If the timestamp is older than 3600 seconds request the files new
155154
if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - self::INVALIDATE_AFTER_SECONDS)) {
156155
return $jsonBlob['data'];
157156
}

tests/lib/App/AppStore/Fetcher/FetcherBase.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
234234
$this->timeFactory
235235
->expects($this->at(0))
236236
->method('getTime')
237-
->willReturn(1501);
237+
->willReturn(4801);
238238
$client = $this->createMock(IClient::class);
239239
$this->clientService
240240
->expects($this->once())
@@ -244,7 +244,15 @@ public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
244244
$client
245245
->expects($this->once())
246246
->method('get')
247-
->with($this->endpoint)
247+
->with(
248+
$this->equalTo($this->endpoint),
249+
$this->equalTo([
250+
'timeout' => 10,
251+
'headers' => [
252+
'Accept-Encoding' => 'gzip',
253+
]
254+
])
255+
)
248256
->willReturn($response);
249257
$response
250258
->expects($this->once())
@@ -332,7 +340,15 @@ public function testGetWithAlreadyExistingFileAndNoVersion() {
332340
$client
333341
->expects($this->once())
334342
->method('get')
335-
->with($this->endpoint)
343+
->with(
344+
$this->equalTo($this->endpoint),
345+
$this->equalTo([
346+
'timeout' => 10,
347+
'headers' => [
348+
'Accept-Encoding' => 'gzip',
349+
]
350+
])
351+
)
336352
->willReturn($response);
337353
$response
338354
->expects($this->once())
@@ -415,7 +431,15 @@ public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
415431
$client
416432
->expects($this->once())
417433
->method('get')
418-
->with($this->endpoint)
434+
->with(
435+
$this->equalTo($this->endpoint),
436+
$this->equalTo([
437+
'timeout' => 10,
438+
'headers' => [
439+
'Accept-Encoding' => 'gzip',
440+
]
441+
])
442+
)
419443
->willReturn($response);
420444
$response
421445
->expects($this->once())
@@ -480,7 +504,15 @@ public function testGetWithExceptionInClient() {
480504
$client
481505
->expects($this->once())
482506
->method('get')
483-
->with($this->endpoint)
507+
->with(
508+
$this->equalTo($this->endpoint),
509+
$this->equalTo([
510+
'timeout' => 10,
511+
'headers' => [
512+
'Accept-Encoding' => 'gzip',
513+
]
514+
])
515+
)
484516
->willThrowException(new \Exception());
485517

486518
$this->assertSame([], $this->fetcher->get());
@@ -518,11 +550,11 @@ public function testGetMatchingETag() {
518550
$this->timeFactory
519551
->expects($this->at(0))
520552
->method('getTime')
521-
->willReturn(1501);
553+
->willReturn(4801);
522554
$this->timeFactory
523555
->expects($this->at(1))
524556
->method('getTime')
525-
->willReturn(1502);
557+
->willReturn(4802);
526558
$client = $this->createMock(IClient::class);
527559
$this->clientService
528560
->expects($this->once())
@@ -537,14 +569,15 @@ public function testGetMatchingETag() {
537569
$this->equalTo([
538570
'timeout' => 10,
539571
'headers' => [
540-
'If-None-Match' => '"myETag"'
572+
'Accept-Encoding' => 'gzip',
573+
'If-None-Match' => '"myETag"',
541574
]
542575
])
543576
)->willReturn($response);
544577
$response->method('getStatusCode')
545578
->willReturn(304);
546579

547-
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
580+
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
548581
$file
549582
->expects($this->at(1))
550583
->method('putContent')
@@ -609,6 +642,7 @@ public function testGetNoMatchingETag() {
609642
$this->equalTo([
610643
'timeout' => 10,
611644
'headers' => [
645+
'Accept-Encoding' => 'gzip',
612646
'If-None-Match' => '"myETag"',
613647
]
614648
])
@@ -623,7 +657,7 @@ public function testGetNoMatchingETag() {
623657
$response->method('getHeader')
624658
->with($this->equalTo('ETag'))
625659
->willReturn('"newETag"');
626-
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
660+
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
627661
$file
628662
->expects($this->at(1))
629663
->method('putContent')
@@ -635,11 +669,11 @@ public function testGetNoMatchingETag() {
635669
$this->timeFactory
636670
->expects($this->at(0))
637671
->method('getTime')
638-
->willReturn(1501);
672+
->willReturn(4801);
639673
$this->timeFactory
640674
->expects($this->at(1))
641675
->method('getTime')
642-
->willReturn(1502);
676+
->willReturn(4802);
643677

644678
$expected = [
645679
[
@@ -695,6 +729,9 @@ public function testFetchAfterUpgradeNoETag() {
695729
$this->equalTo($this->endpoint),
696730
$this->equalTo([
697731
'timeout' => 10,
732+
'headers' => [
733+
'Accept-Encoding' => 'gzip',
734+
],
698735
])
699736
)
700737
->willReturn($response);

0 commit comments

Comments
 (0)