Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Sep 17, 2024

Conversation

kiatng
Copy link
Contributor

@kiatng kiatng commented Aug 24, 2024

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

@github-actions github-actions bot added the Component: Core Relates to Mage_Core label Aug 24, 2024
@sreichel sreichel closed this Aug 28, 2024
@sreichel sreichel deleted the get_config_value branch August 28, 2024 01:14
@sreichel sreichel restored the get_config_value branch August 28, 2024 02:24
@sreichel sreichel reopened this Aug 28, 2024
@sreichel
Copy link
Contributor

UnitTest requires a DB ... added in #4138

@colinmollenhour
Copy link
Member

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.

@sreichel
Copy link
Contributor

sreichel commented Sep 5, 2024

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?

@kiatng
Copy link
Contributor Author

kiatng commented Sep 6, 2024

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.

@sreichel
Copy link
Contributor

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 sreichel merged commit 946f15a into OpenMage:main Sep 17, 2024
17 checks passed
@sreichel sreichel deleted the get_config_value branch September 17, 2024 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Relates to Mage_Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants