Skip to content

Introduction of Softpurge/xKey functionalities within module settings #69

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 66 additions & 1 deletion Helper/VclGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ class VclGenerator extends AbstractHelper {
private $magentoEdition;
private $magentoVersion;
private $moduleVersion;
protected $scopeConfig;

function __construct (
Data $data,
LazyVclParser $parser,
ProductMetadataInterface $metadata
ProductMetadataInterface $metadata,
ScopeConfigInterface $scopeConfig
) {
$this->data = $data;
$this->parser = $parser;
$this->metadata = $metadata;
$this->magentoVersion = $this->metadata->getVersion ();
$this->magentoEdition = $this->metadata->getEdition ();
$this->moduleVersion = $this->data->getModuleVersion ();
$this->scopeConfig = $scopeConfig;
}

public function insertSubHooksInclude ( $root ) {
Expand Down Expand Up @@ -216,6 +219,67 @@ public function versionEndpoint ( $node ) {
return $node;
}

public function insertXkey($node) {
$isXkeyEnabled = $this->scopeConfig->getValue(
'system/full_page_cache/varnish/enable_xkey',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
$isSoftPurgingUsed = $this->scopeConfig->getValue(
'system/full_page_cache/varnish/use_soft_purging',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if (!$isXkeyEnabled || !$isSoftPurgingUsed) {
return $node;
}

if ($node["type"] == "import" && strpos($node["raw"], "import std;") !== false) {
$importXkey = "import xkey;\n";
if (strpos($node["value"], $importXkey) === false) {
$node["value"] .= $importXkey;
$node["raw"] .= $importXkey;
}
return $node;
}

if ($node["type"] == "sub" && $node["identifier"] == "vcl_recv") {
$value = implode("\n", [
" # Full Page Cache flush",
" if (req.http.X-Magento-Tags-Pattern == \".*\") {",
" ban(\"obj.http.X-Magento-Tags ~ \" + req.http.X-Magento-Tags-Pattern);",
" } elseif (req.http.X-Magento-Tags-Pattern) {",
" set req.http.X-Magento-Tags-Pattern = regsuball(req.http.X-Magento-Tags-Pattern, \"[^a-zA-Z0-9_-]+\", \" \");",
" set req.http.X-Magento-Tags-Pattern = regsuball(req.http.X-Magento-Tags-Pattern, \"(^\\s*)|(\\s*$)\", \"\");",
" set req.http.n-gone = xkey.softpurge(req.http.X-Magento-Tags-Pattern);",
" return (synth(200, \"Invalidated \" + req.http.n-gone + \" objects\"));",
" }",
]);

$value = "\n$value\n" . $node["value"];
$raw = "sub " . $node["identifier"] . " {\n" . $value . "\n}";
$node["value"] = $value;
$node["raw"] = $raw;
}

if ($node["type"] == "sub" && $node["identifier"] == "vcl_backend_response") {
$value = implode("\n", [
" set beresp.grace = 3h;",
" if (beresp.http.X-Magento-Tags) {",
" set beresp.http.Grace = beresp.grace;",
" set beresp.http.xkey = regsuball(beresp.http.X-Magento-Tags, \",\", \" \");",
" set beresp.http.X-Magento-Tags = \"fpc\";",
" }",
]);

$value = "\n$value\n" . $node["value"];
$raw = "sub " . $node["identifier"] . " {\n" . $value . "\n}";
$node["value"] = $value;
$node["raw"] = $raw;
}

return $node;
}

function generateDefault ( $vcl ) {
$this->parser->setData ( $vcl );
$root = $this->parser->getAST ();
Expand All @@ -230,6 +294,7 @@ function generateDefault ( $vcl ) {
$root = $this->parser->visit ( $root, [ $this, "insertDeliver" ] );
$root = $this->parser->visit ( $root, [ $this, "insertSubHooks" ] );
$root = $this->insertSubHooksInclude ( $root );
$root = $this->parser->visit($root, [$this, "insertXkey"]);
return $this->parser->getString ( $root );
}

Expand Down
36 changes: 36 additions & 0 deletions conf/varnish/default.vcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
vcl 4.1;

{{if enable_xkey}}
import xkey;
{{/if}}

backend default {
.host = "magento";
.port = "8080";
}

sub vcl_recv {
{{if enable_xkey}}
# Full Page Cache flush
if (req.http.X-Magento-Tags-Pattern == ".*") {
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
} elseif (req.http.X-Magento-Tags-Pattern) {
# replace "((^|,)cat_c(,|$))|((^|,)cat_p(,|$))" to be "cat_c cat_p"
set req.http.X-Magento-Tags-Pattern = regsuball(req.http.X-Magento-Tags-Pattern, "[^a-zA-Z0-9_-]+" ," ");
# trim spaces
set req.http.X-Magento-Tags-Pattern = regsuball(req.http.X-Magento-Tags-Pattern, "(^\s*)|(\s*$)" ,"");
set req.http.n-gone = xkey.softpurge(req.http.X-Magento-Tags-Pattern);
return (synth(200, "Invalidated " + req.http.n-gone + " objects"));
}
{{/if}}
}

sub vcl_backend_response {
# Period to allow stale cache to be served
set beresp.grace = 3h;

{{if enable_xkey}}
# using xkey
if (beresp.http.X-Magento-Tags) {
set beresp.http.Grace = beresp.grace;
# set space separated xkey
set beresp.http.xkey = regsuball(beresp.http.X-Magento-Tags, ",", " ");
# reset beresp.http.X-Magento-Tags with some common general value
set beresp.http.X-Magento-Tags = "fpc";
}
{{/if}}
}
16 changes: 16 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
<field id="caching_application" >1</field>
</depends>
</field>
<field id="enable_xkey" translate="label" type="select" sortOrder="65" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use Xkey vmod</label>
<comment><![CDATA[Varnish Xkey vmod is installed. See <a target="_blank" href="https://github.com/varnish/varnish-modules/blob/master/src/vmod_xkey.vcc">Varnish VMOD xkey</a>.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="caching_application">2</field>
</depends>
</field>
<field id="use_soft_purging" type="select" translate="label comment" sortOrder="36" showInDefault="1">
<label>Use soft purging</label>
<comment>Use soft purging instead of hard purging. This requires Xkey vmod to be installed</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="system/full_page_cache/varnish/enable_xkey">1</field>
</depends>
</field>
<depends>
<field id="caching_application" >2</field>
</depends>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<excluded_wildcard_patterns></excluded_wildcard_patterns>
<excluded_regexp_patterns></excluded_regexp_patterns>
</cache_exclusion_patterns>
<enable_xkey>0</enable_xkey>
</jetrails_varnish>
</default>
</config>