0

In admin side, I want to clean "eav" cache type on records save. I added code in afterSave() function and function also call after save records. But, I think it's not working because, parent afterSave() action perform after my function execute.

My code :

app/code/VendorName/ModuleName/Model/ResourceModel/MyFile.php

/**
 * Action perform after save/duplicate records
 *
 * @param \Magento\Framework\DataObject $customer
 * @return $this
 * @throws \Magento\Framework\Exception\LocalizedException
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
protected function _afterSave(\Magento\Framework\DataObject $object) {
    $this->cacheManager->clean(['eav']);
    return parent::_afterSave($object);
}

Note : After save records if I execute php bin/magento c:c eav Then, I received perfect output.

How can I solve this point?

Anyhelp would be appriciated.

Thanks.

1

Can you try this,

<?php

    protected $cacheTypeList;
    protected $cacheFrontendPool;

    public function __construct(
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
    ) {
        $this->cacheTypeList = $cacheTypeList;
        $this->cacheFrontendPool = $cacheFrontendPool;
    }

    public function flushCache()
    {
        $_types = array_keys($this->cacheTypeList->getTypes()); //For all cache clean.

        foreach ($_types as $type) {
                $this->cacheTypeList->cleanType($type);
        }
        foreach ($this->cacheFrontendPool as $cacheFrontend) {
            $cacheFrontend->getBackend()->clean();
        }
    }

UPDATE :

For single cache clean :

        $this->cacheTypeList->cleanType('eav'); // write cache tag name
        foreach ($this->cacheFrontendPool as $cacheFrontend) {
            $cacheFrontend->getBackend()->clean();
        }
4
  • Not working : It returns error : getBackend() undefined method. – Rohan Hapani Jul 19 '19 at 11:12
  • it's working in my module – Mohit Rane Jul 19 '19 at 11:13
  • Thanks I forgot to add getBackend() in my code. It's working now. – Rohan Hapani Jul 19 '19 at 11:26
  • 1
    @RohanHapani Thanks for the edit, good to know for single cache clean – Mohit Rane Jul 19 '19 at 11:30
1

Try this code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheTypeList = $objectManager->get(\Magento\Framework\App\Cache\TypeListInterface::class);
$cacheFrontendPool = $objectManager->get(\Magento\Framework\App\Cache\Frontend\Pool::class);
$types = array(
    'config',
    'layout',
    'block_html',
    'collections',
    'reflection',
    'db_ddl',
    'eav',
    'config_integration',
    'config_integration_api',
    'full_page',
    'translate',
    'config_webservice'
    );
foreach ($types as $type) {
    $cacheTypeList->cleanType($type);
}
foreach ($cacheFrontendPool as $cacheFrontend) {
   $cacheFrontend->getBackend()->clean();
}

Hope It helps

5
  • What is $this->_cacheFrontendPool? It returns error. – Rohan Hapani Jul 19 '19 at 11:01
  • i just updated the answer please try this – D C Bliss Jul 19 '19 at 11:09
  • Not working. It returns error : getBackend() undefined method. – Rohan Hapani Jul 19 '19 at 11:11
  • I just checked it and it's working in my module – D C Bliss Jul 19 '19 at 11:22
  • Thanks for effort +1 for that. But, Your answer using object Manger. So, I accept 2nd answer :) Thanks I forgot to add getBackend() in my code. – Rohan Hapani Jul 19 '19 at 11:26

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.