outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
271 stars 99 forks source link

nova_get_setting() returns stale results in Tinker #158

Closed nathan-io closed 1 year ago

nathan-io commented 1 year ago

Thanks for this package, we've used in several projects!

Today I noticed a strange behavior that seems to be related to the internal cache used by NovaSettingsStore.

When in Tinker, if I change a setting through the web UI, the change isn't reflected by nova_get_setting() until I exit and restart Tinker.

> nova_get_setting('some_key')
= "test"

Then I change the value to "test2" in Nova and save. Then from the same Tinker session:

> nova_get_setting('some_key')
= "test"

I'm not quite sure how this cache works, but am assuming that it starts as empty on each new request, so we wouldn't need to worry about a stale value on any subsequent request.

What I'm wondering is if this behavior could also happen if a value is changed at some point while processing a request, and then we reference it again before returning the response on that same request?

For example, if we have some API route which we want to have change the value of some nova-setting, and include that new value of the setting in the response body.

Tarpsvo commented 1 year ago

Setting the value via the intended API-s (ienova_set_setting_value() or NovaSettings::setSettingValue()) will force the cache to update. Additionally, the whole cache is dumped when the Settings' model's updating event is fired.

However, if one were to saveQuietly() or directly ->update() a Setting model, they would need to clear the in-memory cache manually using NovaSettings::getStore()->clearCache().

nathan-io commented 1 year ago

@Tarpsvo I don't understand... I'm changing the value in Nova, via a nova-settings field for attribute some_key. Doesn't that use the API as intended?

nathan-io commented 1 year ago

Hi @Tarpsvo could you reopen this? Unless I'm misunderstanding something, the response does not address the issue.

If a user changes a settings value in Nova, via a nova-settings field, does this not use the API and update the cache?

nathan-io commented 8 months ago

Hi @Tarpsvo wanted to follow up on this. I don't believe your response addresses the issue. If a user changes a settings value in Nova, via a nova-settings field, does this not use the API and update the cache?

I've also noticed that our queued jobs continue to use stale settings values, until we run horizon:terminate.