Use NastyCache for MeritOrder and Molecules to reduce cache round-trips#1748
Use NastyCache for MeritOrder and Molecules to reduce cache round-trips#1748aaccensi wants to merge 4 commits into
Conversation
MeritOrder collapses 6 individual cache fetches into one fetch_multi call. Molecules does the same for its 2 keys. Both use Thread.current to ensure fetch_multi runs at most once per request. Closes #1747
985694c to
673325d
Compare
Atlas data only changes on ETSource import, so per-request scoping is wasteful. Class-level memoization is reset via NastyCache#expire_local! whenever an import clears Rails.cache. Closes #1747
|
I've looked at this a couple of times but I am not confident about it without more investigation. I also see as I was typing this you've updated the cache clearance @aaccensi so maybe all moot now - will take a look later on |
|
I was looking at the situation with stale data that we should not get after an ETSource import that the solid cache investigation flagged and decided to try to factor also this in here as a test making it more optimal. |
Discussed with @aaccensi and I understand the approach now - I was a little confused before but the approach makes sense to me now and seems practical! |
NastyCache stores data in process memory, avoiding a cache round-trip on every access, and is automatically cleared on ETSource import.
Context
Since adopting Solid Cache, several controller actions that trigger a GQL calculation are flagged by Sentry as N+1 queries. This was not visible before because Memcached round-trips are cheap; with Solid Cache every cache operation is a SQL query.
Implemented changes
MeritOrderandMoleculespreviously usedRails.cache.fetchon every access. Since this data is purely derived from ETSource and never changes between imports, it is a better fit forNastyCache, which stores values in process memory and avoids a cache round-trip on every access.NastyCachealready handles invalidation correctly across all Puma workers: on ETSource import,expire_local!clears@cache_storeon the importing process andmark_expired!writes a new timestamp toRails.cache. Every other process detects the timestamp change on its next request viainitialize_requestand clears its own store.Related
Closes #1747
Checklist