Please see this code first
 public function setRefreshToken($token, $store = null) {
 $scope = 'default';
 $scopeId = 0;
 if ($store !== null) {
 $scope = 'stores';
 $scopeId = $store->getId();
 }
 Mage::getConfig()->saveConfig('clougistic_twinfield/general/refresh_token', $token, $scope, $scopeId);
 Mage::getConfig()->cleanCache();
 }
This is Magento 1 save config and clear cache code, here they save access token in the config for further use and clear cache.
I have used this code to save config
 /**
 *
 * @param \Magento\Framework\App\Helper\Context $context
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
 * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
 * @param \Magento\Framework\Message\ManagerInterface $messageManager
 * @param \Magento\Framework\App\State $state
 */
 public function __construct(
 \Magento\Framework\App\Helper\Context $context,
 \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
 \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
 \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
 \Magento\Framework\Message\ManagerInterface $messageManager,
 \Magento\Framework\App\State $state
 ){
 $this->scopeConfig = $scopeConfig;
 $this->messageManager = $messageManager;
 $this->configWriter = $configWriter;
 $this->cacheTypeList = $cacheTypeList;
 $this->state = $state;
 parent::__construct($context);
 }
 /**
 * save token in config
 *
 * @param string $token
 * @param object $store
 * @return void
 */
 public function setRefreshToken($token, $store = null)
 {
 $scope = 'default';
 $scopeId = 0;
 if ($store !== null) {
 $scope = 'stores';
 $scopeId = $store->getId();
 }
 $this->configWriter->save(self::TWINFIELD_CUSTOMER_REFERESH_TOKEN, $token, $scope, $scopeId);
 $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
 }
it is working fine but it may affect performance. so if anyone has any better way to do these please share.
Thanks in advance.
2 Answers 2
To not affect performance you should not clear all cache. Only clear cache according to your purpose
$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();
}
In $types should only use 1 field according to your purpose
- 
 $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); it will only clear config cache $this->_cacheFrontendPool is an instance of Magento\Framework\App\Cache\Frontend\Pool ? but this config will use globalyBhavesh Prajapati– Bhavesh Prajapati2021年12月15日 14:42:28 +00:00Commented Dec 15, 2021 at 14:42
- 
 can we ignore specific config value to be catch? or store it to some other place?Bhavesh Prajapati– Bhavesh Prajapati2021年12月15日 14:47:54 +00:00Commented Dec 15, 2021 at 14:47
- 
 "config" is the value that you are required to clear when saving any changes in the backend. You will see a message at the top of the page when you save a configuration. So the values are only changed when you clear the cache for this field.LitExtension Magento Migration– LitExtension Magento Migration2021年12月17日 02:33:09 +00:00Commented Dec 17, 2021 at 2:33
- 
 Yes, I know but my problem is different I need to save one value in the config and use that value at another place during execution just like the registry but also need to save it to the config table for further use. here when I generate a token a second time and get the value it returns the value of the previous so I have to clear config cache but it clears all other caches also so it may affect performance that's why I have write this question for a better solution. Thanks.Bhavesh Prajapati– Bhavesh Prajapati2022年03月14日 08:59:44 +00:00Commented Mar 14, 2022 at 8:59
- 
 @BhaveshPrajapati, Have you found any solution on this, if so please share with us, I too need the same functionality on this. - ThanksSathya– Sathya2022年07月29日 06:29:22 +00:00Commented Jul 29, 2022 at 6:29
Although your example is clearing just Config cache type, it also includes DI configurations and other config types that need to be regenerated and take time.
What is being done in Magento admin after saving a new configuration value (let's say changing contact email) is something like
# vendor/magento/module-config/Model/Config.php:215
$deleteTransaction->delete();
$saveTransaction->save();
// re-init configuration
$this->_appConfig->reinit();
In here, reinit() call deletes cached core_config_data (scoped config) values, i138 translations and maybe something else, but much less than calling ->cleanType('config') would.
After that re-caches deleted things.
In your example scenario I would probably do something like this:
public function __construct(
 #...
 private readonly Magento\Framework\App\Config\ReinitableConfigInterface $reinitableConfig
) {
}
public function setRefreshToken($token, $store = null) 
{
 #...
 $this->configWriter->save(self::TWINFIELD_CUSTOMER_REFERESH_TOKEN, $token, $scope, $scopeId);
 $this->reinitableConfig->reinit();
}
Explore related questions
See similar questions with these tags.