Skip to content

Commit

Permalink
Separate purge logic to allow command line purge
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Song committed Jan 3, 2017
1 parent c18d2cf commit ee1f0de
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 83 deletions.
102 changes: 102 additions & 0 deletions Controller/Shell/Purge.php
@@ -0,0 +1,102 @@
<?php
/**
* LiteMage
*
* NOTICE OF LICENSE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://opensource.org/licenses/GPL-3.0 .
*
* @package LiteSpeed_LiteMage
* @copyright Copyright (c) 2016 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

namespace Litespeed\Litemage\Controller\Shell;

class Purge extends \Magento\Framework\App\Action\Action
{

/**
* @var \Litespeed\Litemage\Model\CacheControl
*/
protected $litemageCache;
protected $httpHeader;


/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Translate\InlineInterface $translateInline
* @param \Litespeed\Litemage\Model\CacheControl $litemageCache
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\HTTP\Header $httpHeader,
\Litespeed\Litemage\Model\CacheControl $litemageCache
) {
parent::__construct($context);
$this->httpHeader = $httpHeader;
$this->litemageCache = $litemageCache;
}

/**
* Returns block content as part of ESI request from Varnish
*
* @return void
*/
public function execute()
{
if (!$this->litemageCache->moduleEnabled()) {
return $this->_errorExit('Abort: LiteMage is not enabled');
}
if ( $this->httpHeader->getHttpUserAgent() !== 'litemage_walker') {
return $this->_errorExit('Access denied');
}

$tags = [];
$req = $this->getRequest();
if ($req->getParam('all')) {
$tags[] = '*';
}
else {
if ($t = $req->getParam('tags')) {
$tags = explode(',', $t);
}
$types = array('P.' => 'products', 'C.' => 'cats', 'S.' => 'stores');
foreach ($types as $prefix => $type) {
if ($ids = $req->getParam($type)) {
$tids = explode(',', $ids);
foreach ($tids as $id) {
$tags[] = $prefix . $id;
}
}
}
$tags = array_unique($tags);
}
if (empty($tags)) {
$this->_errorExit('Invalid url');
}
else {
$this->litemageCache->addPurgeTags($tags, 'ShellPurgeController');
$this->getResponse()->setBody('purged tags ' . implode(',', $tags));
}
}

