Content Caching in Rails <ul><li>Ahmad Gozali </li></ul><ul><li>[email_address] </li></ul>
Content Cache <ul><li>Page cache </li></ul><ul><li>Action cache </li></ul><ul><li>Fragment cache </li></ul>
Page Cache (1)‏ <ul><li>Easiest to use </li></ul><ul><li>Fastest </li></ul><ul><li>Worst scaling properties </li></ul>
Page Cache (2)‏ <ul><li>Pages created by Rails as normal </li></ul><ul><li>Written out to public directory </li></ul><ul><...
Enable Page Caching <ul><li>class PostsController < ApplicationController </li></ul><ul><li>caches_page :show </li></ul><u...
Sweepers <ul><li>Rails::Initializer.run do |config| </li></ul><ul><li># ... </li></ul><ul><li>config.load_paths += %W( #{R...
Expires Cache <ul><li>class PostsSweeper < ActionController::Caching::Sweeper </li></ul><ul><li>observe Post </li></ul><ul...
.htaccess <ul><li>RewriteRule ^$ index.html [QSA] </li></ul><ul><li>RewriteRule ^([^.]+)$ $1.html [QSA] </li></ul><ul><li>...
Action Caching <ul><li>Similar to page cache, but runs entirely inside of Rails </li></ul><ul><li>Slower, because Rails is...
Example <ul><li>class PostsController < ApplicationController </li></ul><ul><li>layout 'base' </li></ul><ul><li>before_fil...
Cleanup Action Cache <ul><li>expire_action(:controller => 'posts', :action => 'list')‏ </li></ul><ul><li>expire_action(:co...
Fragment Cache <ul><li>You have to call it yourself, because it works on short bits of data, not whole pages. </li></ul><u...
Example <ul><li><strong>My Blog Posts</strong> </li></ul><ul><li><% cache do %> </li></ul><ul><li><ul> </li></ul><ul><li><...
Post Controllers <ul><li>def list </li></ul><ul><li>unless read_fragment({})‏ </li></ul><ul><li>@post = Post.find(:all, :o...
Expire Fragment Cache <ul><li>expire_fragment(:controller => 'post', :action => 'list')‏ </li></ul>
Demo
Upcoming SlideShare
Loading in...5
×

Cache Your Rails App

1,179

Published on

Published in: Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total Views
1,179
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
26
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Cache Your Rails App

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

    Clipping is a handy way to collect important slides you want to go back to later.

×