Skip to content

Commit 703d8f2

Browse files
authored
Merge pull request #21052 from nextcloud/backport/21050/stable19
[stable19] Caching and compression for app store requests
2 parents 5763ce2 + 745667e commit 703d8f2

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
use OCP\ILogger;
4242

4343
abstract class Fetcher {
44-
public const INVALIDATE_AFTER_SECONDS = 300;
44+
public const INVALIDATE_AFTER_SECONDS = 3600;
4545

4646
/** @var IAppData */
4747
protected $appData;
@@ -98,12 +98,11 @@ protected function fetch($ETag, $content) {
9898

9999
$options = [
100100
'timeout' => 10,
101+
'headers' => ['Accept-Encoding' => 'gzip'],
101102
];
102103

103104
if ($ETag !== '') {
104-
$options['headers'] = [
105-
'If-None-Match' => $ETag,
106-
];
105+
$options['headers']['If-None-Match'] = $ETag;
107106
}
108107

109108
$client = $this->clientService->newClient();
@@ -153,7 +152,7 @@ public function get() {
153152
// No caching when the version has been updated
154153
if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
155154

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

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

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
239239
$this->timeFactory
240240
->expects($this->at(0))
241241
->method('getTime')
242-
->willReturn(1501);
242+
->willReturn(4801);
243243
$client = $this->createMock(IClient::class);
244244
$this->clientService
245245
->expects($this->once())
@@ -249,7 +249,15 @@ public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {
249249
$client
250250
->expects($this->once())
251251
->method('get')
252-
->with($this->endpoint)
252+
->with(
253+
$this->equalTo($this->endpoint),
254+
$this->equalTo([
255+
'timeout' => 10,
256+
'headers' => [
257+
'Accept-Encoding' => 'gzip',
258+
]
259+
])
260+
)
253261
->willReturn($response);
254262
$response
255263
->expects($this->once())
@@ -342,7 +350,15 @@ public function testGetWithAlreadyExistingFileAndNoVersion() {
342350
$client
343351
->expects($this->once())
344352
->method('get')
345-
->with($this->endpoint)
353+
->with(
354+
$this->equalTo($this->endpoint),
355+
$this->equalTo([
356+
'timeout' => 10,
357+
'headers' => [
358+
'Accept-Encoding' => 'gzip',
359+
]
360+
])
361+
)
346362
->willReturn($response);
347363
$response
348364
->expects($this->once())
@@ -430,7 +446,15 @@ public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
430446
$client
431447
->expects($this->once())
432448
->method('get')
433-
->with($this->endpoint)
449+
->with(
450+
$this->equalTo($this->endpoint),
451+
$this->equalTo([
452+
'timeout' => 10,
453+
'headers' => [
454+
'Accept-Encoding' => 'gzip',
455+
]
456+
])
457+
)
434458
->willReturn($response);
435459
$response
436460
->expects($this->once())
@@ -495,7 +519,15 @@ public function testGetWithExceptionInClient() {
495519
$client
496520
->expects($this->once())
497521
->method('get')
498-
->with($this->endpoint)
522+
->with(
523+
$this->equalTo($this->endpoint),
524+
$this->equalTo([
525+
'timeout' => 10,
526+
'headers' => [
527+
'Accept-Encoding' => 'gzip',
528+
]
529+
])
530+
)
499531
->willThrowException(new \Exception());
500532

501533
$this->assertSame([], $this->fetcher->get());
@@ -533,11 +565,11 @@ public function testGetMatchingETag() {
533565
$this->timeFactory
534566
->expects($this->at(0))
535567
->method('getTime')
536-
->willReturn(1501);
568+
->willReturn(4801);
537569
$this->timeFactory
538570
->expects($this->at(1))
539571
->method('getTime')
540-
->willReturn(1502);
572+
->willReturn(4802);
541573
$client = $this->createMock(IClient::class);
542574
$this->clientService
543575
->expects($this->once())
@@ -552,14 +584,15 @@ public function testGetMatchingETag() {
552584
$this->equalTo([
553585
'timeout' => 10,
554586
'headers' => [
555-
'If-None-Match' => '"myETag"'
587+
'Accept-Encoding' => 'gzip',
588+
'If-None-Match' => '"myETag"',
556589
]
557590
])
558591
)->willReturn($response);
559592
$response->method('getStatusCode')
560593
->willReturn(304);
561594

562-
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
595+
$newData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
563596
$file
564597
->expects($this->at(1))
565598
->method('putContent')
@@ -624,6 +657,7 @@ public function testGetNoMatchingETag() {
624657
$this->equalTo([
625658
'timeout' => 10,
626659
'headers' => [
660+
'Accept-Encoding' => 'gzip',
627661
'If-None-Match' => '"myETag"',
628662
]
629663
])
@@ -638,7 +672,7 @@ public function testGetNoMatchingETag() {
638672
$response->method('getHeader')
639673
->with($this->equalTo('ETag'))
640674
->willReturn('"newETag"');
641-
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
675+
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":4802,"ncversion":"11.0.0.2","ETag":"\"newETag\""}';
642676
$file
643677
->expects($this->at(1))
644678
->method('putContent')
@@ -650,11 +684,11 @@ public function testGetNoMatchingETag() {
650684
$this->timeFactory
651685
->expects($this->at(0))
652686
->method('getTime')
653-
->willReturn(1501);
687+
->willReturn(4801);
654688
$this->timeFactory
655689
->expects($this->at(1))
656690
->method('getTime')
657-
->willReturn(1502);
691+
->willReturn(4802);
658692

659693
$expected = [
660694
[
@@ -710,6 +744,9 @@ public function testFetchAfterUpgradeNoETag() {
710744
$this->equalTo($this->endpoint),
711745
$this->equalTo([
712746
'timeout' => 10,
747+
'headers' => [
748+
'Accept-Encoding' => 'gzip',
749+
],
713750
])
714751
)
715752
->willReturn($response);

0 commit comments

Comments
 (0)