Today I learned something weird about Rails caching.
I was looking at a feature I re-designed a while ago. Rewrote one of our core algorithms from scratch so it's more readable, easier to extend, and faster. Or so I thought.
Turns out not only is my algorithm slower, it gets linearly slower the more users it sees online. 🤔
So I looked at profiling info on New Relic ...
... my algorithm runs 285 database queries to match a pool of 10 users with a pool of 5 or so chatrooms.
53 of them are just for the Settings object, which changes maybe 3 times per month. This algorithm runs every 30 seconds.
Doesn't Rails have SQL cache? Isn't it supposed to automagically avoid running the same query multiple times!?
Rails does. BUT
But while Rails does have SQL cache, that cache only lasts for 1 action. Basically it only caches the same query run multiple times in the same function.
For anything more than not-actually-cache, you have to use Rails.cache
Down to 232 queries ✌️