magento-lts icon indicating copy to clipboard operation
magento-lts copied to clipboard

Add new method to get config value directly from DB bypassing cache.

Open kiatng opened this issue 1 year ago • 1 comments

Description (*)

We can save a config value by

Mage::getConfig()->saveConfig($path, $newValue);

However, if cache is enabled, after saving $newValue, the following will return the old value:

Mage::getStoreConfig($path);

To get the new value, we need to refresh the cache with Mage::getConfig()->reinit();.

In my use case, I needed to get and update config value in a cron running constantly. It's not good idea to keep refreshing cache. (Also, I need to allow admin users to edit the value in the System Configuration. It turns out that values here are updated and not from the cache.)

With this PR, we can get the current value by

Mage::getResourceSingleton('core/config')->getValue($path);

Fixed Issues (if relevant)

Over the years, others need similar feature, see stackoverflow

Manual testing scenarios (*)

$path = 'test/config';
Mage::getConfig()->saveConfig($path, 'foo');
echo Mage::getStoreConfig($path); // null
echo Mage::getResourceSingleton('core/config')->getValue($path); // foo

kiatng avatar Aug 24 '24 02:08 kiatng

UnitTest requires a DB ... added in #4138

sreichel avatar Aug 28 '24 02:08 sreichel

In my use case, I needed to get and update config value in a cron running constantly.

It's up to you, but in my opinion, this is a pattern that should never be used since the config cache should only be updated by users or on deployment. I usually use core/flag instead, very convenient and fast and no need to touch cache. Just a thought.

colinmollenhour avatar Sep 05 '24 23:09 colinmollenhour

Core config is not the right place for frequently changing values, but its "common" practice. Having a method to bypass cache is a +1.

I think most dont know about core/flag .... maybe worth a write-up?

sreichel avatar Sep 05 '24 23:09 sreichel

I am aware of core/flag and use it.

Some errors can only be captured with error_get_last().

// index.php, before Mage::run($mageRunCode, $mageRunType);
register_shutdown_function(function(){
    $err = error_get_last();
    if ($err && $err['type'] != E_WARNING) {
        $err['type'] = $err['type'] . ':' . array_search($err['type'], get_defined_constants(true)['Core']);
        $err['uri'] = $_SERVER['REQUEST_URI'] ?? $_SERVER['SCRIPT_NAME'];
        [$err['user'], $err['role']] = Mage::helper('base')->getSessionUser();
        Mage::getModel('core/flag', ['flag_code' => 'error_get_last'])
            ->loadSelf()
            ->setFlagData($err)
            ->save();
    }
});

And get the last error:

$flag = Mage::getModel('core/flag', ['flag_code' => 'error_get_last'])->loadSelf();

But in my use case, the admin users need to view and edit the value. So I can't use core/flag.

kiatng avatar Sep 06 '24 02:09 kiatng

In my use case, I needed to get and update config value in a cron running constantly.

It's up to you, but in my opinion, this is a pattern that should never be used since the config cache should only be updated by users or on deployment. I usually use core/flag instead, very convenient and fast and no need to touch cache. Just a thought.

I think there plenty of real use-cases for this ... e.g update/(re-)set a timestamp from backend, Set a value thats used in a cron-job ...

IMHO theres nothing wrong to provide this posibility.

sreichel avatar Sep 17 '24 07:09 sreichel