Of course this is unsuitable for controller actions protected by before filters

Of course this is unsuitable for controller actions

This preview shows page 466 - 468 out of 517 pages.

Of course, this is unsuitable for controller actions protected by before-filters, such as pages that require the user to be logged in and therefore require executing the controller filter. In such cases, changing caches_page to caches_action will still execute any filters but allow Rails to deliver a cached page without consulting the database or re-rendering views, as in Figure 12.6 (c). Figure 12.9 shows the benefits of page and action caching for this simple example. Note that in Rails page caching, the name of the cached object ignores embedded parameters in URIs such as /movies?ratings=PG+G , so parameters that affect how the page would be displayed should instead be part of the RESTful route, as in /movies/ratings/PG+G . An in-between case involves action caching in which the main page content doesn’t change, but the layout does. For example, your app/views/layouts/application.html.haml may include a message such as “Welcome, Alice” containing the name of the logged-in user. To allow action caching to work properly in this case, passing :layout=>false to caches_action will result in the layout getting fully re-rendered but the action (content part of the page) taking advantage of the action cache. Keep in mind
Image of page 466
that since the controller action won’t be run, any such dynamic content appearing in the layout must be set up in a before-filter. 1 class MoviesController < ApplicationController 2 caches_page :show 3 cache_sweeper :movie_sweeper 4 def show 5 @movie = Movie.find(params[:id]) 6 end 7 end 1 class MovieSweeper < ActionController::Caching::Sweeper 2 observe Movie 3 # if a movie is created or deleted, movie list becomes invalid 4 # and rendered partials become invalid 5 def after_save(movie) ; invalidate ; end 6 def after_destroy(movie) ; invalidate ; end 7 private 8 def invalidate 9 expire_action :action => [’index’, ’show’] 10 expire_fragment ’movie’ 11 end 12 end Figure 12.7: (Top) Line 2 specifies that Rails should cache the result of the show action. Action caching is implemented as a before-filter that checks whether a cached version should be used and an around-filter that captures and caches the rendered output, making it an example of the Decorator design pattern (Section 11.4 ). (Bottom) This “sweeper,” referenced by line 3 of the controller, uses the Observer design pattern (Section 11.7 ) to add ActiveRecord lifecycle hooks (Section 5.1 ) to expire any objects that might become stale as a result of updating a particular movie. Page-level caching isn’t useful for pages whose content changes dynamically. For example, the list of movies page ( MoviesController#index action) changes when new movies are added or when the user filters the list by MPAA rating. But we can still benefit from caching by observing that the index page consists largely of a collection of table rows, each of which depends only on the attributes of one specific movie, as Figure 5.2 in Section 5.1 shows. Figure 12.8 shows how adding a single line to the partial of Figure 5.2 caches the rendered HTML fragment corresponding to each movie.
Image of page 467
Image of page 468

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