Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MAGETWO-42061-caching-for-home-p…
Browse files Browse the repository at this point in the history
…age' into develop
  • Loading branch information
Joan He committed Sep 10, 2015
2 parents 294d44a + 4a770bb commit 39bbde0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/code/Magento/PageCache/etc/varnish3.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ sub vcl_fetch {
set beresp.do_gzip = true;
}

# cache only successfully responses
if (beresp.status != 200) {
# cache only successfully responses and 404s
if (beresp.status != 200 && beresp.status != 404) {
set beresp.ttl = 0s;
return (hit_for_pass);
} elsif (beresp.http.Cache-Control ~ "private") {
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/PageCache/etc/varnish4.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ sub vcl_backend_response {
set beresp.do_gzip = true;
}

# cache only successfully responses
if (beresp.status != 200) {
# cache only successfully responses and 404s
if (beresp.status != 200 && beresp.status != 404) {
set beresp.ttl = 0s;
set beresp.uncacheable = true;
return (deliver);
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/Magento/Framework/App/PageCache/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function process(\Magento\Framework\App\Response\Http $response)
if (preg_match('/public.*s-maxage=(\d+)/', $response->getHeader('Cache-Control')->getFieldValue(), $matches)) {
$maxAge = $matches[1];
$response->setNoCacheHeaders();
if ($response->getHttpResponseCode() == 200 && ($this->request->isGet() || $this->request->isHead())) {
if (($response->getHttpResponseCode() == 200 || $response->getHttpResponseCode() == 404)
&& ($this->request->isGet() || $this->request->isHead())
) {
$tagsHeader = $response->getHeader('X-Magento-Tags');
$tags = $tagsHeader ? explode(',', $tagsHeader->getFieldValue()) : [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ public function loadProvider()
];
}

public function testProcessSaveCache()
/**
* @param $httpCode
* @dataProvider testProcessSaveCacheDataProvider
*/
public function testProcessSaveCache($httpCode, $at)
{
$httpCode = 200;

$cacheControlHeader = \Zend\Http\Header\CacheControl::fromString(
'Cache-Control: public, max-age=100, s-maxage=100'
);
Expand All @@ -116,21 +118,37 @@ public function testProcessSaveCache()
$this->returnValue($cacheControlHeader)
);
$this->responseMock->expects(
$this->once()
$this->any()
)->method(
'getHttpResponseCode'
)->will(
$this->returnValue($httpCode)
);
$this->requestMock->expects($this->once())->method('isGet')->will($this->returnValue(true));
$this->responseMock->expects($this->once())->method('setNoCacheHeaders');
$this->responseMock->expects($this->at(3))->method('getHeader')->with('X-Magento-Tags');
$this->responseMock->expects($this->at(4))->method('clearHeader')->with($this->equalTo('Set-Cookie'));
$this->responseMock->expects($this->at(5))->method('clearHeader')->with($this->equalTo('X-Magento-Tags'));
$this->cacheMock->expects($this->once())->method('save');
)->willReturn($httpCode);
$this->requestMock->expects($this->once())
->method('isGet')
->willReturn(true);
$this->responseMock->expects($this->once())
->method('setNoCacheHeaders');
$this->responseMock->expects($this->at($at[0]))
->method('getHeader')
->with('X-Magento-Tags');
$this->responseMock->expects($this->at($at[1]))
->method('clearHeader')
->with($this->equalTo('Set-Cookie'));
$this->responseMock->expects($this->at($at[2]))
->method('clearHeader')
->with($this->equalTo('X-Magento-Tags'));
$this->cacheMock->expects($this->once())
->method('save');
$this->kernel->process($this->responseMock);
}

public function testProcessSaveCacheDataProvider()
{
return [
[200, [3, 4, 5]],
[404, [4, 5, 6]]
];
}

/**
* @dataProvider processNotSaveCacheProvider
* @param string $cacheControlHeader
Expand Down Expand Up @@ -167,13 +185,11 @@ public function processNotSaveCacheProvider()
return [
['private, max-age=100', 200, true, false],
['private, max-age=100', 200, false, false],
['private, max-age=100', 404, true, false],
['private, max-age=100', 500, true, false],
['no-store, no-cache, must-revalidate, max-age=0', 200, true, false],
['no-store, no-cache, must-revalidate, max-age=0', 200, false, false],
['no-store, no-cache, must-revalidate, max-age=0', 404, true, false],
['no-store, no-cache, must-revalidate, max-age=0', 500, true, false],
['public, max-age=100, s-maxage=100', 404, true, true],
['public, max-age=100, s-maxage=100', 500, true, true],
['public, max-age=100, s-maxage=100', 200, false, true]
];
Expand Down

0 comments on commit 39bbde0

Please sign in to comment.