Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CMS pages to not be banned in Varnish when products are updated #23

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © 2016 Sectionio. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sectionio\Metrics\Model;

/**
* Class Config
*
*/
class Config extends \Magento\PageCache\Model\Config
{

/**
* Returns include CMS in product purge.
*
* @return bool
*/
public function includeCmsInProductPurge()
{
return $this->_scopeConfig->isSetFlag('system/full_page_cache/sectionio/include_cms_in_product_purge');
}
}
22 changes: 20 additions & 2 deletions Model/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -54,21 +64,29 @@ 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);
if ($info['http_code'] != 200) {
$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'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove dead code.

return true;
}
}

6 changes: 3 additions & 3 deletions Observer/InvalidateVarnishObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [];
Expand Down
23 changes: 23 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Section.io. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="system">
<group id="full_page_cache">
<group id="sectionio" translate="label" showInDefault="1" showInWebsite="0" showInStore="0" sortOrder="700">
<label>section.io Settings</label>
<field id="include_cms_in_product_purge" translate="label comment" type="select" sortOrder="5" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Include CMS Pages in product purges</label>
<comment>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.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</group>
</section>
</system>
</config>
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<system>
<full_page_cache>
<sectionio>
<include_cms_in_product_purge>1</include_cms_in_product_purge>
</sectionio>
</full_page_cache>
</system>
</default>
</config>