Skip to content

Commit 0772e62

Browse files
author
Kopylova,Olga(okopylova)
committed
Merge pull request #237 from magento-ogre/MAGETWO-35131-move-cache-invalidate
[Ogre] MAGETWO 35131 Move cache invalidate
2 parents 009d4eb + d4d9868 commit 0772e62

File tree

8 files changed

+340
-11
lines changed

8 files changed

+340
-11
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CacheInvalidate\Model;
7+
8+
/**
9+
* Class Observer
10+
*/
11+
class Observer
12+
{
13+
/**
14+
* Application config object
15+
*
16+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
17+
*/
18+
protected $_config;
19+
20+
/**
21+
* @var \Magento\PageCache\Helper\Data
22+
*/
23+
protected $_helper;
24+
25+
/**
26+
* @var \Magento\Framework\HTTP\Adapter\Curl
27+
*/
28+
protected $_curlAdapter;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param \Magento\PageCache\Model\Config $config
34+
* @param \Magento\PageCache\Helper\Data $helper
35+
* @param \Magento\Framework\HTTP\Adapter\Curl $curlAdapter
36+
*/
37+
public function __construct(
38+
\Magento\PageCache\Model\Config $config,
39+
\Magento\PageCache\Helper\Data $helper,
40+
\Magento\Framework\HTTP\Adapter\Curl $curlAdapter
41+
) {
42+
$this->_config = $config;
43+
$this->_helper = $helper;
44+
$this->_curlAdapter = $curlAdapter;
45+
}
46+
47+
/**
48+
* If Varnish caching is enabled it collects array of tags
49+
* of incoming object and asks to clean cache.
50+
*
51+
* @param \Magento\Framework\Event\Observer $observer
52+
* @return void
53+
*/
54+
public function invalidateVarnish(\Magento\Framework\Event\Observer $observer)
55+
{
56+
if ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->_config->isEnabled()) {
57+
$object = $observer->getEvent()->getObject();
58+
if ($object instanceof \Magento\Framework\Object\IdentityInterface) {
59+
$tags = [];
60+
$pattern = "((^|,)%s(,|$))";
61+
foreach ($object->getIdentities() as $tag) {
62+
$tags[] = sprintf($pattern, preg_replace("~_\\d+$~", '', $tag));
63+
$tags[] = sprintf($pattern, $tag);
64+
}
65+
$this->sendPurgeRequest(implode('|', array_unique($tags)));
66+
}
67+
}
68+
}
69+
70+
/**
71+
* Flash Varnish cache
72+
*
73+
* @param \Magento\Framework\Event\Observer $observer
74+
* @return void
75+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
76+
*/
77+
public function flushAllCache(\Magento\Framework\Event\Observer $observer)
78+
{
79+
if ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->_config->isEnabled()) {
80+
$this->sendPurgeRequest('.*');
81+
}
82+
}
83+
84+
/**
85+
* Send curl purge request
86+
* to invalidate cache by tags pattern
87+
*
88+
* @param string $tagsPattern
89+
* @return void
90+
*/
91+
protected function sendPurgeRequest($tagsPattern)
92+
{
93+
$headers = ["X-Magento-Tags-Pattern: {$tagsPattern}"];
94+
$this->_curlAdapter->setOptions([CURLOPT_CUSTOMREQUEST => 'PURGE']);
95+
$this->_curlAdapter->write('', $this->_helper->getUrl('*'), '1.1', $headers);
96+
$this->_curlAdapter->read();
97+
$this->_curlAdapter->close();
98+
}
99+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The CacheInvalidate module is used to invalidate the Varnish cache if it is configured.
2+
It listens for events that request the cache to be flushed or cause the cache to be invalid, then sends Varnish a purge request using cURL.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CacheInvalidate\Test\Unit\Model;
7+
8+
class ObserverTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\Observer */
11+
protected $_model;
12+
13+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Event\Observer */
14+
protected $_observerMock;
15+
16+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\HTTP\Adapter\Curl */
17+
protected $_curlMock;
18+
19+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Config */
20+
protected $_configMock;
21+
22+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Helper\Data */
23+
protected $_helperMock;
24+
25+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Object\ */
26+
protected $_observerObject;
27+
28+
/**
29+
* Set up all mocks and data for test
30+
*/
31+
public function setUp()
32+
{
33+
$this->_configMock = $this->getMock(
34+
'Magento\PageCache\Model\Config',
35+
['getType', 'isEnabled'],
36+
[],
37+
'',
38+
false
39+
);
40+
$this->_helperMock = $this->getMock('Magento\PageCache\Helper\Data', ['getUrl'], [], '', false);
41+
$this->_curlMock = $this->getMock(
42+
'\Magento\Framework\HTTP\Adapter\Curl',
43+
['setOptions', 'write', 'read', 'close'],
44+
[],
45+
'',
46+
false
47+
);
48+
$this->_model = new \Magento\CacheInvalidate\Model\Observer(
49+
$this->_configMock,
50+
$this->_helperMock,
51+
$this->_curlMock
52+
);
53+
$this->_observerMock = $this->getMock(
54+
'Magento\Framework\Event\Observer',
55+
['getEvent'],
56+
[],
57+
'',
58+
false
59+
);
60+
$this->_observerObject = $this->getMock('\Magento\Store\Model\Store', [], [], '', false);
61+
}
62+
63+
/**
64+
* Test case for cache invalidation
65+
*/
66+
public function testInvalidateVarnish()
67+
{
68+
$tags = ['cache_1', 'cache_group'];
69+
$pattern = '((^|,)cache(,|$))|((^|,)cache_1(,|$))|((^|,)cache_group(,|$))';
70+
71+
$this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
72+
$this->_configMock->expects(
73+
$this->once()
74+
)->method(
75+
'getType'
76+
)->will(
77+
$this->returnValue(\Magento\PageCache\Model\Config::VARNISH)
78+
);
79+
$eventMock = $this->getMock('Magento\Framework\Event', ['getObject'], [], '', false);
80+
$eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->_observerObject));
81+
$this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock));
82+
$this->_observerObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags));
83+
$this->sendPurgeRequest($pattern);
84+
85+
$this->_model->invalidateVarnish($this->_observerMock);
86+
}
87+
88+
/**
89+
* Test case for flushing all the cache
90+
*/
91+
public function testFlushAllCache()
92+
{
93+
$this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
94+
$this->_configMock->expects(
95+
$this->once()
96+
)->method(
97+
'getType'
98+
)->will(
99+
$this->returnValue(\Magento\PageCache\Model\Config::VARNISH)
100+
);
101+
102+
$this->sendPurgeRequest('.*');
103+
$this->_model->flushAllCache($this->_observerMock);
104+
}
105+
106+
/**
107+
* @param string $tags
108+
*/
109+
protected function sendPurgeRequest($tags)
110+
{
111+
$url = 'http://mangento.index.php';
112+
$httpVersion = '1.1';
113+
$headers = ["X-Magento-Tags-Pattern: {$tags}"];
114+
$this->_helperMock->expects(
115+
$this->any()
116+
)->method(
117+
'getUrl'
118+
)->with(
119+
$this->equalTo('*'),
120+
[]
121+
)->will(
122+
$this->returnValue($url)
123+
);
124+
$this->_curlMock->expects($this->once())->method('setOptions')->with([CURLOPT_CUSTOMREQUEST => 'PURGE']);
125+
$this->_curlMock->expects(
126+
$this->once()
127+
)->method(
128+
'write'
129+
)->with(
130+
$this->equalTo(''),
131+
$this->equalTo($url),
132+
$httpVersion,
133+
$this->equalTo($headers)
134+
);
135+
$this->_curlMock->expects($this->once())->method('read');
136+
$this->_curlMock->expects($this->once())->method('close');
137+
}
138+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "magento/module-cache-invalidate",
3+
"description": "N/A",
4+
"require": {
5+
"php": "~5.5.0|~5.6.0",
6+
"magento/module-page-cache": "0.74.0-beta4",
7+
"magento/framework": "0.74.0-beta4",
8+
"magento/magento-composer-installer": "*"
9+
},
10+
"type": "magento2-module",
11+
"version": "0.74.0-beta4",
12+
"license": [
13+
"OSL-3.0",
14+
"AFL-3.0"
15+
],
16+
"extra": {
17+
"map": [
18+
[
19+
"*",
20+
"Magento/CacheInvalidate"
21+
]
22+
]
23+
}
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
9+
<event name="clean_cache_by_tags">
10+
<observer name="invalidate_varnish" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
11+
</event>
12+
<event name="adminhtml_cache_flush_system">
13+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" />
14+
</event>
15+
<event name="clean_media_cache_after">
16+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" />
17+
</event>
18+
<event name="clean_catalog_images_cache_after">
19+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" />
20+
</event>
21+
<event name="assigned_theme_changed">
22+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
23+
</event>
24+
<event name="catalogrule_after_apply">
25+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
26+
</event>
27+
<event name="adminhtml_cache_refresh_type">
28+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" />
29+
</event>
30+
<event name="adminhtml_cache_flush_all">
31+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" />
32+
</event>
33+
<event name="assign_theme_to_stores_after">
34+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" />
35+
</event>
36+
<event name="controller_action_postdispatch_adminhtml_system_currency_saveRates">
37+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
38+
</event>
39+
<event name="controller_action_postdispatch_adminhtml_system_config_save">
40+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
41+
</event>
42+
<event name="controller_action_postdispatch_adminhtml_catalog_product_action_attribute_save">
43+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
44+
</event>
45+
<event name="controller_action_postdispatch_adminhtml_catalog_product_massStatus">
46+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
47+
</event>
48+
<event name="controller_action_postdispatch_adminhtml_system_currencysymbol_save">
49+
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" />
50+
</event>
51+
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
9+
<module name="Magento_CacheInvalidate" setup_version="2.0.0">
10+
<sequence>
11+
<module name="Magento_Store"/>
12+
</sequence>
13+
</module>
14+
</config>

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"magento/module-backend": "self.version",
6464
"magento/module-backup": "self.version",
6565
"magento/module-bundle": "self.version",
66+
"magento/module-cache-invalidate": "self.version",
6667
"magento/module-captcha": "self.version",
6768
"magento/module-catalog": "self.version",
6869
"magento/module-catalog-import-export": "self.version",

0 commit comments

Comments
 (0)