Skip to content

Shorter cache tags #23

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

Closed
wants to merge 2 commits into from
Closed
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
25 changes: 25 additions & 0 deletions Helper/CacheTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Fastly\Cdn\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;

class CacheTags extends AbstractHelper
{
/**
* Replaces long Magento Cache tags with a shorter version
*
* @param string
* @return string
*/
public function convertCacheTags($tags)
{
$fastlyTags = array(
'catalog_product' => 'p',
'catalog_category' => 'c',
'cms_page' => 'cpg'
);

return str_replace(array_keys($fastlyTags), $fastlyTags, $tags);
}
}
12 changes: 10 additions & 2 deletions Model/Layout/LayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,26 @@ class LayoutPlugin
*/
protected $response;

/**
* @var \Fastly\Cdn\Helper\CacheTags
*/
protected $cacheTags;

/**
* Constructor
*
* @param \Magento\Framework\App\ResponseInterface $response
* @param \Fastly\Cdn\Model\Config $config
* @param \Fastly\Cdn\Helper\CacheTags $cacheTags
*/
public function __construct(
\Magento\Framework\App\ResponseInterface $response,
Config $config
Config $config,
\Fastly\Cdn\Helper\CacheTags $cacheTags
) {
$this->response = $response;
$this->config = $config;
$this->cacheTags = $cacheTags;
}

/**
Expand Down Expand Up @@ -94,7 +102,7 @@ public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result)
// Fastly expects surrogate keys separated by space. replace existing header.
$header = $this->response->getHeader('X-Magento-Tags');
if ($header instanceof \Zend\Http\Header\HeaderInterface) {
$this->response->setHeader($header->getFieldName(), str_replace(',', ' ', $header->getFieldValue()), true);
$this->response->setHeader($header->getFieldName(), $this->cacheTags->convertCacheTags(str_replace(',', ' ', $header->getFieldValue())), true);
}
}
return $result;
Expand Down
11 changes: 10 additions & 1 deletion Observer/InvalidateVarnishObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
namespace Fastly\Cdn\Observer;

use Fastly\Cdn\Helper\CacheTags;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\PurgeCache;
use Magento\Framework\Event\ObserverInterface;
Expand All @@ -38,14 +39,21 @@ class InvalidateVarnishObserver implements ObserverInterface
*/
protected $purgeCache;

/**
* @var CacheTags
*/
protected $cacheTags;

/**
* @param Config $config
* @param PurgeCache $purgeCache
* @param CacheTags $cacheTags
*/
public function __construct(Config $config, PurgeCache $purgeCache)
public function __construct(Config $config, PurgeCache $purgeCache, CacheTags $cacheTags)
{
$this->config = $config;
$this->purgeCache = $purgeCache;
$this->cacheTags = $cacheTags;
}

/**
Expand All @@ -60,6 +68,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$object = $observer->getEvent()->getObject();
if ($object instanceof \Magento\Framework\DataObject\IdentityInterface && $this->canPurgeObject($object)) {
foreach ($object->getIdentities() as $tag) {
$tag = $this->cacheTags->convertCacheTags($tag);
$this->purgeCache->sendPurgeRequest($tag);
}
}
Expand Down
13 changes: 0 additions & 13 deletions etc/fastly.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,6 @@ sub vcl_recv {
}

sub vcl_fetch {

# Remove Set-Cookies from responses for static content
# to match the cookie removal in recv.
if (req.url ~ "^/(pub/)?(media|static)/") {
unset beresp.http.set-cookie;

# Set a short TTL for 404's
if (beresp.status == 404) {
set beresp.ttl = 300s;
}

}

#FASTLY fetch

if (beresp.status >= 500) {
Expand Down