From 3986564e878f25d31b062228af385e9879236a0a Mon Sep 17 00:00:00 2001 From: Glenn Slaven Date: Fri, 27 Apr 2018 15:46:32 +1000 Subject: [PATCH 1/4] Allow CMS pages to not be banned in Varnish when products are updated Adds a new option to the Full Page Cache configuration section in Stores configuration "Include CMS Pages in product purges". If set to No, CMS pages will not be purged when a product is updated, even if that product is displayed on the page. CMS pages will still be purged if their content is updated. --- Model/Config.php | 24 ++++++++++++++++++++++++ Model/PurgeCache.php | 22 ++++++++++++++++++++-- Observer/InvalidateVarnishObserver.php | 6 +++--- etc/adminhtml/system.xml | 23 +++++++++++++++++++++++ etc/config.xml | 11 +++++++++++ 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 Model/Config.php create mode 100644 etc/adminhtml/system.xml create mode 100644 etc/config.xml diff --git a/Model/Config.php b/Model/Config.php new file mode 100644 index 0000000..90c2ffa --- /dev/null +++ b/Model/Config.php @@ -0,0 +1,24 @@ +_scopeConfig->isSetFlag('system/full_page_cache/sectionio/include_cms_in_product_purge'); + } + } diff --git a/Model/PurgeCache.php b/Model/PurgeCache.php index 1f2dd8f..335da93 100644 --- a/Model/PurgeCache.php +++ b/Model/PurgeCache.php @@ -14,6 +14,13 @@ class PurgeCache */ private $logger; + /** + * Application config object + * + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + private $config; + /** @var \Sectionio\Metrics\Helper\State $helper */ protected $state; @@ -27,15 +34,18 @@ class PurgeCache * Constructor * * @param InvalidateLogger $logger + * @param \Sectionio\Metrics\Model\Config $config * @param \Sectionio\Metrics\Helper\State $state * @param \Sectionio\Metrics\Helper\Aperture $aperture */ public function __construct( InvalidateLogger $logger, + \Sectionio\Metrics\Model\Config $config, \Sectionio\Metrics\Helper\State $state, \Sectionio\Metrics\Helper\Aperture $aperture ) { $this->logger = $logger; + $this->config = $config; $this->state = $state; $this->aperture = $aperture; } @@ -54,13 +64,20 @@ public function sendPurgeRequest($tagsPattern) $environment_name = $this->state->getEnvironmentName(); $proxy_name = $this->state->getProxyName(); + $banExpression = urlencode('obj.http.X-Magento-Tags ~ ' . $tagsPattern); + + // If this ban contains product tags & includeCmsInProductPurge is false, exclude any object that also has a CMS Page tag + if (!$this->config->includeCmsInProductPurge() && strpos($tagsPattern, \Magento\Catalog\Model\Product::CACHE_TAG) !== false) { + $banExpression .= urlencode(' && obj.http.X-Magento-Tags !~ (^|,)'.\Magento\Cms\Model\Page::CACHE_TAG.'_[0-9]+'); + } + $uri = $this->aperture->generateUrl([ 'api' => true, 'accountId' => $account_id, 'applicationId' => $application_id, 'environmentName' => $environment_name, 'proxyName' => $proxy_name, - 'uriStem' => '/state?async=true&banExpression=' . urlencode('obj.http.X-Magento-Tags ~ ' . $tagsPattern) + 'uriStem' => '/state?async=true&banExpression=' . $banExpression ]); $info = $this->aperture->executeAuthRequest($uri, 'POST', [], self::BAN_TIMEOUT_SECONDS); @@ -68,7 +85,8 @@ public function sendPurgeRequest($tagsPattern) $this->logger->execute('Error executing purge: ' . $tagsPattern.', Error: ' . $info['body_content']); return false; } - $this->logger->execute(compact('server', 'tagsPattern')); + //$this->logger->execute(compact('server', 'tagsPattern')); return true; } } + diff --git a/Observer/InvalidateVarnishObserver.php b/Observer/InvalidateVarnishObserver.php index 6be060d..2857372 100644 --- a/Observer/InvalidateVarnishObserver.php +++ b/Observer/InvalidateVarnishObserver.php @@ -23,11 +23,11 @@ class InvalidateVarnishObserver implements ObserverInterface private $purgeCache; /** - * @param \Magento\PageCache\Model\Config $config + * @param \Sectionio\Metrics\Model\Config $config * @param \Sectionio\Metrics\Model\PurgeCache $purgeCache */ public function __construct( - \Magento\PageCache\Model\Config $config, + \Sectionio\Metrics\Model\Config $config, \Sectionio\Metrics\Model\PurgeCache $purgeCache ) { $this->config = $config; @@ -43,7 +43,7 @@ public function __construct( */ public function execute(\Magento\Framework\Event\Observer $observer) { - if ($this->config->getType() == \Magento\PageCache\Model\Config::VARNISH && $this->config->isEnabled()) { + if ($this->config->getType() == \Sectionio\Metrics\Model\Config::VARNISH && $this->config->isEnabled()) { $object = $observer->getEvent()->getObject(); if ($object instanceof \Magento\Framework\DataObject\IdentityInterface) { $tags = []; diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml new file mode 100644 index 0000000..59cfec5 --- /dev/null +++ b/etc/adminhtml/system.xml @@ -0,0 +1,23 @@ + + + + +
+ + + + + + If set to No, CMS pages will not be purged when a product is updated, even if that product is displayed on the page. CMS pages will still be purged if their content is updated. + Magento\Config\Model\Config\Source\Yesno + + + +
+
+
diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 0000000..cd6c938 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,11 @@ + + + + + + 1 + + + + + From aa960075c5506c083d4ed77f19f2004806538d0d Mon Sep 17 00:00:00 2001 From: Glenn Slaven Date: Fri, 27 Apr 2018 15:53:23 +1000 Subject: [PATCH 2/4] Update copyrite date to 2018 --- Block/Adminhtml/Report/Edit.php | 6 +++--- Block/Adminhtml/Report/Edit/Form.php | 8 ++++---- Block/Adminhtml/Report/Edit/Tab/Credentials.php | 2 +- Block/Adminhtml/Report/Edit/Tab/Metrics.php | 2 +- Block/Adminhtml/Report/Edit/Tab/Settings.php | 2 +- Block/Adminhtml/Report/Edit/Tabs.php | 2 +- Controller/Adminhtml/Report/FetchInfo.php | 2 +- Controller/Adminhtml/Report/Index.php | 2 +- Controller/Adminhtml/Report/Save.php | 2 +- Controller/Index/Index.php | 2 +- Controller/Router.php | 2 +- Helper/Aperture.php | 2 +- Helper/Data.php | 2 +- Helper/State.php | 2 +- LICENSE | 4 ++-- Model/Account.php | 4 ++-- Model/Application.php | 4 ++-- Model/Config.php | 2 +- Model/ResourceModel/Account.php | 4 ++-- Model/ResourceModel/Account/Collection.php | 2 +- Model/ResourceModel/Application.php | 4 ++-- Model/ResourceModel/Application/Collection.php | 2 +- Model/ResourceModel/Settings.php | 2 +- Model/ResourceModel/Settings/Collection.php | 2 +- Model/Settings.php | 4 ++-- Setup/InstallSchema.php | 2 +- etc/adminhtml/menu.xml | 4 ++-- etc/adminhtml/routes.xml | 4 ++-- etc/adminhtml/system.xml | 2 +- etc/di.xml | 2 +- etc/events.xml | 2 +- etc/frontend/di.xml | 2 +- etc/frontend/routes.xml | 2 +- etc/module.xml | 2 +- view/adminhtml/layout/metrics_report_index.xml | 2 +- 35 files changed, 48 insertions(+), 48 deletions(-) diff --git a/Block/Adminhtml/Report/Edit.php b/Block/Adminhtml/Report/Edit.php index 6cf8fd3..6b40769 100644 --- a/Block/Adminhtml/Report/Edit.php +++ b/Block/Adminhtml/Report/Edit.php @@ -1,6 +1,6 @@ _objectId = 'general_id'; @@ -28,7 +28,7 @@ protected function _construct() parent::_construct(); } - + /** * Retrieve text for header element * diff --git a/Block/Adminhtml/Report/Edit/Form.php b/Block/Adminhtml/Report/Edit/Form.php index fe5f60b..c96d8e7 100644 --- a/Block/Adminhtml/Report/Edit/Form.php +++ b/Block/Adminhtml/Report/Edit/Form.php @@ -1,12 +1,12 @@ setUseContainer(true); $this->setForm($form); - + return parent::_prepareForm(); } } diff --git a/Block/Adminhtml/Report/Edit/Tab/Credentials.php b/Block/Adminhtml/Report/Edit/Tab/Credentials.php index 714a580..0e85c76 100644 --- a/Block/Adminhtml/Report/Edit/Tab/Credentials.php +++ b/Block/Adminhtml/Report/Edit/Tab/Credentials.php @@ -1,6 +1,6 @@ transactionManager = $context->getTransactionManager(); $this->_resources = $context->getResources(); $this->objectRelationProcessor = $context->getObjectRelationProcessor(); diff --git a/Model/ResourceModel/Account/Collection.php b/Model/ResourceModel/Account/Collection.php index 94c7507..e9b2965 100644 --- a/Model/ResourceModel/Account/Collection.php +++ b/Model/ResourceModel/Account/Collection.php @@ -1,6 +1,6 @@ transactionManager = $context->getTransactionManager(); $this->_resources = $context->getResources(); $this->objectRelationProcessor = $context->getObjectRelationProcessor(); diff --git a/Model/ResourceModel/Application/Collection.php b/Model/ResourceModel/Application/Collection.php index ab7488c..fef56f9 100644 --- a/Model/ResourceModel/Application/Collection.php +++ b/Model/ResourceModel/Application/Collection.php @@ -1,6 +1,6 @@ - diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml index fdb3e87..29882b8 100644 --- a/etc/adminhtml/routes.xml +++ b/etc/adminhtml/routes.xml @@ -1,11 +1,11 @@ - diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 59cfec5..88737ad 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -1,7 +1,7 @@ diff --git a/etc/di.xml b/etc/di.xml index a96f27f..f6e7364 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1,7 +1,7 @@ diff --git a/etc/events.xml b/etc/events.xml index 906aac3..dfe413d 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -1,7 +1,7 @@ diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 7d1e1c9..f0cf631 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -1,7 +1,7 @@ diff --git a/etc/frontend/routes.xml b/etc/frontend/routes.xml index bb402d7..e22d442 100644 --- a/etc/frontend/routes.xml +++ b/etc/frontend/routes.xml @@ -1,7 +1,7 @@ diff --git a/etc/module.xml b/etc/module.xml index c2db4cb..1aa5fd5 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ diff --git a/view/adminhtml/layout/metrics_report_index.xml b/view/adminhtml/layout/metrics_report_index.xml index 7113e7f..4308ec0 100644 --- a/view/adminhtml/layout/metrics_report_index.xml +++ b/view/adminhtml/layout/metrics_report_index.xml @@ -1,7 +1,7 @@ From a7a065642cb905c079759aaac2abfa22409d809f Mon Sep 17 00:00:00 2001 From: Glenn Slaven Date: Fri, 27 Apr 2018 16:05:41 +1000 Subject: [PATCH 3/4] Remove dead code --- Model/PurgeCache.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Model/PurgeCache.php b/Model/PurgeCache.php index 335da93..58c9154 100644 --- a/Model/PurgeCache.php +++ b/Model/PurgeCache.php @@ -85,7 +85,6 @@ public function sendPurgeRequest($tagsPattern) $this->logger->execute('Error executing purge: ' . $tagsPattern.', Error: ' . $info['body_content']); return false; } - //$this->logger->execute(compact('server', 'tagsPattern')); return true; } } From 9f6641b1c21159b79d18eeac3acf0e9c5768ea77 Mon Sep 17 00:00:00 2001 From: Aaron Schreck Date: Tue, 26 Jun 2018 11:48:36 -0600 Subject: [PATCH 4/4] Allow for php 7.1.* --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 757540d..e05c807 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "AFL-3.0" ], "require": { - "php": "~5.5.0|~5.6.0|~7.0.0", + "php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0", "magento/framework": "~100.0" }, "autoload": {