Why cache if the db is still being hit?
oleg-kiviljov opened this issue · 3 comments
Hello, could you please explain what purpose does the caching serve if in order to retrieve the value of the setting the query is still being made?
Talking about get method in Sail::Setting model
def self.get(name)
Sail.instrumenter.increment_usage_of(name)
setting = Setting.for_value_by_name(name).first
return if setting.nil?
if setting.should_not_cache?
setting.safe_cast
else
Rails.cache.fetch("setting_get_#{name}", expires_in: Sail.configuration.cache_life_span) do
setting.safe_cast
end
end
end
Doesn't this defeat purpose of caching?
Thank you for an awesome gem and sorry if this is a stupid question and I just miss something :)
Hello! Thanks for the love, I have a lot of fun maintaining it. It is not stupid at all! In fact, you caught a bug and I owe you the effort of looking into the code.
What happened was actually the following: initially, Sail would just use Rails.cache.fetch
and return the value based on the name straight away. However, some setting types cannot be cached (e.g.: the AB test type needs to randomly return true or false every time it is invoked, so caching would break it).
In an attempt to fix this, I accidentally broke the cache. We need to refactor this part of the code to do the following steps:
- First read from cache based on name if the setting has already been cached
- If it is cached, return straight away
- If not, query the setting and check if it should be cached or not
- If it should be cached, write to the cache and return
- If it shouldn't, just return the value
I think that would be the right approach. Let me know if you have other ideas on how to fix it. Also, let me know if you actually want to put a pull request together for it.
Hi, it was just such an obvious bug that I thought that I don't fully understand something, was just surprised that this gem has 10000 downloads already and no one noticed this yet. I'm totally up for making a fix somewhere on this week and my thoughts on how to fix this exactly match yours. This will be my first contribution to OS (yay!), and I'm not quite familiar with how to do this properly. If we talk about this case in particular, I understand that the steps would be:
- Clone the project
- Fix the bug
- Run the tests
- Open pull request
Is there something else I should consider?
It is a simple bug, but you would be impressed on how hard it is to find contributors. Or even just people willing to give feedback about the project.
Here's how you would go about setting up your local environment:
- Fork the project (click fork on Sail's homepage)
- Clone your fork to your local machine
- Add the original repository as another remote source so that you can keep your fork up to date
git remote add sail git@github.com:vinistock/sail.git
(this is ssh, switch to https if desired)
Now if you rungit remote -v
, you should see bothorigin
pointing to your fork andsail
pointing to the original repo.
It is good practice to set this up, because then you can easily update your fork by using the following:
git fetch sail && git rebase sail/master
- Create a branch, fix the bug and run tests
- Push your branch to your local fork
git push origin my_branch
- Visit Sail's homepage and there should be a button prompting you to open a pull request
All done! Let me know if you have any other questions. If you're in doubt, just open the PR and we can collaborate on it.