I have developed a custom module the retrieve data from database and displayed in a block. The data is updated in database but the page is not showing the latest data if cached is enabled. If the cache is disabled, everything just works fine. Sometimes, even cannot get the customer ID from session if cache is enabled. How to resolve this issue?
Magento 2 Cache Issue – Page Content Not Refreshing
cachemagento2
Related Solutions
I have updated my response. In my previous answer, I had a block cacheable=false. This is incorrect because it do the page is not cacheable.
For knowing if the customer is logged or not, you must do the next:
Namespace/Modulename/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Model\Session">
<plugin name="namespace_module_customer_plugin_session" type="Namespace\Modulename\Plugin\Customer\Model\Session"/>
</type>
</config>
Namespace/Modulename/Plugin/Customer/Model/Session.php
<?php
namespace Namespace\Modulename\Plugin\Customer\Model;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Http\Context;
/**
* Class Session
*/
class Session
{
/**
* @var Context
*/
private $httpContext;
/**
* Session constructor.
*
* @param Context $context
*/
public function __construct(
Context $context
) {
$this->httpContext = $context;
}
/**
* Check http context to know if user is really logged in
*
* @param CustomerSession $subject
* @param bool $isLoggedIn
* @return bool
*/
public function afterIsLoggedIn(CustomerSession $subject, $isLoggedIn)
{
return $isLoggedIn ?: $this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
}
}
When you do $customerSession->isLoggedIn()
, it will return the correct value.
To retrieve the id of the customer, the best way is how says @SohelRana in its comment, using ajax or private cache. https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cache/page-caching/private-content.html
Picking up from where you got stuck....
the implementation of the interface StateInterface
is Magento\Framework\App\Cache\State
.
method setEnabled
looks like this:
public function setEnabled($cacheType, $isEnabled)
{
$this->load();
$this->statuses[$cacheType] = (int)$isEnabled;
}
This is in a default install. If you have other custom modules that use a different implementation for the Cache states interface maybe you should look in there.
Moving on to the persist
method...
It should be in the same class
public function persist()
{
$this->load();
$this->writer->saveConfig([ConfigFilePool::APP_ENV => [self::CACHE_KEY => $this->statuses]]);
}
this is the actual method that should write in env.php
.
You can easily test if when running the command you reach this method.
(use a simple var_dump, die for test purposes).
If you do not reach this code it means you are using a different implementation for this interface.
And you can check this easily by adding
echo get_class($this->cacheState);exit;
(or use xdebug and adding a breakpoint)
right before $this->cacheState->setEnabled($type, $isEnabled);
Best Answer
Cacheable false should be avoid. It set complete as a non-cache if added false in layout