0

Latest data does not show at front-end after submitting form in my custom module. when I clear cache then latest data show at front end.

Help me! how to fix that problem.

when I disable cache my module work correctly but I want to displaying latest data at front side without disable cache.

1

2 Answers 2

1

You can flush cache programmatically in your controller, So don't need to clear cache manually and don't need to disable cache for particular block. Please check the below code for cache clear

1.Add below lines after namespace

use Magento\Framework\App\PageCache\Version;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Cache\Frontend\Pool;

2.Define variables

protected $cacheTypeList;

protected $cacheFrontendPool;
  1. Add params to construct method
/**
 * Cache clear
 *
 * @param TypeListInterface $cacheTypeList
 * @param Pool $cacheFrontendPool
 */
public function __construct(
    TypeListInterface $cacheTypeList, 
    Pool $cacheFrontendPool
) {
    $this->cacheTypeList = $cacheTypeList;
    $this->cacheFrontendPool = $cacheFrontendPool;
}
  1. The Last step for clearing the cache in your execute method
    $_types = [
        'config',
        'block_html',
        'full_page',
        'config_webservice'
    ];
 
    foreach ($_types as $type) {
        $this->cacheTypeList->cleanType($type);
    }
    foreach ($this->cacheFrontendPool as $cacheFrontend) {
        $cacheFrontend->getBackend()->clean();
    }

You can check here

Sometimes I also faced this type of issue, at this time I use this way.

I hope it's an alternative way without disabling the cache and manually clearing the cache, Your submitted form data will show in the frontend.

Thanks

2
  • Thank you for giving solution. My custom module work perfectly by this solution. But, I think it is a bad practice because performance(increase loading time of page) of module will be lose by clear cache programmatically.I want to find solution by js. Jan 1, 2022 at 18:37
  • 1
    you are right @MuhammadZahoor, This is a bad practice by clearing cache, it down the performance. Jan 2, 2022 at 4:36
0

Just add cacheable false for the particular PHTML file like below.

<block class="Magento\Review\Block\Customer\Recent" name="customer_account_dashboard_info1" template="Magento_Review::customer/recent1.phtml" cacheable="false"/>
1
  • I know, by this solution it work correctly but I want to finding alternating solution because I doesn't want to define cache false for a block. Kindly give alternative solution for this. Dec 31, 2021 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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