0

I have tried DI and Object Manager way to clean or flush cache but its not working for me

using DI

public function __construct(
    Context $context,
    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
    parent::__construct($context);
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheFrontendPool = $cacheFrontendPool;
}
$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) {
    $this->_cacheTypeList->cleanType($type);
}
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
    $cacheFrontend->getBackend()->clean();
}

using ObjectManager:

$obj = \Magento\Framework\App\ObjectManager::getInstance();
$indexerCollectionFactory = $obj->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
$indexerFactory = $obj->get("\Magento\Indexer\Model\IndexerFactory");

// custom function for reindexing
public function reIndexing(){
    $indexerCollection = $indexerCollectionFactory->create();
    $allIds = $indexerCollection->getAllIds();

    foreach ($allIds as $id) {
        $indexer = $indexerFactory->create()->load($id);
        //$indexer->reindexRow($id); // or you can use reindexRow according to your need
        $indexer->reindexAll(); // this reindexes all
    }
}
  • Basically i am calling a block function when a product detail page is open and in block function i am using the above code which not flush cache for me .One way is that i use cacheable false its working for me but it disable the cache for all page anywhere the block is call .so i am finding a way in which there is no need of doing cacheable false for block – usman Aug 20 at 5:46
  • 1
    Can you please tell me why do you want to clear cache in product detail page ? – Muzammal Hassan Aug 20 at 5:50
  • @MuzammalHassan bacially i am working on extention development and there i want to display product view count .so i need to clear cache to increment the view whenever the page is requested – usman Aug 20 at 7:36
  • Are you saving view count in the database ? or you getting view count on run time? – Muzammal Hassan Aug 20 at 8:05
0

Hard coding the types is a bad idea. Instead you can use the same method used by the cache:flush and cache:clean commands. The cache manager class can also pull all the cache types for you, as done in the example below.

public function __construct(
    \Magento\Framework\App\Cache\Manager $cacheManager
) {
    $this->cacheManager = $cacheManager;
}

private function whereYouNeedToCleanCache()
{
    $this->cacheManager->flush($this->cacheManager->getAvailableTypes());

    // or this
    $this->cacheManager->clean($this->cacheManager->getAvailableTypes());
}
  • i have tried this before but its also not working – usman Aug 20 at 5:51
  • did you try this ? webkul.com/blog/… – Shafeel Sha Aug 20 at 5:52
  • yes i have tried – usman Aug 20 at 5:52
  • 1
    both one is working well !!! – Shafeel Sha Aug 20 at 5:53
  • <?php $command = 'php bin/magento cache:clean && php bin/magento cache:flush'; echo '<pre>' . shell_exec($command) . '</pre>'; ?> – Shafeel Sha Aug 20 at 5:53

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.