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

New Feature - Dedicated MegaMenu Cache #5

Open
wants to merge 4 commits 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
322 changes: 163 additions & 159 deletions Block/Menu.php

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions Helper/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php


namespace Magiccart\Magicmenu\Helper;

use Magento\Framework\App\Helper;

class Cache extends Helper\AbstractHelper
{
const CACHE_TAG = 'MEGAMENU';
const CACHE_ID = 'megamenu';
const CACHE_LIFETIME = 86400;

protected $cache;
protected $cacheState;
protected $storeManager;
private $storeId;

/**
* Cache constructor.
* @param Helper\Context $context
* @param \Magento\Framework\App\Cache $cache
* @param \Magento\Framework\App\Cache\State $cacheState
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
Helper\Context $context,
\Magento\Framework\App\Cache $cache,
\Magento\Framework\App\Cache\State $cacheState,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->cache = $cache;
$this->cacheState = $cacheState;
$this->storeManager = $storeManager;
$this->storeId = $storeManager->getStore()->getId();
parent::__construct($context);
}

/**
* @param $method
* @param array $vars
* @return string
*/
public function getId($method, $vars = array())
{
return base64_encode($this->storeId . self::CACHE_ID . $method . implode('', $vars));
}

/**
* @param $cacheId
* @return bool|string
*/
public function load($cacheId)
{
if ($this->cacheState->isEnabled(self::CACHE_ID)) {
return $this->cache->load($cacheId);
}

return FALSE;
}

/**
* @param $data
* @param $cacheId
* @param int $cacheLifetime
* @return bool
*/
public function save($data, $cacheId, $cacheLifetime = self::CACHE_LIFETIME)
{
if ($this->cacheState->isEnabled(self::CACHE_ID)) {
$this->cache->save($data, $cacheId, array(self::CACHE_TAG), $cacheLifetime);
return TRUE;
}
return FALSE;
}
}
25 changes: 25 additions & 0 deletions Model/Cache/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Magiccart\Magicmenu\Model\Cache;

class Type extends \Magento\Framework\Cache\Frontend\Decorator\TagScope
{
/**
* Cache type code unique among all cache types
*/
const TYPE_IDENTIFIER = 'megamenu';

/**
* Cache tag used to distinguish the cache type from all other cache
*/
const CACHE_TAG = 'MEGAMENU';

/**
* @param \Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool
*/
public function __construct(\Magento\Framework\App\Cache\Type\FrontendPool $cacheFrontendPool)
{
parent::__construct($cacheFrontendPool->get(self::TYPE_IDENTIFIER), self::CACHE_TAG);
}

}
7 changes: 7 additions & 0 deletions etc/cache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="megamenu" translate="label,description" instance="Magiccart\Magicmenu\Model\Cache\Type">
<label>Megamenu</label>
<description>Main Menu Cache</description>
</type>
</config>
3 changes: 3 additions & 0 deletions view/frontend/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
<block class="Magiccart\Magicmenu\Block\Menu" ifconfig="magicmenu/accordion/enabled" name="accordion.sidebar" before="-"
template="accordion.phtml" />
</referenceContainer>
<referenceContainer name="content">
<block class="Magiccart\Magicmenu\Block\Menu" name="megamenu.active" after="-" template="active.phtml" />
</referenceContainer>
</body>
</page>
20 changes: 20 additions & 0 deletions view/frontend/templates/active.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
$categoryId = ($this->getCurrentCategory()->getId())? $this->getCurrentCategory()->getId() : '';
?>

<script type="text/javascript">
require(['jquery'], function($) {
$(document).ready(function () {

$(".magicmenu ul").find("[data-categoryid='<?php echo $categoryId ?>']").parents('li').addClass('active');

<?php
$accordion = $this->_sysCfg->accordion;
if($accordion['enabled']):
?>
$(".accordion").find("[data-categoryid='<?php echo $categoryId ?>']").addClass('active');
$(".accordion").find("[data-categoryid='<?php echo $categoryId ?>']").parents('li').addClass('active');
<?php endif; ?>
});
});
</script>