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

Re-set config_item() cache #708

Closed
SlimDeluxe opened this issue Nov 24, 2011 · 6 comments
Closed

Re-set config_item() cache #708

SlimDeluxe opened this issue Nov 24, 2011 · 6 comments

Comments

@SlimDeluxe
Copy link

Hi,
when you change a config item during parse time, the config_item( ) would need to drop it's static "cache".
If you use this procedure:
in config.php:
$config['foo'] = 'bar';
in some controller:
echo config_item('foo'); # produces: bar $this->config->set_item('foo', 'baz'); echo config_item('foo'); # still produces: bar

The workaround is to use the direct Config method: $this->config->item('foo).

This should be fixed or at least noted in the documentation, that the common function config_item() uses a static var for caching purposes and can not be changed during parsetime (if the variable was already read and therefore cached one before).

@appleboy
Copy link
Contributor

I think it is not static var probelm, you should following the config_item function

function config_item($item)
{
    static $_config_item = array();

    if ( ! isset($_config_item[$item]))
    {
        $config =& get_config();

        if ( ! isset($config[$item]))
        {
            return FALSE;
        }
        $_config_item[$item] = $config[$item];
    }

    return $_config_item[$item];
}

If you call "echo config_item('foo')" first, it run "$_config_item[$item] = $config[$item];";

It will return $_config_item[$item]; directly because the $_config_item array has the "foo" index value, if you run "config_item('foo')" twice.

You can change the source code:

    $this->config->set_item('foo', 'baz');
    echo $this->config->item('foo') . "\n"; # produces: baz
    echo config_item('foo') . "\n"; # produces: baz

Thanks.

@SlimDeluxe
Copy link
Author

Mate, you completely missed the point... :)
I need to change a config item based on the user setting, in a loop that processes all users.
Once you call config_item('something') it will be cached in the static variable and there's no way to drop that cache because it is available in local scope only.
The Config::set_item() function should be allowed to drop the cache on config change.

@appleboy
Copy link
Contributor

What's situation you want to change?

You can use set_item(key, value) and item(key) to resolve your problem.

@narfbg
Copy link
Contributor

narfbg commented May 18, 2012

So, this one's an exact duplicate of issue #180.

The use of a static var is intended to speed up things, but it does indeed make it hard to keep track of dynamic config changes. So, there are two options:

  • Make config_item() to be yet another function-to-method alias or in other words - lose the small speed gain coming from the static var use and just point it to CI_Config::item().
  • Make it clear in the documentation that this function should NOT be used with dynamically changed config items and that CI_Config::item() should be used in such cases instead.

... I vote for the second one.

@SlimDeluxe
Copy link
Author

As an OOP I personally would move the static var into "public Config::config", so Config::set_item() would be able to drop/update it. You can still reach Config::config from the helper. But I know this (sadly) is not the way stuff is done in CI...
If I have to choose, I'm for the first option. I never had any speed problems but I did waste lots of hours because of unclear/outdated documentation, or because you don't really read much of it until you encounter a problem :)

@narfbg narfbg closed this as completed in 7b18a3f Nov 4, 2012
nonchip pushed a commit to nonchip/CodeIgniter that referenced this issue Jun 29, 2013
@vlakoff
Copy link
Contributor

vlakoff commented Jul 25, 2013

For reference, has been truly fixed in #2538

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants