Skip to content

Commit 2532407

Browse files
committed
Add CACHE_TAG
1 parent 0032cab commit 2532407

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

Block/Categories.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Magepow\Categories\Block;
1313

14-
class Categories extends \Magento\Framework\View\Element\Template
14+
class Categories extends \Magento\Framework\View\Element\Template implements \Magento\Framework\DataObject\IdentityInterface
1515
{
16+
const DEFAULT_CACHE_TAG = 'MAGEPOW_CATEGORIES';
17+
1618
const XML_PATH = 'category_page';
1719

1820
public $helper;
@@ -46,6 +48,28 @@ public function __construct(
4648
parent::__construct($context, $data);
4749
}
4850

51+
protected function getCacheLifetime()
52+
{
53+
return parent::getCacheLifetime() ?: 86400;
54+
}
55+
56+
public function getCacheKeyInfo()
57+
{
58+
$keyInfo = parent::getCacheKeyInfo();
59+
$categoryId = $this->getCurrentCategory() ? $this->getCurrentCategory()->getId() : 0;
60+
$keyInfo[] = $categoryId;
61+
return $keyInfo;
62+
}
63+
64+
/**
65+
* @return array
66+
*/
67+
public function getIdentities()
68+
{
69+
$categoryId = $this->getCurrentCategory() ? $this->getCurrentCategory()->getId() : 0;
70+
return [self::DEFAULT_CACHE_TAG, self::DEFAULT_CACHE_TAG . '_' . $categoryId];
71+
}
72+
4973
public function getLayout()
5074
{
5175
return $this->helper->getConfig(self::XML_PATH . '/layout');
@@ -71,10 +95,15 @@ public function getExcludeCategory()
7195
return $this->helper->getConfig(self::XML_PATH . '/exclude_category');
7296
}
7397

98+
public function getCurrentCategory()
99+
{
100+
return $this->coreRegistry->registry('current_category');
101+
}
102+
74103
public function getCategories()
75104
{
76105

77-
$category = $this->coreRegistry->registry('current_category');
106+
$category = $this->getCurrentCategory();
78107
if(!$category) return;
79108

80109
$categoryId = $category->getId();

0 commit comments

Comments
 (0)