Skip to content

CI4: Load ospos_app_config into memory #3656

Closed

Description

@objecttothis

Calling Appconfig->get('foo') every time we need an OSPOS configuration setting is a lot of overhead accessing the database. It would be much better for us to load the app_config table into an array that is accessible everywhere.

CI4 is different in this way also. I was mimicking what we are doing in CI3 by using a post_controller_constructor event (formerly hooks) to a load_config function in a class by the same name which then populates a custom Config called OSPOS that has an associative array as one of its fields. The problem that I'm encountering with this method is that the database data is not persisting. I think this is because calling config('OSPOS')->settings['foo']; creates a new class instance and therefore returns an empty array for settings. Maybe there's a different problem I'm not seeing, but that's what it looks like.

This guy (https://stackoverflow.com/questions/64632046/codeigniter-4-loading-general-settings-from-db) is doing the same thing, but he's populating the associative array in the constructor for the custom config... The problem with this is that, I think, every time config('OSPOS') is called, it will pull the data from the database, and that's no different than just using Appconfig->get().

The third option that I thought about is loading the data from app_config into $_SESSION. This would allow us to access any OSPOS config item simply with the session() helper function <?= session('foobar') ?>

@jekkos can you weigh in on this one? I'm not seeing a downside to storing the contents of the app_config table in the session. None of it has sensitive information like usernames or passwords.

Activity

  1. added this to the 3.4.0 milestone on Feb 11, 2023
  2. self-assigned this
    on Feb 11, 2023
  3. objecttothis commented on Feb 11, 2023

    @objecttothis
    MemberAuthor

    To clarify a bit, it looks like there are encrypted API keys and an encrypted SMTP password stored in app_config. If I understand correctly, the risk is session hijacking. Passive hijacking is difficult because the session is regenerated regularly. Active hijacking requires a MITM attack, but taking over the session doesn't give access to the session data, I don't think.

  4. objecttothis commented on Feb 11, 2023

    @objecttothis
    MemberAuthor

    I did a proof of concept to load app_config into the session data. Not only does it load properly and is referencing it very simple, but it is also persisting until the session is destroyed and it's accessible to Models, Views, Controllers, Helpers and Libraries... the former method I could not get the data to persist.

  5. objecttothis commented on Feb 11, 2023

    @objecttothis
    MemberAuthor
  6. objecttothis commented on Feb 12, 2023

    @objecttothis
    MemberAuthor

    The only downside I can see to session data is in the event you have concurrent users. If user A changes setting X, it will be updated in their session data, but not updated in anyone else's session, so until their session expires, or destroyed (on logout), user B will still be using the old setting for X. We could potentially get around this by destroying all sessions when one of the app_config values is changed, but that seems a little hacky to me.

  7. objecttothis commented on Feb 12, 2023

    @objecttothis
    MemberAuthor

    It seems that our options are down to two, unless I'm missing something

    1. Use cache to store the app_config table in memory, then in the constructor for our custom config pull the data from cache, or refresh the cache if it's no longer cached then pull the data.
    2. Populate the settings array from the app_config() table but try to instantiate it as few times as possible. This would mean not using the config() convience method throughout the view, but calling it one time in the constructor and passing it to the view or calling it one time per class/view and then just referencing that throughout the constructor/view. 2 is easier, but may cause more overhead.
      @jekkos thoughts?
  8. objecttothis commented on Feb 12, 2023

    @objecttothis
    MemberAuthor

    I've implemented the cache option but there is a small problem. Cache Config has the following

        /**
         * --------------------------------------------------------------------------
         * Reserved Characters
         * --------------------------------------------------------------------------
         *
         * A string of reserved characters that will not be allowed in keys or tags.
         * Strings that violate this restriction will cause handlers to throw.
         * Default: {}()/\@:
         * Note: The default set is required for PSR-6 compliance.
         */
        public string $reservedCharacters = '{}()/\@:';
    

    The problem is that we use { } in a few places and we definitely use @ in storing emails. To get around this we either need to drop PSR-6 compliance and remove those reserved characters or we need to encode/decode values when caching. I don't think this changes how we need to save anything in the database.

  9. objecttothis commented on Feb 12, 2023

    @objecttothis
    MemberAuthor

    For speed, I think it makes sense in addition to the cache option to call the constructor as few times as possible. This means calling the config() convenience method one time in the Controller. It would be super easy to pass the data to the view in the data array too. Doing so would also have the advantage of it being simpler in the view to reference the data. Rather than <?= config('OSPOS')->settings['foo'] ?> if we pass it in the data array from the controller it becomes <?= $config['foo'] ?>

  10. objecttothis commented on Feb 12, 2023

    @objecttothis
    MemberAuthor

    @jekkos on a slightly related note, we need to have whatever Travis build scripts manually change public string $commit_sha1 = 'dev'; in \app\Config\OSPOS.php when it builds so that when the build is downloaded, it has that value hardcoded for installs.

  11. objecttothis commented on Feb 12, 2023

    @objecttothis
    MemberAuthor

    For speed, I think it makes sense in addition to the cache option to call the constructor as few times as possible. This means calling the config() convenience method one time in the Controller. It would be super easy to pass the data to the view in the data array too. Doing so would also have the advantage of it being simpler in the view to reference the data. Rather than <?= config('OSPOS')->settings['foo'] ?> if we pass it in the data array from the controller it becomes <?= $config['foo'] ?>

    I've placed an instance of the OSPOS config in the secure_controller and I'm passing it into a global view, so no need to pass it in the data from the individual controllers to the views since it will already be available. The big question is whether that array gets stale when a row in app_config changes. I think there must be a way to force a new instance or update that instance.

  12. jekkos commented on Feb 12, 2023

    @jekkos
    Member

    I quickly read through it, and it seems indeed that using Factories would be a good solution. Loading the values in the session will probably work but you'll still have to reload them for each user separately, which is probably not the most efficient solution. So the memory should remain accessible by multiple sessions in parallel, and if this can be kept within the php process then that would be great. Already happy that we do not need memcached or any other extra store to do this.

  13. jekkos commented on Feb 12, 2023

    @jekkos
    Member

    @jekkos on a slightly related note, we need to have whatever Travis build scripts manually change public string $commit_sha1 = 'dev'; in \app\Config\OSPOS.php when it builds so that when the build is downloaded, it has that value hardcoded for installs.

    I suppose that should be doable, so changing the file and then trigger an in memory load to fetch the last version. Will need to hash out the details later on

  14. jekkos commented on Feb 12, 2023

    @jekkos
    Member

    I'd just load the config table in memory and then all changes done to the class can update both the in memory and persisted versions. The table is not that big anyway and if this can save the overhead of doing individual queries for every value lookup, then I think it's worth the tradeoff.

  15. objecttothis commented on Feb 13, 2023

    @objecttothis
    MemberAuthor

    I'd just load the config table in memory and then all changes done to the class can update both the in memory and persisted versions. The table is not that big anyway and if this can save the overhead of doing individual queries for every value lookup, then I think it's worth the tradeoff.

    With current implementation, we are saving the ospos_app_config table using a custom config called OSPOS. How to make this fast and persistent is the big question. Every time config('OSPOS') is called, it instantiates a new object. The constructor is populating the settings[] array and then it can be used. The most immediate way to reduce overhead is to call config('OSPOS') just once and pass around the object. The other way to reduce overhead is by caching the database pull and pulling it from cache if the cache exists or populating the cache. I did some benchmarks:
    Raw database pull (no cache): 5ms
    Cache 1st pull (database): 9.5ms
    All subsequent pulls: 2.2ms

    Clearly we aren't talking about large amounts of time regardless of how we do it, so the biggest gains are in calling config('OSPOS') once rather than peppering the view or other code with config('OSPOS')->settings['foo']. For example, once call to config('OSPOS') costs 5ms, so a view with 30 references to config('OSPOS') means 150ms or 73.3ms if caching is used.

    I've already implemented caching using file system driver and it's super simple, so unless we have a good reason to remove it, I think we should keep it and just reduce calls to config('OSPOS')

  16. objecttothis commented on Feb 18, 2023

    @objecttothis
    MemberAuthor

    Done

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions