jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

jbuilder_cache_multi still needed when using Rails 5?

Open aried3r opened this issue 7 years ago • 3 comments

The README mentions that

If you are rendering fragments for a collection of objects, have a look at jbuilder_cache_multi gem. It uses fetch_multi (>= Rails 4.1) to fetch multiple keys at once.

but it also mentions that

Fragment caching is supported, it uses Rails.cache and works like caching in HTML templates

Does that mean enhancements in Rails 5 for collection caching (article here) also hold true for jbuilder?

If so, can the documentation be updated? I'd send a PR, but first I wanna make sure this is indeed the case.

Edit: I'm just not sure because I don't think I understand the cache! implementation of this gem exactly and "works like" does not imply "uses the same code as".

aried3r avatar Apr 07 '17 13:04 aried3r

I also am unclear on what the status of this is

mdoyle13 avatar Apr 18 '17 19:04 mdoyle13

I'm using Rails 5.0.0.1 with the latest version of jbuilder. Here is a nested partial setup I have that seems to be working as you'd expect with russian doll caching.

json with array of schedules that have stages, shows and artists. note some of this is pseudo code as i don't want to post everything in my partials :)

json.schedules do
  json.array! schedules.collect do |schedule|
    json.name schedule.name
    json.stages do
      json.array! schedule.shows.collect do |stage|
        json.name stage.name
        json.cache! ['v1', schedule.name, stage.name, stage.shows] do
          json.shows stage.shows, partial: 'api/v1/shows/show', as: :show
        end
      end
    end
  end
end

Show/Event partial: 'api/v1/shows/show'

json.id show.id
json.name show.name
json.cache! ['v1', show.artists] do
  json.artists show.artists, partial: 'api/v1/festival_artists/artist', as: :artist, cache: true
end

and then the last partial - artist partial

json.cache! ['v1', artist] do
  json.name artist.name
  json.id artist.id
end

After initial fragments have been written, subsequent read request looks like this

Read fragment jbuilder/views/v1/10/10/2017/Watch Floor Stage/shows/query-efb697c6d987d38256e6a96c19c35df9-7-20170418202217000000/9b35779922b3d39167ad234882c64e00 (1.5ms)
Read fragment jbuilder/views/v1/10/10/2017/Linen Bell Stage/shows/query-869ecd8ef046e3a9ac1582012b232bea-7-20170418202217000000/9b35779922b3d39167ad234882c64e00 (1.3ms)
Read fragment jbuilder/views/v1/10/10/2017/Canvas Chief Stage/shows/query-5f2d399c86c0c9ec582bbcb12112784a-7-20170412212735000000/9b35779922b3d39167ad234882c64e00 (1.5ms)
Read fragment jbuilder/views/v1/10/10/2017/Bulb Language Stage/shows/query-2aa6eba13075cb9df543d2c60713f200-7-20170418202217000000/9b35779922b3d39167ad234882c64e00 (1.4ms)

mdoyle13 avatar Apr 18 '17 20:04 mdoyle13

Thanks for your code, @mdoyle13!

Btw, I think Rails 5 is unrelated to using jbuilder_fetch_multi, but I'm wondering now that jbuilder only supports Rails 4.2+ and fetch_multi is available since 4.1, maybe the gem can be folded into this one?

aried3r avatar Aug 09 '17 16:08 aried3r