protected function _errorExit($errorMesg)
{
$resp = $this->getResponse() ;
$resp->setHttpResponseCode(500) ;
$resp->setBody($errorMesg) ;
$this->litemageCache->debugLog('litemage/shell/purge ErrorExit: ' . $errorMesg) ;
}
}
2 changes: 1 addition & 1 deletion Model/App/Response/HttpPlugin.php
Expand Up @@ -59,7 +59,7 @@ public function beforeSendResponse(\Magento\Framework\App\Response\Http $subject
if ($subject instanceof \Magento\Framework\App\PageCache\NotCacheableInterface) {
return;
}
if ($this->litemageCache->moduleEnabled()) {
if ($this->litemageCache->moduleEnabled()) {
$this->litemageCache->setCacheControlHeaders($subject);
}
}
Expand Down
42 changes: 30 additions & 12 deletions Model/CacheControl.php
Expand Up @@ -59,8 +59,9 @@ class CacheControl
protected $_ttl = 0;

/** @var \Magento\Framework\App\Http\Context */
protected $context;
protected $httpContext;
protected $request;
protected $context;

/** @var \Magento\Framework\Stdlib\CookieManagerInterface */
protected $cookieManager;
Expand All @@ -76,21 +77,24 @@ class CacheControl
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
* @param \Magento\Framework\App\Request\Http $request,
* @param \Magento\Framework\App\Action\Context $context,
* @param \Litespeed\Litemage\Model\Config $config,
* @param \Litespeed\Litemage\Helper\Data $helper
*/
public function __construct(\Magento\Framework\App\Http\Context $httpContext,
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\App\Action\Context $context,
\Litespeed\Litemage\Model\Config $config,
\Litespeed\Litemage\Helper\Data $helper
)
{
$this->context = $httpContext;
$this->httpContext = $httpContext;
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
$this->request = $request;
$this->context = $context;
$this->config = $config;
$this->helper = $helper;
$this->_moduleEnabled = $config->moduleEnabled();
Expand Down Expand Up @@ -123,15 +127,22 @@ public function debugEnabled()
return $this->_debug;
}

public function addPurgeTags($tags)
/**
* Add purgeable tags
* @param array $tags
* @param string $source
*
*/
public function addPurgeTags($tags, $source)
{
$this->debugLog("add purge Tags " . print_r($tags, true));
if (is_array($tags)) {
$this->_purgeTags = array_merge($this->_purgeTags, $tags);
} else if ($tags) {
$this->_purgeTags[] = $tags;
}
$this->_purgeTags = array_unique(($this->_purgeTags));
if (!empty($tags)) {
$this->_purgeTags = array_unique(array_merge($this->_purgeTags, $tags));
}
if ($this->_debug) {
$this->debugLog('add purge tags from '
. $source . ' : ' . implode(',', $tags)
. ' Result=' . implode(',',$this->_purgeTags) );
}
}

public function setCacheable($isCacheable, $ttl = 0)
Expand Down Expand Up @@ -274,7 +285,7 @@ protected function _setCacheTagHeader($response)
protected function _checkCacheVary()
{
$varyString = null;
$data = $this->context->getData();
$data = $this->httpContext->getData();
if (!empty($data)) {
ksort($data);
$varyString = sha1(serialize($data));
Expand Down Expand Up @@ -305,7 +316,14 @@ public function setPurgeHeaders($response)
$purgeTags = $this->translateTags(implode(',', $this->_purgeTags));
}
$response->setHeader(self::LSHEADER_PURGE, $purgeTags);
$this->debugLog('set purge header ' . $purgeTags);
$om = $this->context->getObjectManager();
$state = $om->get('Magento\Framework\App\State');
if ($state->getAreaCode() === \Magento\Framework\App\Area::AREA_ADMINHTML) {
// if in adminhtml, show the message
$this->context->getMessageManager()->addSuccessMessage('Informed LiteSpeed Web Server to purge ' . $purgeTags);
}

$this->debugLog('Set purge header ' . $purgeTags);
}
}

Expand Down
40 changes: 23 additions & 17 deletions Model/Config.php
Expand Up @@ -27,13 +27,6 @@
use Magento\Framework\Filesystem;
use Magento\Framework\Module\Dir;

/**
* Model is responsible for replacing default vcl template
* file configuration with user-defined from configuration
*
* @author Magento Core Team <core@magentocommerce.com>
*/

/**
* Class Config
*
Expand Down Expand Up @@ -77,7 +70,13 @@ class Config
protected $reader;
protected $_debug = -1; // avail value: -1(not set), true, false

protected $_moduleEnabled = false;
/**
* @var int moduleStatus bitmask
* 1: SERVER variable set
* 2: FPC enabled
* 4: FPC type is LITEMAGE
*/
protected $_moduleStatus = 0;

/**
* @param Filesystem\Directory\ReadFactory $readFactory
Expand All @@ -88,22 +87,24 @@ class Config
public function __construct(
\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\Cache\StateInterface $cacheState,
\Magento\PageCache\Model\Config $pagecacheConfig,
\Magento\Framework\Module\Dir\Reader $reader,
\Magento\Framework\App\State $state
) {
$this->readFactory = $readFactory;
$this->scopeConfig = $scopeConfig;
$this->reader = $reader;

if (isset($_SERVER['X-LITEMAGE']) && $_SERVER['X-LITEMAGE']) {
$this->_moduleStatus |= 1;
}
if ($pagecacheConfig->isEnabled()) {
$this->_moduleStatus |= 2;
}
if ($pagecacheConfig->getType() == self::LITEMAGE) {
$this->_moduleStatus |= 4;
}

if (isset($_SERVER['X-LITEMAGE']) && $_SERVER['X-LITEMAGE']) {
if ($pagecacheConfig->isEnabled()
&& $pagecacheConfig->getType() == self::LITEMAGE
&& $cacheState->isEnabled(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER)) {
$this->_moduleEnabled = true;
}
}
if ($state->getMode() === \Magento\Framework\App\State::MODE_PRODUCTION) {
// turn off debug for production
$this->_debug = 0;
Expand All @@ -122,9 +123,14 @@ public function __construct(
*/
public function moduleEnabled()
{
return $this->_moduleEnabled;
return ($this->_moduleStatus == 7);
}

public function cliModuleEnabled()
{
return ($this->_moduleStatus == 6);
}

public function debugEnabled()
{
return $this->_debug;
Expand Down
11 changes: 9 additions & 2 deletions Model/System/Config/Source/ApplicationPlugin.php
Expand Up @@ -45,7 +45,7 @@ public function afterToOptionArray(\Magento\PageCache\Model\System\Config\Source
if ($this->_hasLicense()) {
$result[] = [
'value' => \Litespeed\Litemage\Model\Config::LITEMAGE,
'label' => __('LiteMage Cache in LiteSpeed Web Server')
'label' => __('LiteMage Cache within LiteSpeed Server')
];
}
return $result;
Expand All @@ -66,7 +66,14 @@ public function afterToArray(Magento\PageCache\Model\System\Config\Source\Applic

protected function _hasLicense()
{
return (isset($_SERVER['X-LITEMAGE']) && $_SERVER['X-LITEMAGE']);
if (isset($_SERVER['X-LITEMAGE']) && $_SERVER['X-LITEMAGE']) {
return true; // for lsws
}
elseif (isset($_SERVER['HTTP_X_LITEMAGE']) && $_SERVER['HTTP_X_LITEMAGE']) {
return true; // for lslb
}
else
return false;
}

}
42 changes: 20 additions & 22 deletions Observer/FlushAllCache.php
Expand Up @@ -29,42 +29,40 @@

class FlushAllCache implements ObserverInterface
{
protected $config;

/** @var \Magento\Framework\Event\ManagerInterface */
protected $eventManager;


/**
* @var \Litespeed\Litemage\Model\CacheControl
* @param \Litespeed\Litemage\Model\Config $config,
* @param \Magento\Framework\Event\ManagerInterface $eventManager,
*/
protected $litemageCache;

/**
* @var \Magento\Framework\App\Action\Context
*/
protected $context;

/**
* @param \Litespeed\Litemage\Model\CacheControl $litemageCache
* @param \Magento\Framework\App\Action\Context $context
*/
public function __construct(\Litespeed\Litemage\Model\CacheControl $litemageCache,
\Magento\Framework\App\Action\Context $context)
public function __construct(\Litespeed\Litemage\Model\Config $config,
\Magento\Framework\Event\ManagerInterface $eventManager)
{
$this->litemageCache = $litemageCache;
$this->context = $context;
$this->config = $config;
$this->eventManager = $eventManager;
}

/**
* Flush Litemage cache
* Flush All Litemage cache
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->litemageCache->moduleEnabled()) {
$this->litemageCache->addPurgeTags('*');
$this->messageManager = $this->context->getMessageManager();
$this->messageManager->addSuccess('litemage purge all');
$this->litemageCache->debugLog('purge all invoked');
if (PHP_SAPI == 'cli') {
// from command line
if ($this->config->cliModuleEnabled())
$this->eventManager->dispatch('litemage_cli_purge_all');
}
else if ($this->config->moduleEnabled()) {
$this->eventManager->dispatch('litemage_purge_all');
}
}


}

0 comments on commit ee1f0de

Please sign in to comment.