Skip to content

Commit

Permalink
FlushCacheByTags - add extra layer - will either trigger cli mode or …
Browse files Browse the repository at this point in the history
…http mode.
  • Loading branch information
litespeedtech committed Feb 10, 2017
1 parent d1fcea2 commit 299ded0
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 106 deletions.
19 changes: 4 additions & 15 deletions Controller/Shell/Purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* 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)
* @copyright Copyright (c) 2016-2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

Expand Down Expand Up @@ -68,21 +68,10 @@ public function execute()
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);
elseif ($t = $req->getParam('tags')) {
$tags = explode(',', $t);
}

if (empty($tags)) {
$this->_errorExit('Invalid url');
}
Expand Down
2 changes: 1 addition & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* 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)
* @copyright Copyright (c) 2016-2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

Expand Down
67 changes: 41 additions & 26 deletions Model/CacheControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,9 @@ protected function _setCacheTagHeader($response)
{
$lstags = '';
if (!empty($this->_cacheTags)) {
$lstags = $this->_cacheTags;
$lstags = $this->translateTags($this->_cacheTags);
if (is_array($lstags)) {
$lstags = implode(',', $this->_cacheTags);
$lstags = $this->translateTags($lstags);
$lstags = implode(',', array_unique($lstags));
}
$response->setHeader(self::LSHEADER_CACHE_TAG, $lstags);
$response->clearHeader('X-Magento-Tags');
Expand Down Expand Up @@ -314,22 +313,23 @@ protected function _checkCacheVary()

public function setPurgeHeaders($response)
{
if (!empty($this->_purgeTags)) {
if (in_array('*', $this->_purgeTags)) {
$purgeTags = '*';
} else {
$purgeTags = $this->translateTags(implode(',', $this->_purgeTags));
}
$response->setHeader(self::LSHEADER_PURGE, $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);
}
if (empty($this->_purgeTags))
return;

if (in_array('*', $this->_purgeTags)) {
$purgeTags = '*';
} else {
$purgeTags = 'tag=' . implode(',tag=', array_unique($this->translateTags($this->_purgeTags)));
}
$response->setHeader(self::LSHEADER_PURGE, $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);
}

public function addCacheTags($tags)
Expand All @@ -347,14 +347,27 @@ public function setCacheTags($tags)
$this->_cacheTags = $tags;
}

public function translateTags($tagString)
/**
* translateTags
* @param string or array of strings $tagString
* @return string or array
*/
// input can be array or string
public function translateTags($tagString)
{
$lstags = str_replace(['_block', 'catalog_product_', 'catalog_category_',
'catalog_category_product_'], ['.B', 'P.', 'C.', 'C.'], $tagString);
// $this->debugLog("in translate tags from = $tagString , to = $lstags");
$search = ['_block',
'catalog_product_',
'catalog_category_product_', // sequence matters, need to be in front of shorter ones
'catalog_category_' ];
$replace = ['.B', 'P.', 'C.', 'C.'];

$lstags = str_replace($search, $replace, $tagString);

//$this->debugLog("in translate tags from = $tagString , to = $lstags");
return $lstags;
}

// used by ESI blocks
public function getElementCacheTags($layout, $elementName)
{
if (!$layout->hasElement($elementName))
Expand Down Expand Up @@ -389,10 +402,12 @@ public function getElementCacheTags($layout, $elementName)
}
}
}
$lstag = $this->translateTags(implode(',', array_unique($tags)));
if ($lstag == '') {
if (!empty($tags)) {
$lstag = implode(',', array_unique($this->translateTags($tags)));
}
else {
$lstag = $elementName;
}
}
return $lstag;
}

Expand Down
14 changes: 7 additions & 7 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,21 @@ public function __construct(
}


/**
/**
* Check if LiteMage module is enabled based on LiteSpeed license and config
*
* @return bool
*/
public function moduleEnabled()
{
return ($this->_moduleStatus == 7);
if (PHP_SAPI == 'cli') {
return ($this->_moduleStatus == 6);
}
else {
return ($this->_moduleStatus == 7);
}
}

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

public function debugEnabled()
{
return $this->_debug;
Expand Down
16 changes: 11 additions & 5 deletions Observer/FlushAllCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* 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)
* @copyright Copyright (c) 2016-2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

Expand All @@ -29,6 +29,9 @@

class FlushAllCache implements ObserverInterface
{
/**
* @var \Litespeed\Litemage\Model\Config
*/
protected $config;

/** @var \Magento\Framework\Event\ManagerInterface */
Expand All @@ -54,13 +57,16 @@ public function __construct(\Litespeed\Litemage\Model\Config $config,
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if (!$this->config->moduleEnabled())
return;

$tags = ['tags' => ['*']];
if (PHP_SAPI == 'cli') {
// from command line
if ($this->config->cliModuleEnabled())
$this->eventManager->dispatch('litemage_cli_purge_all');
$this->eventManager->dispatch('litemage_cli_purge', $tags);
}
else if ($this->config->moduleEnabled()) {
$this->eventManager->dispatch('litemage_purge_all');
else {
$this->eventManager->dispatch('litemage_purge', $tags);
}
}

Expand Down
36 changes: 28 additions & 8 deletions Observer/FlushCacheByCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* 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)
* @copyright Copyright (c) 2016-2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

Expand Down Expand Up @@ -78,14 +78,34 @@ public function __construct(\Litespeed\Litemage\Model\Config $config,
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->config->cliModuleEnabled()
&& ($this->coreRegistry->registry('shellPurgeAll') === null)) {
$this->coreRegistry->register('shellPurgeAll', 1);
$this->_shellPurgeAll();
if (!$this->config->moduleEnabled())
return;

$event = $observer->getEvent();
$tags = $event->getTags();

if (in_array('*', $tags)) {
if ($this->coreRegistry->registry('shellPurgeAll') === null) {
$this->coreRegistry->register('shellPurgeAll', 1);
$this->_shellPurge(['all' => 1]);
}
}
else {
$tags = array_unique($tags);
$used = [];
foreach ($tags as $tag) {
if ($this->coreRegistry->registry("shellPurge_{$tag}") === null) {
$this->coreRegistry->register("shellPurge_{$tag}", 1);
$used[] = $tag;
}
}
if (!empty($used)) {
$this->_shellPurge(['tags' => implode(',', $used)]);
}
}
}

protected function _shellPurgeAll()
protected function _shellPurge($params)
{
$client = $this->httpClientFactory->create();

Expand All @@ -95,7 +115,8 @@ protected function _shellPurgeAll()

$client->setConfig($clientConfig);
$client->setMethod(\Zend_Http_Client::POST);
$client->setParameterPost('all', 1);
foreach($params as $k => $v)
$client->setParameterPost($k, $v);

$server_ip = false; //in future, allow this configurable.
$base = $this->url->getBaseUrl();
Expand All @@ -121,7 +142,6 @@ protected function _shellPurgeAll()
}

return true;

}

}
38 changes: 20 additions & 18 deletions Observer/FlushCacheByEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* 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)
* @copyright Copyright (c) 2016-2017 LiteSpeed Technologies, Inc. (https://www.litespeedtech.com)
* @license https://opensource.org/licenses/GPL-3.0
*/

Expand Down Expand Up @@ -52,23 +52,25 @@ public function __construct(
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->litemageCache->moduleEnabled()) {
// do not use getEventName() directly, maybe empty
$eventName = $observer->getEvent()->getName();
$tags = [];
switch ($eventName) {
case 'catalog_category_save_after':
case 'catalog_category_delete_after':
$tags[] = 'topnav';
break;
case 'litemage_purge_all':
$tags[] = '*';
break;
}
if (!empty($tags)) {
$this->litemageCache->addPurgeTags($tags , "FlushCacheByEvents $eventName");
}
}
if (!$this->litemageCache->moduleEnabled())
return;

$event = $observer->getEvent();
// do not use getEventName() directly, maybe empty
$eventName = $event->getName();
$tags = [];
switch ($eventName) {
case 'catalog_category_save_after':
case 'catalog_category_delete_after':
$tags[] = 'topnav';
break;
case 'litemage_purge':
$tags = $event->getTags();
break;
}
if (!empty($tags)) {
$this->litemageCache->addPurgeTags($tags , "FlushCacheByEvents $eventName");
}
}

}
Loading

0 comments on commit 299ded0

Please sign in to comment.