Cache Your Rails App

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

1 Favorite

Cache Your Rails App - Presentation Transcript

  1. Content Caching in Rails
    • Ahmad Gozali
    • [email_address]
  2. Content Cache
    • Page cache
    • Action cache
    • Fragment cache
  3. Page Cache (1)‏
    • Easiest to use
    • Fastest
    • Worst scaling properties
  4. Page Cache (2)‏
    • Pages created by Rails as normal
    • Written out to public directory
    • Cached pages served directly by webserver without involking rails
  5. Enable Page Caching
    • class PostsController < ApplicationController
    • caches_page :show
    • cache_sweeper :posts_sweeper, :only => [:create, :update, :destroy]
    • def show
    • Post.find_by_id(params[:id])‏
    • end
    • end
  6. Sweepers
    • Rails::Initializer.run do |config|
    • # ...
    • config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )‏
    • # ...
    • end
  7. Expires Cache
    • class PostsSweeper < ActionController::Caching::Sweeper
    • observe Post
    • def after_update(post)‏
    • expire_cache_for(post)‏
    • end
    • ...
  8. .htaccess
    • RewriteRule ^$ index.html [QSA]
    • RewriteRule ^([^.]+)$ $1.html [QSA]
    • RewriteCond %{REQUEST_FILENAME} !-f
    • RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
  9. Action Caching
    • Similar to page cache, but runs entirely inside of Rails
    • Slower, because Rails is always involved
    • Much less complex on the web server side; no mod_rewrite tricks
    • Uses the fragment cache internally
  10. Example
    • class PostsController < ApplicationController
    • layout 'base'
    • before_filter :authenticate
    • caches_action :list, :show
  11. Cleanup Action Cache
    • expire_action(:controller => 'posts', :action => 'list')‏
    • expire_action(:controller => 'posts', :action => 'show', :id => record.id)‏
  12. Fragment Cache
    • You have to call it yourself, because it works on short bits of data, not whole pages.
    • read_fragment(key) and write_fragment(key,value)‏
    • Expire with expire_fragment(key) (or regex).
    • No way to list entries.
  13. Example
    • <strong>My Blog Posts</strong>
    • <% cache do %>
    • <ul>
    • <% for post in @posts %>
    • <li><%= link_to post.title, :controller => 'posts', :action => 'show', :id => post %></li>
    • <% end %>
    • </ul>
    • <% end %>
  14. Post Controllers
    • def list
    • unless read_fragment({})‏
    • @post = Post.find(:all, :order => 'created_on desc', :limit => 10) %>
    • end
    • end
  15. Expire Fragment Cache
    • expire_fragment(:controller => 'post', :action => 'list')‏
  16. Demo

+ Achmad GozaliAchmad Gozali, 11 months ago

custom

802 views, 1 favs, 1 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 802
    • 790 on SlideShare
    • 12 from embeds
  • Comments 1
  • Favorites 1
  • Downloads 21
Most viewed embeds
  • 12 views on http://ariekusumaatmaja.wordpress.com

more

All embeds
  • 12 views on http://ariekusumaatmaja.wordpress.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories

Tags