You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
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.
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.
It seems that our options are down to two, unless I'm missing something
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.
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?
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.
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'] ?>
@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.
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.
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.
@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
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.
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')
Activity
objecttothis commentedon Feb 11, 2023
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.
objecttothis commentedon Feb 11, 2023
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.
objecttothis commentedon Feb 11, 2023
These guys are saying Factories are the way to go (https://forum.codeigniter.com/showthread.php?tid=86689&pid=406697#pid406697)
The documentation is here: https://codeigniter4.github.io/CodeIgniter4/concepts/factories.html
I'm having a difficult time wrapping my mind around the concept.
objecttothis commentedon Feb 12, 2023
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.
objecttothis commentedon Feb 12, 2023
It seems that our options are down to two, unless I'm missing something
@jekkos thoughts?
objecttothis commentedon Feb 12, 2023
I've implemented the cache option but there is a small problem. Cache Config has the following
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.
objecttothis commentedon Feb 12, 2023
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'] ?>objecttothis commentedon Feb 12, 2023
@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.objecttothis commentedon Feb 12, 2023
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.
jekkos commentedon Feb 12, 2023
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.
jekkos commentedon Feb 12, 2023
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
jekkos commentedon Feb 12, 2023
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.
objecttothis commentedon Feb 13, 2023
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')
objecttothis commentedon Feb 18, 2023
Done