Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Page caching in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 386
    Comment on it

    Page caching is a technique  in which the output of an action is stored as a HTML file and when a request comes for that action,the web server can serve it without going through Action Pack. So basically in this approach we cache the content instead of going dynamically through the process of generating the content.
    If the pages requires authentication or other before/after filters, we can cached the content using the Rails' action_caching gem.

    Lets start by adding the gem to our Gemfile and then run bundle install:

    gem 'actionpack-page_caching'

    After this set page_cache_directory in the configuration file:

    config.action_controller.page_cache_directory = "#{Rails.root.to_s}/public/action_cache"

    Suppose we have a home controller and we want to cache the index and the show action, so we use cache_page to specify which action to cache.

    class HomeController < ActionController::Base
      caches_page :show, :index
    end

    This will generate cache files such as home/show/1.html and home/index.html.
    In page caching, web server check the files on disk, and if found serve them directly without passing the request through to Action Pack
    Expiration of the cache is handled by deleting the cached file.

    class HomeController < ActionController::Base
      def update
        User.update(params[:user][:id], params[:user])
        expire_page action: 'show', id: params[:user][:id]
        redirect_to action: 'show', id: params[:user][:id]
      end
    end

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: