1

I want to invalidate particular cache tags when admin saves an attribute. How should I do it.

| improve this question | | | | |
3

OK. In that case, use cache invalidator in Controller. I found an example for you. Execute method in Controller class must be:

public function execute()
   {
       /** @var Page $resultPage */
       $resultPage = $this->resultPageFactory->create();

       /* Invalidate Full Page Cache */
       $this->cache->invalidate('YOUR_CUSTOM_CACHE_TYPE_CODE');

       return $resultPage;
   }

See more here.

| improve this answer | | | | |
2

Create observer

use Magento\Framework\Event\ObserverInterface;

class CacheInvalidator implements ObserverInterface
{
 /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $typeList;

    /**
     * @var \Magento\PageCache\Model\Config
     */
    protected $config;

    /**
     * @param \Magento\PageCache\Model\Config $config
     * @param \Magento\Framework\App\Cache\TypeListInterface $typeList
     */
    public function __construct(
        \Magento\PageCache\Model\Config $config,
        \Magento\Framework\App\Cache\TypeListInterface $typeList
    ) {
        $this->config = $config;
        $this->typeList = $typeList;
    }
}

$config will help you check cache enabling. Create execute method in CacheInvalidator class with

if ($this->config->isEnabled()) {
            $this->typeList->invalidate(
                \Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER
            );
        }
| improve this answer | | | | |
  • No, This is to invalidate full page cache only. I want a code to invalidate my custom cache. – Pankaj Bhope Feb 25 '17 at 13:36
  • To change this from invalidating the full page cache, just change the string being passed into the typeList::invalidate method from '\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER' to 'YOUR_CUSTOM_CACHE_TYPE_CODE' – Nathaniel Rogers Nov 4 '19 at 1:01
0

You can inject a class that implements \Magento\Framework\App\Cache\TypeListInterface

Then, i.e. if you want to mark a cache as invalid, or even clear it, you can do something like this:

$this->cacheTypeList->invalidate(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);

or

$this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.