Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jbuilder_cache_multi still needed when using Rails 5? #394

Open
aried3r opened this issue Apr 7, 2017 · 3 comments
Open

jbuilder_cache_multi still needed when using Rails 5? #394

aried3r opened this issue Apr 7, 2017 · 3 comments

Comments

@aried3r
Copy link
Contributor

aried3r commented Apr 7, 2017

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".

@mdoyle13
Copy link

I also am unclear on what the status of this is

@mdoyle13
Copy link

mdoyle13 commented Apr 18, 2017

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)

@aried3r
Copy link
Contributor Author

aried3r commented Aug 9, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants