0

I am in a project which uses 3 servers to serve a Magento 2 site. I have written a code to clear cache and re-indexing. But for certain reasons the cache flush has to happen in all the three servers. The reason i am looking for these approach because after every csv product update we need to manually run all these commands. So, I am looking for a programmatic approach by which i can trigger the re-indexing and cache flush commands after CSV product upload.

<?php
/**
 * 
 * Reindex the indexes and perform chache flush
 *
 */
 namespace Retailinsights\Testing\Controller\Index;

 class Test extends \Magento\Framework\App\Action\Action {

  protected $_indexerFactory;

  public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
    \Magento\Indexer\Model\IndexerFactory $indexerFactory
  ) {
    parent::__construct($context);
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheFrontendPool = $cacheFrontendPool;
    $this->_indexerFactory = $indexerFactory;
  }

  public function reindexAll() {

    /* reindexs the following category */
    $indexer_ids = [
        //"design_config_grid",
        //"customer_grid",
        "catalog_category_product",
        "catalog_product_category",
        "catalog_product_price",
        "catalog_product_attribute",
        "catalogsearch_fulltext",
        "cataloginventory_stock",
        //"catalogrule_rule",
        //"catalogrule_product"
    ];

    foreach ($indexer_ids as $indexerId) {

        $indexerFactoryObject = $this->_indexerFactory->create();
        $indexerFactoryObject->load($indexerId);                
        $indexerFactoryObject->reindexAll();

        // destroy object
        unset($indexerFactoryObject);
    }
    return $this;
  }

  public function cacheFlush() {

    $typeCollection = $this->_cacheTypeList->getTypes();

    foreach ($typeCollection as $type) {
        $this->_cacheTypeList->cleanType($type->getId());
    }

    foreach ($this->_cacheFrontendPool as $cacheFrontend) {
        $cacheFrontend->getBackend()->clean();
    }
  }

  public function execute() {

    /**
     *  reindex 
     */
    $this->reindexAll();
    /**
     *  Cache Flush
     */
    $this->cacheFlush();

    }
  }
  • you can create root file so after csv import just hit the url in browser – Sourav Jan 2 at 6:29
0

Create reindex_cache.php file into magento root directory and copy below code into that file.

Url: After csv import just hit the domain.com/reindex_cache.php in browser.

 <?php
    try {
        require __DIR__ . '/app/bootstrap.php';
    } catch (\Exception $e) {
        echo <<<HTML
    <div style="font:12px/1.35em arial, helvetica, sans-serif;">
        <div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;">
            <h3 style="margin:0;font-size:1.7em;font-weight:normal;text-transform:none;text-align:left;color:#2f2f2f;">
            Autoload error</h3>
        </div>
        <p>{$e->getMessage()}</p>
    </div>
    HTML;
        exit(1);
    }


    error_reporting(1);
    ini_set('max_execution_time', 0);
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $objectManager = $objectManager;

    // set whether frontend or admin
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('adminhtml');

    // code for reindexing 
    $indexerCollectionFactory = $objectManager->get("\Magento\Indexer\Model\Indexer\CollectionFactory");
    $indexerFactory = $objectManager->get("\Magento\Indexer\Model\IndexerFactory");

    $indexerCollection = $indexerCollectionFactory->create();
    $allIds = $indexerCollection->getAllIds();
    foreach ($allIds as $id) {
        $indexer = $indexerFactory->create()->load($id);
        $indexer->reindexAll();
    }

// code for caching

try{
    $_cacheTypeList = $objectManager->create('Magento\Framework\App\Cache\TypeListInterface');
    $_cacheFrontendPool = $objectManager->create('Magento\Framework\App\Cache\Frontend\Pool');
    $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();
    }
}catch(Exception $e){

}

?>
  • Hi Sourav, If you closely look at my code above, i have implemented the same code as you have specified. Adding to that i was this code to get executed in all the 3 servers from one php file. – kaushik kumar roy Jan 2 at 9:10
  • It may be risk to clean and re-index from database level. – Sourav Jan 3 at 6:09
  • Please mark my answer so other take benefit from this. – Sourav Jan 3 at 6:10

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.