Ognjen Regoje bio photo

Ognjen Regoje
But you can call me Oggy


I make things that run on the web (mostly).
More ABOUT me and my PROJECTS.

me@ognjen.io LinkedIn

Combining cached and live JSON in Rails

#caching #rails #technical

Had a scenario for the Supplybunny API where an endpoint needed to return a combination of live and cached data.

The cached part was already in use in a few places. The new endpoint needed to return a few different pieces so as to avoid making multiple calls.

Never encountered that before and didn’t really find much about it.

This is what I came up with:

response = {
  live: @live,                            # Live data goes in as normal
  cached: "CACHEDHERE",                   # Cached data gets a placeholder
}.to_json

response.gsub!("\"CACHEDHERE\"", @cached) # Placeholder is then replaced with the cached JSON

Has worked well so far.