-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from magento-ogre/MAGETWO-35131-move-cache-in…
…validate [Ogre] MAGETWO 35131 Move cache invalidate
- Loading branch information
Showing
8 changed files
with
340 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\CacheInvalidate\Model; | ||
|
||
/** | ||
* Class Observer | ||
*/ | ||
class Observer | ||
{ | ||
/** | ||
* Application config object | ||
* | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
protected $_config; | ||
|
||
/** | ||
* @var \Magento\PageCache\Helper\Data | ||
*/ | ||
protected $_helper; | ||
|
||
/** | ||
* @var \Magento\Framework\HTTP\Adapter\Curl | ||
*/ | ||
protected $_curlAdapter; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Magento\PageCache\Model\Config $config | ||
* @param \Magento\PageCache\Helper\Data $helper | ||
* @param \Magento\Framework\HTTP\Adapter\Curl $curlAdapter | ||
*/ | ||
public function __construct( | ||
\Magento\PageCache\Model\Config $config, | ||
\Magento\PageCache\Helper\Data $helper, | ||
\Magento\Framework\HTTP\Adapter\Curl $curlAdapter | ||
) { | ||
$this->_config = $config; | ||
$this->_helper = $helper; | ||
$this->_curlAdapter = $curlAdapter; | ||
} | ||
|
||
/** | ||
* If Varnish caching is enabled it collects array of tags | ||
* of incoming object and asks to clean cache. | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* @return void | ||
*/ | ||
public function invalidateVarnish(\Magento\Framework\Event\Observer $observer) | ||
{ | ||
if ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->_config->isEnabled()) { | ||
$object = $observer->getEvent()->getObject(); | ||
if ($object instanceof \Magento\Framework\Object\IdentityInterface) { | ||
$tags = []; | ||
$pattern = "((^|,)%s(,|$))"; | ||
foreach ($object->getIdentities() as $tag) { | ||
$tags[] = sprintf($pattern, preg_replace("~_\\d+$~", '', $tag)); | ||
$tags[] = sprintf($pattern, $tag); | ||
} | ||
$this->sendPurgeRequest(implode('|', array_unique($tags))); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Flash Varnish cache | ||
* | ||
* @param \Magento\Framework\Event\Observer $observer | ||
* @return void | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function flushAllCache(\Magento\Framework\Event\Observer $observer) | ||
{ | ||
if ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->_config->isEnabled()) { | ||
$this->sendPurgeRequest('.*'); | ||
} | ||
} | ||
|
||
/** | ||
* Send curl purge request | ||
* to invalidate cache by tags pattern | ||
* | ||
* @param string $tagsPattern | ||
* @return void | ||
*/ | ||
protected function sendPurgeRequest($tagsPattern) | ||
{ | ||
$headers = ["X-Magento-Tags-Pattern: {$tagsPattern}"]; | ||
$this->_curlAdapter->setOptions([CURLOPT_CUSTOMREQUEST => 'PURGE']); | ||
$this->_curlAdapter->write('', $this->_helper->getUrl('*'), '1.1', $headers); | ||
$this->_curlAdapter->read(); | ||
$this->_curlAdapter->close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
The CacheInvalidate module is used to invalidate the Varnish cache if it is configured. | ||
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. |
138 changes: 138 additions & 0 deletions
138
app/code/Magento/CacheInvalidate/Test/Unit/Model/ObserverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\CacheInvalidate\Test\Unit\Model; | ||
|
||
class ObserverTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\Observer */ | ||
protected $_model; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Event\Observer */ | ||
protected $_observerMock; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\HTTP\Adapter\Curl */ | ||
protected $_curlMock; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Config */ | ||
protected $_configMock; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Helper\Data */ | ||
protected $_helperMock; | ||
|
||
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Object\ */ | ||
protected $_observerObject; | ||
|
||
/** | ||
* Set up all mocks and data for test | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->_configMock = $this->getMock( | ||
'Magento\PageCache\Model\Config', | ||
['getType', 'isEnabled'], | ||
[], | ||
'', | ||
false | ||
); | ||
$this->_helperMock = $this->getMock('Magento\PageCache\Helper\Data', ['getUrl'], [], '', false); | ||
$this->_curlMock = $this->getMock( | ||
'\Magento\Framework\HTTP\Adapter\Curl', | ||
['setOptions', 'write', 'read', 'close'], | ||
[], | ||
'', | ||
false | ||
); | ||
$this->_model = new \Magento\CacheInvalidate\Model\Observer( | ||
$this->_configMock, | ||
$this->_helperMock, | ||
$this->_curlMock | ||
); | ||
$this->_observerMock = $this->getMock( | ||
'Magento\Framework\Event\Observer', | ||
['getEvent'], | ||
[], | ||
'', | ||
false | ||
); | ||
$this->_observerObject = $this->getMock('\Magento\Store\Model\Store', [], [], '', false); | ||
} | ||
|
||
/** | ||
* Test case for cache invalidation | ||
*/ | ||
public function testInvalidateVarnish() | ||
{ | ||
$tags = ['cache_1', 'cache_group']; | ||
$pattern = '((^|,)cache(,|$))|((^|,)cache_1(,|$))|((^|,)cache_group(,|$))'; | ||
|
||
$this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); | ||
$this->_configMock->expects( | ||
$this->once() | ||
)->method( | ||
'getType' | ||
)->will( | ||
$this->returnValue(\Magento\PageCache\Model\Config::VARNISH) | ||
); | ||
$eventMock = $this->getMock('Magento\Framework\Event', ['getObject'], [], '', false); | ||
$eventMock->expects($this->once())->method('getObject')->will($this->returnValue($this->_observerObject)); | ||
$this->_observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock)); | ||
$this->_observerObject->expects($this->once())->method('getIdentities')->will($this->returnValue($tags)); | ||
$this->sendPurgeRequest($pattern); | ||
|
||
$this->_model->invalidateVarnish($this->_observerMock); | ||
} | ||
|
||
/** | ||
* Test case for flushing all the cache | ||
*/ | ||
public function testFlushAllCache() | ||
{ | ||
$this->_configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); | ||
$this->_configMock->expects( | ||
$this->once() | ||
)->method( | ||
'getType' | ||
)->will( | ||
$this->returnValue(\Magento\PageCache\Model\Config::VARNISH) | ||
); | ||
|
||
$this->sendPurgeRequest('.*'); | ||
$this->_model->flushAllCache($this->_observerMock); | ||
} | ||
|
||
/** | ||
* @param string $tags | ||
*/ | ||
protected function sendPurgeRequest($tags) | ||
{ | ||
$url = 'http://mangento.index.php'; | ||
$httpVersion = '1.1'; | ||
$headers = ["X-Magento-Tags-Pattern: {$tags}"]; | ||
$this->_helperMock->expects( | ||
$this->any() | ||
)->method( | ||
'getUrl' | ||
)->with( | ||
$this->equalTo('*'), | ||
[] | ||
)->will( | ||
$this->returnValue($url) | ||
); | ||
$this->_curlMock->expects($this->once())->method('setOptions')->with([CURLOPT_CUSTOMREQUEST => 'PURGE']); | ||
$this->_curlMock->expects( | ||
$this->once() | ||
)->method( | ||
'write' | ||
)->with( | ||
$this->equalTo(''), | ||
$this->equalTo($url), | ||
$httpVersion, | ||
$this->equalTo($headers) | ||
); | ||
$this->_curlMock->expects($this->once())->method('read'); | ||
$this->_curlMock->expects($this->once())->method('close'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "magento/module-cache-invalidate", | ||
"description": "N/A", | ||
"require": { | ||
"php": "~5.5.0|~5.6.0", | ||
"magento/module-page-cache": "0.74.0-beta4", | ||
"magento/framework": "0.74.0-beta4", | ||
"magento/magento-composer-installer": "*" | ||
}, | ||
"type": "magento2-module", | ||
"version": "0.74.0-beta4", | ||
"license": [ | ||
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"extra": { | ||
"map": [ | ||
[ | ||
"*", | ||
"Magento/CacheInvalidate" | ||
] | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd"> | ||
<event name="clean_cache_by_tags"> | ||
<observer name="invalidate_varnish" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="adminhtml_cache_flush_system"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" /> | ||
</event> | ||
<event name="clean_media_cache_after"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" /> | ||
</event> | ||
<event name="clean_catalog_images_cache_after"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" /> | ||
</event> | ||
<event name="assigned_theme_changed"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="catalogrule_after_apply"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="adminhtml_cache_refresh_type"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" /> | ||
</event> | ||
<event name="adminhtml_cache_flush_all"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" /> | ||
</event> | ||
<event name="assign_theme_to_stores_after"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="flushAllCache" /> | ||
</event> | ||
<event name="controller_action_postdispatch_adminhtml_system_currency_saveRates"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="controller_action_postdispatch_adminhtml_system_config_save"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="controller_action_postdispatch_adminhtml_catalog_product_action_attribute_save"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="controller_action_postdispatch_adminhtml_catalog_product_massStatus"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
<event name="controller_action_postdispatch_adminhtml_system_currencysymbol_save"> | ||
<observer name="flush_varnish_pagecache" instance="Magento\CacheInvalidate\Model\Observer" method="invalidateVarnish" /> | ||
</event> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> | ||
<module name="Magento_CacheInvalidate" setup_version="2.0.0"> | ||
<sequence> | ||
<module name="Magento_Store"/> | ||
</sequence> | ||
</module> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.