1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\PageCache\Model\Layout;
/**
* Class LayoutPlugin
*/
class LayoutPlugin
{
/**
* @var \Magento\PageCache\Model\Config
*/
protected $config;
/**
* @var \Magento\Framework\App\ResponseInterface
*/
protected $response;
/**
* Constructor
*
* @param \Magento\Framework\App\ResponseInterface $response
* @param \Magento\PageCache\Model\Config $config
*/
public function __construct(
\Magento\Framework\App\ResponseInterface $response,
\Magento\PageCache\Model\Config $config
) {
$this->response = $response;
$this->config = $config;
}
/**
* Set appropriate Cache-Control headers
* We have to set public headers in order to tell Varnish and Builtin app that page should be cached
*
* @param \Magento\Framework\View\Layout $subject
* @param mixed $result
* @return mixed
*/
public function afterGenerateXml(\Magento\Framework\View\Layout $subject, $result)
{
if ($subject->isCacheable() && $this->config->isEnabled()) {
$this->response->setPublicHeaders($this->config->getTtl());
}
return $result;
}
/**
* Retrieve all identities from blocks for further cache invalidation
*
* @param \Magento\Framework\View\Layout $subject
* @param mixed $result
* @return mixed
*/
public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result)
{
if ($subject->isCacheable() && $this->config->isEnabled()) {
$tags = [];
foreach ($subject->getAllBlocks() as $block) {
if ($block instanceof \Magento\Framework\DataObject\IdentityInterface) {
$isEsiBlock = $block->getTtl() > 0;
$isVarnish = $this->config->getType() == \Magento\PageCache\Model\Config::VARNISH;
if ($isVarnish && $isEsiBlock) {
continue;
}
$tags = array_merge($tags, $block->getIdentities());
}
}
$tags = array_unique($tags);
$this->response->setHeader('X-Magento-Tags', implode(',', $tags));
}
return $result;
}
}