1 A single row of the All Movies table 2 cachemovie do

1 a single row of the all movies table 2 cachemovie do

This preview shows page 467 - 470 out of 517 pages.

caches the rendered HTML fragment corresponding to each movie. 1 -# A single row of the All Movies table 2 - cache(movie) do 3 %tr 4 %td= movie.title 5 %td= movie.rating
Image of page 467
6 %td= movie.release_date 7 %td= link_to "More about #{movie.title}", movie_path(movie) Figure 12.8: Compared to Figure 5.2 in Section 5.1 , only line 2 has been added. Rails will generate a name for the cached fragment based on the pluralized resource name and primary key, for example, movies/23 . A convenient shortcut provided by Rails is that if the argument to cache is an ActiveRecord object whose table includes an updated_at or updated_on column, the cache will auto-expire a fragment if its table row has been updated since the fragment was first cached. Nonetheless, for clarity, line 10 of the sweeper in Figure 12.7 shows how to explicitly expire a fragment whose name matches the argument of cache whenever the underlying movie object is saved or destroyed. Unlike action caching, which avoids running the controller action at all, checking the fragment cache occurs after the controller action has run. Given this fact, you may already be wondering how fragment caching helps reduce the load on the database. For example, suppose we add a partial to the list of movies page to display the @top_5 movies based on average review scores, and we add a line to the index controller action to set up the variable: 1 -# a cacheable partial for top movies 2 - cache(’top_moviegoers’) do 3 %ul#topmovies 4 - @top_5.each do |movie| 5 %li= moviegoer.name 1 class MoviegoersController < ApplicationController 2 def index 3 @movies = Movie.all 4 @top_5 = Movie.joins(:reviews).group(’movie_id’). 5 order("AVG(potatoes) DESC").limit(5) 6 end 7 end Action caching is now less useful, because the index view may change when a new movie is added or when a review is added (which might change what the top 5 reviewed movies are). If the controller action is run before the fragment cache is checked, aren’t we negating the benefit of caching, since setting @top_5 in lines 4–5 of the controller method causes a database query? Surprisingly, no. In fact, lines 4–5 don’t cause a query to happen: they construct an object that can do the query if it’s ever asked for the result! This is called lazy evaluation , an enormously powerful programming-language technique that comes from the lambda calculus underlying functional programming. Lazy evaluation is used in Rails’ ActiveRelation (ARel) subsystem, which is used by ActiveRecord. The actual database query doesn’t happen until each is called in line 5 of of the Haml
Image of page 468
template, because that’s the first time the ActiveRelation object is asked to produce a value. But since that line is inside the cache block starting on line 2, if the fragment cache hits, the line will never be executed and therefore the database will never be queried. Of course, you must still include logic in your cache sweeper to correctly expire the top-5-movies fragment when a new review is added.
Image of page 469
Image of page 470

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors