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
{{ message }}
Repository navigation
[Technical Debt]: Fix stale cache data in appconfig #3693
Currently, when the an app_config key/value pair is updated or inserted into the database, the cash gets invalidated then updated. There are two problems:
1- Calling the update_settings() function in the save() function causes a lot of calls to invalidating and updating the cache. Perhaps there is a better place to call this.
2- calling update_settings() invalidates and updates the cache, so any future instances of config('OSPOS') will be current, but existing instances of the config are stale until they are regenerated. This could cause data to be updated in the database and cache but not in the in-memory values.
For 1, We could call update_settings() in the batch_save() function after the foreach loop. This will cut down on all but three calls to save() directly. Then we could call update_settings() after those three calls. The potential risk is that if we add more calls to save() directly and forget to call update_settings() we will end up with stale data in the cache.
As for 2, we need to create a new instance of config('OSPOS') after update_settings is run. The problem with this method is that it's decentralized. Perhaps the best option here is to create a wrapper function and pass the config() instance by reference, then call both the save() function, update_settings() and refresh the config instance together. We may need two functions or to just call batch_save or save from the wrapper based on whether the passed parameters are a single row or multiple.
The crazy thing is that the CI4 cache is by default file backed, so instead of reading the values from the db, we read them from a file, which might not be all that fast in the end? I would only consider something like this if the query is very expensive, but here it should not be the case, it's a very simple table with not a lot of data.
There is no easy in memory solution unless if we enable redis or memcached. But doing this only for this purpose feels a bit overkill. I would just forget about the cache and delegate the load call to the AppConfig model from the OSPOS wrapper instead; If we decide that this is not fast enough we can still consider an in memory solution afterwards. We could load the whole table at once as we used to do in CI3. I think there it used to be request scoped as well, we never had any performance issues on that front, so should be fine.
Also, we are loading the table all at once currently (if the cache is not populated), storing it in memory cache (after pulling it) and in the global array which all gets repeated if a user makes a change to one of the elements of the array. As I said in the other PR, I don't think we should get rid of it just for the sake of getting rid of it (since it's working... I think), but only if it serves a purpose, such as making the code empty value resilient without having to add in checks for empty values.
That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.
btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.
btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.
I'm far enough away to not be in any danger. It seems as if the fighting has stopped anyway.
That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.
Beyond the hope of performance increase, I can't remember now what was driving me to store the array in cache. I can't remember if not doing so was affecting accessibility or not. Feel free to tweak the current implementation as you see fit.
My suggestion is that we use the current implementation if it doesn't mean peppering the code with a bunch of checks and then after CI4 has been released, we consider simplifying it in a separate PR. Thoughts?
btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.
I'm far enough away to not be in any danger. It seems as if the fighting has stopped anyway.
That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.
Beyond the hope of performance increase, I can't remember now what was driving me to store the array in cache. I can't remember if not doing so was affecting accessibility or not. Feel free to tweak the current implementation as you see fit.
My suggestion is that we use the current implementation if it doesn't mean peppering the code with a bunch of checks and then after CI4 has been released, we consider simplifying it in a separate PR. Thoughts?
I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.
I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.
Sounds good to me. If @jekkos isn't opposed, I think you can proceed. There should be an existing migration for ci4. I'd put those changes in that migration so that we don't end up with 100's of migrations.
I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.
Sounds good to me. If @jekkos isn't opposed, I think you can proceed. There should be an existing migration for ci4. I'd put those changes in that migration so that we don't end up with 100's of migrations.
@jekkos has already pointed me to the migration script for the existing database population, so I expect I can extend that very quickly. I will get onto it tomorrow.
There are cases in which just having the array key in existence is not enough to avoid problems. The migration script is now creating a record for 'mailchimp_api_key' but the value is the empty string. In Controllers/Config.php, for example, we need to also test if the value of 'mailchimp_api_key' is not empty before passing it to Services::encrypter->decrypt() because that crashes if not passed a valid string. This would be overcome if we implement an array dereferencing mechanism which tests for both existence and content. In the meantime, I have to submit a revision to Controllers/Config.php to implement this test. Previously it crashed because the key did not exist; now it crashes because the value is empty. It is reasonable to assume other cases where we create an app-config table record with empty data will raise similar problems.
@jekkosNot yet. The current code still has both problems described in #3693:
Repeated cache refreshes:Appconfig::batch_save() calls save() for each key. Each successful save deletes the settings cache and reloads the full table. See app/Models/Appconfig.php, Line 86 and Line 102.
Stale settings copies: Controllers and libraries copy config(OSPOS::class)->settings into their own arrays. Updating the shared configuration does not refresh those copies. For example, see app/Controllers/Config.php, Line 63.
The Mailchimp empty-value problem discussed later is handled in the current code. That does not resolve the original cache problems.
I recommend keeping this issue open until batch saves refresh settings once after a successful transaction, and same-request consumers can read the updated settings.
@objecttothis Thanks for the clarification. #4407 will address the Mailchimp problems. #3693 should remain open for the remaining work:
Refresh the settings cache once after a successful batch-save transaction.
Ensure same-request consumers read updated settings rather than stale copies.
✏️ Learnings added
Learnt from: objecttothis
URL: https://github.com/opensourcepos/opensourcepos/issues/3693
Timestamp: 2026-10-05T06:17:35.130Z
Learning: In opensourcepos/opensourcepos, the maintainer states that #4407 will address the Mailchimp-related configuration problems discussed in #3693. Issue #3693 still covers the unresolved repeated settings-cache refreshes in Appconfig::batch_save() and stale settings copies held by same-request consumers. Do not treat the Mailchimp fix alone as resolution of #3693.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
Activity
jekkos commentedon Sep 24, 2023
The crazy thing is that the CI4 cache is by default file backed, so instead of reading the values from the db, we read them from a file, which might not be all that fast in the end? I would only consider something like this if the query is very expensive, but here it should not be the case, it's a very simple table with not a lot of data.
There is no easy in memory solution unless if we enable redis or memcached. But doing this only for this purpose feels a bit overkill. I would just forget about the cache and delegate the load call to the AppConfig model from the OSPOS wrapper instead; If we decide that this is not fast enough we can still consider an in memory solution afterwards. We could load the whole table at once as we used to do in CI3. I think there it used to be request scoped as well, we never had any performance issues on that front, so should be fine.
objecttothis commentedon Sep 24, 2023
Yes, the default is file, but I believe I changed that to be session based since we use sessions in CI3
objecttothis commentedon Sep 24, 2023
Also, we are loading the table all at once currently (if the cache is not populated), storing it in memory cache (after pulling it) and in the global array which all gets repeated if a user makes a change to one of the elements of the array. As I said in the other PR, I don't think we should get rid of it just for the sake of getting rid of it (since it's working... I think), but only if it serves a purpose, such as making the code empty value resilient without having to add in checks for empty values.
jekkos commentedon Sep 24, 2023
I do believe the appconfig table was fully reloaded in memory at the start of every request in CI3.. I don''t think it was linked to the session at that point. (https://github.com/opensourcepos/opensourcepos/blob/master/application/hooks/load_config.php) . This hook is called pre controller constructor.
That has changed now as we keep the table contents as a json array in a cache that is backed by a file, with ttl of 5 mins. I'm ok to keep it if it works and if we don't need to do any cache eviction once we update the config and refresh it from the database. I would certainly keep the class but then change the implementation a bit so that it can deal with array dereference and missing values directly.
btw @objecttothis do you live in Nagorno Karabach currently? Was just reading the news on the situation in Azerbeidzjan. Hope you're all fine.
objecttothis commentedon Sep 25, 2023
@owlbrudder let's move the conversation here, rather than keeping it going in a merged PR.
https://codeigniter4.github.io/userguide/general/configuration.html#getting-a-config-property
CI4 has a means of accessing built-in configuration files and allowing the user to specify and modify custom configuration files. I had considered the idea of completely scrapping the CI3 implementation, establishing a custom config file with an associative array and populating that from the database, but decided to try to make something similar to our current implementation working since the CI4 conversion was immense as it was.
objecttothis commentedon Sep 25, 2023
My suggestion is that we use the current implementation if it doesn't mean peppering the code with a bunch of checks and then after CI4 has been released, we consider simplifying it in a separate PR. Thoughts?
owlbrudder commentedon Sep 25, 2023
Thank you for the clarification. I agree about the scale of the conversion.
owlbrudder commentedon Sep 25, 2023
I expect 99.9% of the config[] problems will go away if we accept the proposal to write a new migration script to populate the database with all keys we dereference later. This avoids wrapping every dereference in a protective layer. Far better to focus on getting what we have working reliably than to rewrite at this point. Let rewrites become later point releases, but make the existing system work first.
objecttothis commentedon Sep 25, 2023
Sounds good to me. If @jekkos isn't opposed, I think you can proceed. There should be an existing migration for ci4. I'd put those changes in that migration so that we don't end up with 100's of migrations.
owlbrudder commentedon Sep 25, 2023
@jekkos has already pointed me to the migration script for the existing database population, so I expect I can extend that very quickly. I will get onto it tomorrow.
owlbrudder commentedon Oct 5, 2023
There are cases in which just having the array key in existence is not enough to avoid problems. The migration script is now creating a record for 'mailchimp_api_key' but the value is the empty string. In Controllers/Config.php, for example, we need to also test if the value of 'mailchimp_api_key' is not empty before passing it to Services::encrypter->decrypt() because that crashes if not passed a valid string. This would be overcome if we implement an array dereferencing mechanism which tests for both existence and content. In the meantime, I have to submit a revision to Controllers/Config.php to implement this test. Previously it crashed because the key did not exist; now it crashes because the value is empty. It is reasonable to assume other cases where we create an app-config table record with empty data will raise similar problems.
[-]CI4: Stale data in appconfig update of cache[/-][+][Technical Debt]: Fix stale cache data in appconfig[/+]jekkos commentedon Oct 2, 2026
@CodeRabbit can be closed?
coderabbitai commentedon Oct 2, 2026
🧩 Analysis chain
🏁 Script executed:
Length of output: 64433
🏁 Script executed:
Length of output: 19549
🏁 Script executed:
Length of output: 9853
@jekkosNot yet. The current code still has both problems described in#3693:Appconfig::batch_save()callssave()for each key. Each successful save deletes the settings cache and reloads the full table. Seeapp/Models/Appconfig.php, Line 86 and Line 102.config(OSPOS::class)->settingsinto their own arrays. Updating the shared configuration does not refresh those copies. For example, seeapp/Controllers/Config.php, Line 63.The Mailchimp empty-value problem discussed later is handled in the current code. That does not resolve the original cache problems.
I recommend keeping this issue open until batch saves refresh settings once after a successful transaction, and same-request consumers can read the updated settings.
You are interacting with an AI system.
objecttothis commentedon Oct 5, 2026
@coderabbitai the Mailchimp related problems will go away when #4407 is merged. The rest of the issue still needs to be resolved.
coderabbitai commentedon Oct 5, 2026
@objecttothisThanks for the clarification.#4407will address the Mailchimp problems.#3693should remain open for the remaining work:✏️ Learnings added
You are interacting with an AI system.
fix(appconfig): refresh cached settings once and avoid stale same-req…
fix(appconfig): refresh cached settings once and avoid stale same-req…
fix(appconfig): refresh cached settings once and avoid stale same-req…